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() {
//
// #ifdef APP-PLUS
getApp().globalData.hostapi = 'http://maintain.7and5.cn';
this.globalData.hostapi = 'http://maintain.7and5.cn';
// #endif
// #ifdef MP-WEIXIN
getApp().globalData.hostapi = 'http://maintain.7and5.cn';
this.globalData.hostapi = 'http://maintain.7and5.cn';
// #endif
// #ifdef H5
getApp().globalData.hostapi = '/web';
this.globalData.hostapi = '/web';
// #endif
},
onShow: function() {

View File

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

View File

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

View File

@ -18,12 +18,42 @@ const goLogin = () => {
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) => {
// console.error("----接口错误----", e)
if (e.data) {
if (e.data.code) {
switch (Number(e.data.code)) {
case 500:
// 接口错误
console.log('500接口错误');
case 4003:
// 参数错误
console.log('4003参数错误');
@ -39,6 +69,8 @@ const checkError = (e) => {
case 5050:
// 服务器错误,请稍后重试
console.log('5050服务器错误请稍后重试');
// 调用到登录页
goLogin();
break;
case 5051:
// 未知错误
@ -47,7 +79,11 @@ const checkError = (e) => {
case 6001:
// token验证失败或已失效
console.log('6001token验证失败或已失效');
goLogin();
if(flag) {
flag = false;
// 调用刷新token事件并跳转到当前页面
refreshTokenPage();
}
break;
}
}
@ -81,20 +117,22 @@ const request = (method, url, options) => {
}
break;
}
let params = {};
if(options!=undefined) params = options;
params.token = uni.getStorageSync('token');
return new Promise((resolve, reject) => {
uni.request({
url: `${hostapi}${url}`,
method: methods,
data: params,
data: options,
header: headers,
success: res => {
console.log(`${url}返的结果===>`,res);
if (res.statusCode == 200) {
// 接口调用成功
resolve(res.data);
if (res.data.code == 0) {
// 接口调用成功
resolve(res.data);
} else {
// 接口返回错误信息
checkError(res);
}
} else {
// 接口返回错误信息
checkError(res);

View File

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

View File

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