最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解uniapp無痛刷新token方法

 更新時間:2021年06月04日 14:42:24   作者:jser_Amos  
為了給用戶一個流暢的體驗,token過期后需要重新請求新的token替換過期的token。本文將詳細(xì)介紹uniapp無痛刷新token方法。

前端在請求接口時,和后端定義好了,如果狀態(tài)碼為 401 ,則表明 token 過期,需要前端請求新的 token

大概流程如下:

1.用戶登錄之后,后端會返回兩個 token ,分別為accessToken 和refreshToken 存儲到Storage

平時請求數(shù)據(jù)時,請求頭使用accessToken 來發(fā)送接口

2.當(dāng)返回401 token 過期后, 我們通過接口向后端獲取新的 token ,請求參數(shù)為refreshToken

3.我們拿到新的accessToken 和refreshToken 之后, 替換掉之前的Storage 中存儲的 token

4.同時還要將我們報 401 的哪個接口 ,使用新的accessToken ,重新請求一次, 拿到數(shù)據(jù),實現(xiàn)無痛刷新 token

5.如果后端返回的新的 token 也無法使用,表明需要重新登錄,跳到登錄頁(這一步可以靈活使用,我個人用的是一個路由插件配合使用的: https://ext.dcloud.net.cn/plugin?id=578)

配合uni-app的插件進(jìn)行使用和實現(xiàn):

到uni-app的插件市場下載封裝的request網(wǎng)絡(luò)請求,按照文檔配置到自己的項目上

地址:https://ext.dcloud.net.cn/plugin?id=159

配置好后修改vmeitime-http 文件夾下的 index.js 文件

再修改vmeitime-http 文件夾下的 interface.js 文件,將401狀態(tài)暴漏出來

如果看到這里還是看不明白,那么請看我的源碼,請注意我使用了兩個插件,觀眾們酌情理解,仔細(xì)消化,配合到自己的項目中思考...

import http from './interface'
import config from './config'

// request.js
import Vue from 'vue'
import Router  from '@/router'

//...其它邏輯代碼

export const execute = (name, data = {}) => {

    //設(shè)置請求前攔截器
    http.interceptor.request = (config) => {
        let token = uni.getStorageSync('accessToken')
        delete config.header['x-access-token']
        if (token) {
            config.header['x-access-token'] = token
        }
    }
    //設(shè)置請求結(jié)束后攔截器
    http.interceptor.response = async (response) => {
        const statusCode = response.statusCode;
        if (statusCode === 401) {
            response = await doRequest(response)
        }
        if (statusCode === 402) {
            uni.removeStorageSync('accessToken');
            uni.removeStorageSync('refreshToken');
            uni.removeStorageSync('realname');
            let jump = uni.getStorageSync('jump')
            if (!jump) {
                setTimeout(() => {
                    uni.showModal({
                        title: '提示',
                        content: '您的賬號在其它地點登錄!',
                        showCancel: false,
                        success: function(res) {
                            if (res.confirm) {
                                Router.push({
                                    name: 'login',
                                    params: {
                                        'RouterName': 'home'
                                    }
                                })
                            }
                        },
                    })
                });
                uni.setStorageSync('jump', 'true')
            }
        }
        if (statusCode == 403) {
            let jump = uni.getStorageSync('jump')
            if (!jump) {
                setTimeout(() => {
                    Router.replace({
                        name: 'login',
                        params: {
                            'RouterName': 'home'
                        }
                    })
                },500)
                uni.setStorageSync('jump', 'true')
            }
        }
        // 統(tǒng)一處理錯誤請求
        const code = response.data.code;
        const message = response.data.message;
        if (response.statusCode == 200 && code !== 0 && code != -1 && code) {
            uni.showToast({
                title: message,
                icon: "none",
                duration: 2000
            });
        }
        return response;
    }
    return http.request({
        name: name,
        baseUrl: config.base,
        url: config.interface[name].path,
        method: config.interface[name].method ? config.interface[name].method : 'GET',
        dataType: 'json',
        data,
    })
}

export default {
    execute
}
    // 刷新 token 方法
    async function doRequest(response) {
        const res = await execute('refresh', {refreshToken: uni.getStorageSync('refreshToken')})
        const {
            code,
            data
        } = res.data
        if (code == 0) {
            uni.setStorageSync('accessToken', data.accessToken)
            uni.setStorageSync('refreshToken', data.refreshToken)
            let config = response.config
            config.header['x-access-token'] = data.accessToken
            const resold = await execute(config.name,{ ...config.data
            })
            return resold
        } else {
            uni.removeStorageSync('accessToken');
            uni.removeStorageSync('refreshToken');
            uni.showToast({
                title: '登陸過期請重新登陸!',
                icon: "none",
                success() {
                    Router.push({
                        name: 'login',
                        params: {
                            'RouterName': 'home'
                        }
                    })
                }
            });
        }
    }

以上就是詳解uniapp無痛刷新token方法的詳細(xì)內(nèi)容,更多關(guān)于uni-app無痛刷新token方法的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

北辰区| 招远市| 馆陶县| 诸城市| 远安县| 永仁县| 金平| 克拉玛依市| 太仓市| 萝北县| 长顺县| 伽师县| 怀来县| 长宁区| 扎赉特旗| 凌源市| 青铜峡市| 富民县| 高要市| 乌兰浩特市| 武清区| 南充市| 新竹市| 正安县| 万州区| 临颍县| 历史| 铜梁县| 涿鹿县| 祁东县| 聊城市| 枞阳县| 荥经县| 长岛县| 布拖县| 勃利县| 汉寿县| 牙克石市| 宣恩县| 霍邱县| 莱芜市|