2021-09-15uniapp 無(wú)痛刷新ToKen

import Request from './request'
import Config from '@/config.js'
const http = new Request()

http.setConfig((config) => { /* 設(shè)置全局配置 */
    config.baseUrl = Config.baseUrl;
    return config
})

http.validateStatus = (response) => {
    return response.statusCode === 200 || response.statusCode === 401;
}

http.interceptor.request((config, cancel) => { /* 請(qǐng)求之前攔截器 */
    if(config.method === "GET"){
        // config.params.only_time = parseInt(new Date().getTime()/1000);

    }else if(config.method === "POST"){
        // config.data.only_time = parseInt(new Date().getTime()/1000);
    }

    config.header = {
        ...config.header,
        // "blade-auth": uni.getStorageSync('blade-auth') || '',
        "User-Type": "staff",
        "Tenant-Id":"156245",
        "Authorization": "Basic c3dvcmQ6c3dvcmRfc2VjcmV0",
    }
    if(uni.getStorageSync('blade-auth')){
        let obj = {
            "blade-auth": uni.getStorageSync('blade-auth') || '',
        }
        Object.assign(config.header,obj)
    }
    return config
})

http.interceptor.response( async (response) => { /* 請(qǐng)求之后攔截器 */
        const statusCode = response.statusCode;
        if (statusCode === 401) {
            return await doRequest(response); //刷新 token 方法
        }else{
            return response.data
        }
}, (response) => { // 請(qǐng)求錯(cuò)誤做點(diǎn)什么
    return response
})
export {
    http
}
    // 刷新 token 方法
    async function doRequest(response) {
        uni.setStorageSync("blade-auth", "");
        let url = Config.locatBladeAuth + "oauth/token"
        let paramData = {
            'grant_type':'refresh_token',
            'scope':'all',
            'refresh_token': uni.getStorageSync('refresh_token') || '',
        }
        let opitons ={
            header:{
                'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
            }
        };
        const res = await http.post(url, paramData,opitons);
        if(res.access_token){
                uni.setStorageSync("blade-auth", "bearer "+ res.access_token);
                uni.setStorageSync("access_token", res.access_token);
                uni.setStorageSync("refresh_token", res.refresh_token);
                response.config.header['blade-auth'] = "bearer "+res.access_token
                response.config.header['User-Type'] = 'staff'
                response.config.header['Tenant-Id'] = '156245'
                response.config.header['Authorization'] = "Basic c3dvcmQ6c3dvcmRfc2VjcmV0"
                const resold = await http.request(response.config)
                return resold
        }else{
            uni.removeStorageSync('blade-auth');
            uni.removeStorageSync('access_token');
            uni.removeStorageSync('refresh_token');
            uni.removeStorageSync("userInfo");
            uni.showToast({
                title: '登陸過(guò)期請(qǐng)重新登陸',
                icon:'none'
            })
            setTimeout(()=>{
                uni.reLaunch({
                    url:'/pages/login/studentLogin'
                })
            },1000)
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容