instance.interceptors.response.use(
    response => {
        const data = response.data || {};
        const errorT = data.error || {};
        //22006
        if (data.success !== true && errorT.errorText && ![22006, 22001, 22002, 22005].includes(errorT.errorCode || 9999)) {
            MsgBox.error(errorT.errorText);
            // return;
        } else if (!data.success && [22001, 22006].includes(errorT.errorCode || 9999)) {
            let message = "用户过期或退出,重新登录";
            MsgBox.error(message);
            $requestLogin.toAuthLogin($routerT.currentRoute.fullPath);
            // return;
        } else if (errorT.errorCode === 22002) {
            // let message = "用token换取新的token";
            let tokenO = loginAuth.getToken() || {};
            // console.log(tokenO.refreshToken);
            const config = response.config;
            if (isRefreshing === false) {
                isRefreshing = true;
                console.log('refresh1');
                return refreshToken({
                    refreshToken: tokenO.refreshToken
                }).then(res => {
                    //直接跳转到登录/默认页面
                    //res.data.success === false || (res.error || {}) === 22005
                    //res.data.success === false &&
                    let resultT = (res.data.error || {}).errorCode === 22005;
                    if (resultT !== true) {
                        //token获取成功,重新赋值
                        let dataT = res.data.data || {};
                        const token = {
                            tokenType: dataT.tokenType,
                            accessToken: dataT.accessToken,
                            expires: dataT.expires,
                            timeStamp: dataT.timeStamp,
                            refreshToken: dataT.refreshToken
                        };
                        // accessToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJSRzMiLCJhdWQiOiJhcGkiLCJpZCI6InB0ZGV2IiwibmFtZSI6ImRldiIsImNsaWVudF9pZCI6InB0ZGV2Iiwib3Blbl9pZCI6IiIsIndiX2lkIjoiIiwid3hfb3Blbl9pZCI6IiIsInd4X3VuaW9uX2lkIjoiIiwidGVsZXBob25lIjoiIiwicHVzaF9uYW1lIjoi5byA5Y-R566h55CG5ZGYIiwiaGVhZF9pY29uIjoiIiwiZGVwX2lkIjoiIiwic3lzX2lkIjoiIiwibWVyZ2VfdXNlcl9pZCI6IiIsImFyZWFfY29kZSI6IiIsInJvbGVfaWQiOiJkZXZlbG9wbWVudCIsImRldmVsb3BlciI6IjEiLCJuYmYiOjE2MTQwOTMxNjAsImV4cCI6MTYxNDA5MzQ2MCwiaWF0IjoxNjE0MDkzMTYwfQ.NIGyK2GxauKWbLc7uZu_Z7_LweNbIhF-ne_1U_MpGRc"
                        // expires: "2021-03-02T23:17:40"
                        // refreshToken: "YjY3RALvh0OZIjiH8o9ld91H1c1HgMKZ"
                        // timeStamp: 1614093460600.7488
                        // tokenType: "Bearer"
                        const user = loginAuth.getUser();
                        loginAuth.saveAuth({}, user, token)
                            // 已经刷新了token,将所有队列中的请求进行重试
                        requests.forEach(cb => cb(user, token))
                        requests = []
                        return instance(config)
                    } else {
                        //直接退出,避免死循环
                        isRefreshing = false;
                        let message = "用户过期或退出,重新登录";
                        MsgBox.error(message);
                        $requestLogin.toAuthLogin($routerT.currentRoute.fullPath);
                    }
                }).catch(res => {
                    console.log(res)
                    window.location.href = '/'
                }).finally((res) => {
                    console.log(res)
                    isRefreshing = false;
                    // let message = "用户过期或退出,重新登录";
                    // MsgBox.error(message);
                    // $requestLogin.toAuthLogin($routerT.currentRoute.fullPath);
                })
            } else {
                console.log('refresh2');
                // 正在刷新token,将返回一个未执行resolve的promise
                return new Promise((resolve) => {
                    console.log('refresh3');
                    // 将resolve放进队列,用一个函数形式来保存,等token刷新后直接执行
                    requests.push((user, token) => {
                        console.log('refresh4');
                        // config.baseURL = ''
                        // config.headers['X-Token'] = token
                        loginAuth.saveAuth({}, user, token);
                        resolve(instance(config))
                    })
                })
            }
        }
        return response
    }, error => {
        store.dispatch("dataLoading", false);
        MsgBox.error("请求超时");
        return Promise.reject(error)
    })

export default instance
文档更新时间: 2021-10-18 09:38   作者:admin