当调用业务接口,返回状态码 为402,则token 失效;
需要调用刷新token 接口,/refresh 通过refreshToken获取新Token
http://ip:port/swagger-ui.html#/login-controller/refreshUsingPOST
再将获取到的token 以及刷新token 信息等存起来。
if (callbackdata.status == 402) {
let Refresh_Token = uni.getStorageSync('Refresh_Token');
refreshToken(`/refresh/%7BrefreshToken%7D?refreshToken=`+Refresh_Token, 'post',
function(data) {
var portaldata =data.data.data;
uni.setStorageSync('user', portaldata.user);
uni.setStorageSync('access_token', portaldata.Authentication);
uni.setStorageSync('Refresh_Token', portaldata.Refresh_Token);
setTimeout(function() {
let ssoArr = uni.getStorageSync('portaldata');
ssoArr.Refresh_Token =portaldata.Refresh_Token;
ssoArr.Authentication =portaldata.Authentication;
if (uni.getSystemInfoSync().platform == 'android') {
// #ifdef APP-PLUS
let nd = plus.android.invoke('com.zb.portal.zbmobile.NativeDispatch', 'getInstance');
plus.android.invoke(nd, 'updateArg', JSON.stringify(ssoArr));
// #endif
} else {
// #ifdef APP-PLUS
let nd = plus.ios.invoke('NativeDispatch', 'shareInstance');
plus.ios.invoke(nd, 'updateArg:', JSON.stringify(ssoArr));
// #endif
}}, 1000);
}
onLineCommRequest(actionurl, params, headers, requestType, callback);
})
刷新token 回调
//刷新token
const refreshToken = function(actionurl, requestType, successback) {
let url = yzscmobile + "/AUTH" + actionurl;
uni.request({
url: url,
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
method: requestType,
success: data => {
let callbackdata = data.data;
if(callbackdata.status==401){
uni.showModal({
title: '提示',
content: '登录信息已过期,请退出移动门户重新登录。',
confirmText: '确认',
success: res => {
if(plus.runtime.appid=='HBuilder'){
uni.navigateBack({
delta: 1
});
} else if (uni.getSystemInfoSync().platform == 'android') {
// #ifdef APP-PLUS
var nd = plus.android.invoke('com.zb.portal.zbmobile.NativeDispatch', 'getInstance');
plus.android.invoke(nd, 'uniExit');
// #endif
} else {
// #ifdef APP-PLUS
plus.ios.invoke('NativeDispatch', 'uniExit');
// #endif
}
}
})
}else{
successback(data);
}
}
})
}
文档更新时间: 2020-03-07 15:58 作者:linjunlong