我们在接口请求时,大部分接口都需要鉴权接口,此时就需要拿到token,但是token是有过期时间的,所以需要在过期之前刷新token.
以下是请求刷新 token逻辑图:
本次开发项目是小程序
import $store from '../store'
import { GetStorageSync, SetStorageSync, RemoveStorageSync } from './util'
import { LmRefreshToken } from "../api/wxFetch";
//刷新token
export function RefreshToken() {
let tokenInfo = GetStorageSync( 'tokenInfo' );
const { refreshToken } = tokenInfo;
return LmRefreshToken( { 'refreshToken': refreshToken } ).then( res => {
console.log( '刷新token--res' );
if ( res.code == 100 && res.data.accessToken ) {
RemoveStorageSync( tokenInfo );
SetStorageSync( 'tokenInfo', res.data );
$store.commit( 'SET_TOKEN_INFO', res.data );
return res.data.accessToken
} else {
return null
}
} ).catch( res => {
console.log( '刷新token失败-', res );
return null
} )
}
/**
*
accessToken: ""
accessTokenValidateTime: 1603519513620
refreshToken: ""
refreshTokenValidateTime: 1604728993620
*/
//get token 的方法,异步
export function getAccessToken() {
let tokenInfo = GetStorageSync( 'tokenInfo' );
const { accessToken, accessTokenValidateTime, refreshToken, refreshTokenValidateTime } = tokenInfo;
let timestamp = new Date().getTime();
if ( accessToken && accessTokenValidateTime && accessTokenValidateTime - timestamp > 1000 * 60 * 60 * 24 * 6 ) { //7天
// token 存在且有效
console.log( 'token 存在且有效' );
return new Promise( ( resolve, reject ) => {
resolve( accessToken )
} )
} else if ( refreshToken && refreshTokenValidateTime && refreshTokenValidateTime - timestamp > 1000 * 60 * 60 * 24 * 13 ) {//14天
// token无效 rt有效,刷新token 重新获取token并记录
console.log( 'token无效 rt有效,刷新token 重新获取token并记录' );
return RefreshToken();
} else {
// token无效 rt无效
console.log( 'token无效 rt无效' );
return new Promise( ( resolve, reject ) => {
resolve( null )
RemoveStorageSync( 'tokenInfo' );
uni.navigateTo( {
url: '/pages/login/index'
} );
} )
}
}
const request = ( options, accessToken ) => {
const { openId, unionId } = GetStorageSync( 'userInfo' )
let a = openId && { openId },
b = unionId && { unionId },
c = accessToken && { accessToken },
contentType = options.contentType;
if ( typeof ( contentType ) == 'undefined' || contentType == null ) {
contentType = 'application/json; charset=UTF-8';
};
let optionsData = {
...options,
header: {
'content-Type': contentType,
...a,
...b,
...c
},
}
if ( optionsData.loading == true ) {
uni.showLoading();
}
function RequestHideLoading() {
if ( optionsData.loading == true ) {
uni.hideLoading();
}
}
function RequestShowToast( title ) {
setTimeout( () => {
uni.showToast( {
title: title,
icon: 'none',
duration: 2000
} )
}, 200 );
}
return uni.request( optionsData ).then( res => {
RequestHideLoading();
// console.log( res, 'uni.request' );
if ( res[ 0 ] ) {
// RequestShowToast( res[ 0 ].errMsg )
RequestShowToast( '网络异常' )
throw new Error( res[ 0 ] );
} else {
console.log( res[ 1 ].statusCode, 'res[ 1 ].statusCode' );
if ( res[ 1 ].statusCode !== 200 ) {
RequestShowToast( res[ 1 ].data.error )
} else {
console.log( res[ 1 ].data, 'res[1].data' );
return res[ 1 ].data
}
}
} )
}
//鉴权请求
export const requestAuth = ( options ) => {
return getAccessToken().then( accessToken => {
return request( options, accessToken )
} )
}
//非鉴权请求
export const requestNoAuth = ( options ) => {
return request( options )
}
export const SetStorageSync = ( key, value ) => {
uni.setStorageSync( STORAGE + key, value )
}
export const GetStorageSync = ( key ) => {
return uni.getStorageSync( STORAGE + key )
}
export const RemoveStorageSync = ( key ) => {
uni.removeStorageSync( STORAGE + key )
}