因为uview http组件没有返回二次请求所需要的数据config,所以在使用前需要修改一下uview文件让其将config返回。
uview ui框架封装的Http请求获取不到config
Vue.prototype.$u.http.interceptor.response = async (res) => {
if (res.statusCode == 401 || res.statusCode == 422) {
// assessToken相关,刷新令牌
if (res.data.code === 10041 || res.data.code === 10051) {
//刷新数据
const refreshResult = await RefreshToken(res)
return refreshResult.data
}
var msg = ''
if (typeof(res.data.message) == 'object') {
msg = get_object_first_attribute(res.data.message)
}
if (typeof(res.data.message) == 'string') {
msg = res.data.message
}
vm.$u.toast(msg);
// setTimeout(() => {
// // 此为uView的方法,详见路由相关文档
// vm.$u.route('/pages/user/login')
// }, 1500)
return false;
}
}
刷新token并二次请求
async function RefreshToken(response) {
const Authorization = 'Bearer ' + uni.getStorageSync('refresh_token');
var data = await uni.request({
url: configs.url + "/cms/user/refresh",
method: "GET",
header: {
'Authorization': Authorization
}
});
if (data[1].statusCode == 200) {
const Authorization2 = 'Bearer ' + data[1].data.access_token;
var data2 = await uni.request({
url: response.config.url,
method: response.config.method,
header: {
'Authorization': Authorization2
},
data: response.config.data
});
return data2[1]
} else {
uni.showToast({
title: '登录失效,请重新登录',
icon: 'none'
})
uni.redirectTo({
url: "/pages/login/login"
})
}
}