request.js中token失效处理

master
chen 2022-04-14 18:30:07 +08:00
parent 631399f32c
commit de2e3cbb43
6 changed files with 61 additions and 33 deletions

View File

@ -10,13 +10,13 @@
onLaunch: function() { onLaunch: function() {
// //
// #ifdef APP-PLUS // #ifdef APP-PLUS
getApp().globalData.hostapi = 'http://maintain.7and5.cn'; this.globalData.hostapi = 'http://maintain.7and5.cn';
// #endif // #endif
// #ifdef MP-WEIXIN // #ifdef MP-WEIXIN
getApp().globalData.hostapi = 'http://maintain.7and5.cn'; this.globalData.hostapi = 'http://maintain.7and5.cn';
// #endif // #endif
// #ifdef H5 // #ifdef H5
getApp().globalData.hostapi = '/web'; this.globalData.hostapi = '/web';
// #endif // #endif
}, },
onShow: function() { onShow: function() {

View File

@ -105,8 +105,7 @@
// }).exec(); // }).exec();
// //
this.$toolAll.tools.obtainUrl(); this.$toolAll.tools.obtainPagePath();
this.$toolAll.tools.obtainUrlParam();
setTimeout(()=>{ setTimeout(()=>{
this.ifNet = uni.getStorageSync('isNet'); this.ifNet = uni.getStorageSync('isNet');
},500) },500)

View File

@ -1,7 +1,7 @@
<template> <template>
<view class="status-box statusHNH" :style="{marginBottom: marginBottom + 'rpx'}"> <view class="status-box" :style="{marginBottom: marginBottom + 'rpx'}">
<!-- 网络电量栏 start --> <!-- 网络电量栏 start -->
<view :style="{height: height+'px',background: backgroudColor}"></view> <view :style="{height: statusBarHeight+'px',background: backgroudColor}"></view>
<!-- 网络电量栏 end --> <!-- 网络电量栏 end -->
<!-- 头部状态栏 start --> <!-- 头部状态栏 start -->
@ -101,7 +101,7 @@
}, },
data(){ data(){
return { return {
height: uni.getSystemInfoSync().statusBarHeight, statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
ifNet:true ,// ifNet:true ,//
netText:'当前无网络', netText:'当前无网络',
netTimer:null netTimer:null
@ -110,15 +110,8 @@
mounted() { mounted() {
// //
this.$toolAll.tools.networkStatus(); this.$toolAll.tools.networkStatus();
// +
// const query = wx.createSelectorQuery().in(this)
// query.select('.statusHNH').boundingClientRect((rect) => {
// uni.setStorageSync('statusHNH',rect.height)
// }).exec();
// //
this.$toolAll.tools.obtainUrl(); this.$toolAll.tools.obtainPagePath();
this.$toolAll.tools.obtainUrlParam();
setTimeout(()=>{ setTimeout(()=>{
this.ifNet = uni.getStorageSync('isNet'); this.ifNet = uni.getStorageSync('isNet');
},500) },500)

View File

@ -18,12 +18,42 @@ const goLogin = () => {
url: '/pages/login/login' url: '/pages/login/login'
}) })
} }
let flag = true;
// 刷新token并跳转到当前页面
const refreshTokenPage = () => {
uni.login({
provider: 'weixin',
success: (result)=> {
uni.request({
url: `${hostapi}user/login`,
method: 'post',
data: {code:result.code},
success: res => {
if(res.data.data.token!=''){
flag = true;
uni.setStorageSync('token',res.data.data.token); // 缓存token
uni.setStorageSync('openid',res.data.data.openid)//缓存openid
uni.setStorageSync('expire',res.data.data.expire); // 缓存失效时间(时间戳格式)
uni.setStorageSync('phone_active',res.data.data.phone_active); // 是否绑定手机号
uni.setStorageSync('is_active',res.data.data.is_active)//是否第一次授权
uni.reLaunch({ // 重新进入当前页面
url:uni.getStorageSync('page-path-options')
})
}
}
})
},
});
}
// 请求错误处理 // 请求错误处理
const checkError = (e) => { const checkError = (e) => {
// console.error("----接口错误----", e) // console.error("----接口错误----", e)
if (e.data) { if (e.data) {
if (e.data.code) { if (e.data.code) {
switch (Number(e.data.code)) { switch (Number(e.data.code)) {
case 500:
// 接口错误
console.log('500接口错误');
case 4003: case 4003:
// 参数错误 // 参数错误
console.log('4003参数错误'); console.log('4003参数错误');
@ -39,6 +69,8 @@ const checkError = (e) => {
case 5050: case 5050:
// 服务器错误,请稍后重试 // 服务器错误,请稍后重试
console.log('5050服务器错误请稍后重试'); console.log('5050服务器错误请稍后重试');
// 调用到登录页
goLogin();
break; break;
case 5051: case 5051:
// 未知错误 // 未知错误
@ -47,7 +79,11 @@ const checkError = (e) => {
case 6001: case 6001:
// token验证失败或已失效 // token验证失败或已失效
console.log('6001token验证失败或已失效'); console.log('6001token验证失败或已失效');
goLogin(); if(flag) {
flag = false;
// 调用刷新token事件并跳转到当前页面
refreshTokenPage();
}
break; break;
} }
} }
@ -81,20 +117,22 @@ const request = (method, url, options) => {
} }
break; break;
} }
let params = {};
if(options!=undefined) params = options;
params.token = uni.getStorageSync('token');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
uni.request({ uni.request({
url: `${hostapi}${url}`, url: `${hostapi}${url}`,
method: methods, method: methods,
data: params, data: options,
header: headers, header: headers,
success: res => { success: res => {
console.log(`${url}返的结果===>`,res); console.log(`${url}返的结果===>`,res);
if (res.statusCode == 200) { if (res.statusCode == 200) {
// 接口调用成功 if (res.data.code == 0) {
resolve(res.data); // 接口调用成功
resolve(res.data);
} else {
// 接口返回错误信息
checkError(res);
}
} else { } else {
// 接口返回错误信息 // 接口返回错误信息
checkError(res); checkError(res);

View File

@ -561,18 +561,16 @@ const tools = {
// #endif // #endif
}, },
// 获取当前页面url不带参数 // 获取当前页面url不带参数
obtainUrl(){ obtainPagePath(){
let pages = getCurrentPages(); let pages = getCurrentPages();
// 获取纯页面路径
let route = pages[pages.length - 1].route; let route = pages[pages.length - 1].route;
uni.setStorageSync('url',`/${route}?invite_code=${uni.getStorageSync('invite_code')}`); uni.setStorageSync('url',route);
console.log(`${route}`,'tools.js当前页面路径不带参数') // 获取当前页面url带参数
},
// 获取当前页面url带参数
obtainUrlParam(){
let pages = getCurrentPages();
let routeParam = pages[pages.length - 1].$page.fullPath; let routeParam = pages[pages.length - 1].$page.fullPath;
uni.setStorageSync('urlParam',`${routeParam}?invite_code=${uni.getStorageSync('invite_code')}`); console.log(routeParam.options,'获取当前url参数');
console.log(uni.getStorageSync('urlParam'),'tools.js当前页面路径带参数') uni.setStorageSync('page-path-options',routeParam);
console.log(uni.getStorageSync('page-path-options'),'当前页面完整路径');
}, },
// 去这里 // 去这里
goFlag:true, goFlag:true,

View File

@ -68,7 +68,7 @@
}, },
onLoad() { onLoad() {
// url // url
this.$toolAll.tools.obtainUrl(); this.$toolAll.tools.obtainPagePath();
}, },
methods: { methods: {
switchType(id){ switchType(id){