| | |
| | | let Constant = require('../utils/constant.js') |
| | | let app = getApp() |
| | | let ACCESS_TOKEN_KEY = 'AccessToken' |
| | | const expiredDateOffset = -1000 * 60 * 5; |
| | | let API = (params) => { |
| | | let tokenInfo = wx.getStorageSync(ACCESS_TOKEN_KEY) |
| | | let currentDateTime = new Date().getTime() |
| | | if (!tokenInfo) { |
| | | wx.reLaunch({ |
| | | url: '/login/login/index', |
| | | }) |
| | | return |
| | | } |
| | | |
| | | const token = tokenInfo.Token; |
| | | const refresh_token = tokenInfo.RefreshToken |
| | | |
| | | if (!refresh_token) { |
| | | wx.reLaunch({ |
| | | url: '/login/login/index', |
| | | }) |
| | | return |
| | | } |
| | | |
| | | //解析token |
| | | const jwt_token = decryptJWT(token); |
| | | // 获取token失效时间 |
| | | const exp_token = getJWTDate(jwt_token.exp).getTime(); |
| | | // token失效状态 |
| | | const isExpireByToken = currentDateTime >= exp_token |
| | | |
| | | //解析refreshtoken |
| | | const jwt_refresh_token = decryptJWT(refresh_token); |
| | | // 获取refreshToken失效时间 |
| | | const exp_refresh_token = getJWTDate(jwt_refresh_token.exp).getTime(); |
| | | // refreshToken失效状态 |
| | | const isExpireByRefreshToken = currentDateTime >= exp_refresh_token |
| | | |
| | | // console.log(utils.formatTime(new Date()), utils.formatTime(getJWTDate(jwt_token.exp)), utils.formatTime(getJWTDate(jwt_refresh_token.exp)), 22) |
| | | // console.log(new Date().getTime() > getJWTDate(jwt.exp).getTime(), 22) |
| | | |
| | | |
| | | // 判断 token和refreshToken是否都失效,都失效则跳转登录 |
| | | if ((isExpireByToken && isExpireByRefreshToken) && tokenInfo.isNeedToken) { |
| | | wx.reLaunch({ |
| | | url: '/login/login/index', |
| | | }) |
| | | return |
| | | } |
| | | |
| | | // 判断 token 失效但refreshToken没失效 |
| | | if ((!isExpireByToken && isExpireByRefreshToken) && tokenInfo.isNeedToken) { |
| | | tokenInfo.Token = refresh_token |
| | | wx.setStorageSync(ACCESS_TOKEN_KEY, tokenInfo) |
| | | } |
| | | |
| | | let defaultParams = { |
| | | url: '', |
| | | method: "GET", |
| | | data: "", |
| | | header: { |
| | | 'content-type': 'application/json' |
| | | 'content-type': 'application/json' |
| | | }, |
| | | success: function () {}, |
| | | fail: function () {}, |
| | | complete: function () {} |
| | | }; |
| | | let requestParam = Object.assign(defaultParams, params); |
| | | let tokenInfo = wx.getStorageSync(ACCESS_TOKEN_KEY) |
| | | let accessToken= tokenInfo.Token |
| | | if (accessToken) { |
| | | // 将 token 添加到请求报文头中 |
| | | requestParam.header.Authorization = `Bearer ${accessToken}`; |
| | | // 判断 accessToken 是否过期 |
| | | const isExpired = checkTokenExpired(accessToken, expiredDateOffset); |
| | | // token 已经过期 |
| | | if (isExpired) { |
| | | // 获取刷新 token |
| | | const tokenInfo2 = wx.getStorageSync(ACCESS_TOKEN_KEY); |
| | | let refreshAccessToken=tokenInfo2.RefreshToken |
| | | if (refreshAccessToken) { |
| | | const refreshIsExpired = checkTokenExpired(refreshAccessToken); |
| | | if (refreshIsExpired) { |
| | | wx.reLaunch({ |
| | | url: '/login/login/index', |
| | | }) |
| | | return |
| | | } else { |
| | | requestParam.header.Authorization = `Bearer ${refreshAccessToken}`; |
| | | } |
| | | } |
| | | } |
| | | |
| | | let requestParam = Object.assign(defaultParams, params) |
| | | // 判断是否要添加token |
| | | if (tokenInfo.isNeedToken == true) { |
| | | requestParam.header.Authorization = 'Bearer ' + tokenInfo.Token |
| | | } |
| | | wx.request({ |
| | | } |
| | | |
| | | |
| | | wx.request({ |
| | | url: requestParam.url, |
| | | method: requestParam.method, |
| | | data: requestParam.data, |
| | | header: requestParam.header, |
| | | success: res => { |
| | | let result = res.data |
| | | if (result.data != undefined && result.data == 401) { |
| | | RefreshToken() |
| | | return |
| | | } |
| | | if (result.code && result.code != 0) { |
| | | wx.showToast({ |
| | | title: result.message, |
| | | icon: 'error', |
| | | duration: 1500 |
| | | }) |
| | | RefreshToken() |
| | | return |
| | | } |
| | | requestParam.success(res) |
| | | }, |
| | | fail: err => { |
| | | requestParam.fail(err) |
| | | }, |
| | | complete: () => { |
| | | requestParam.complete() |
| | | } |
| | | }) |
| | | // 只能叫 access-token,不能跟 accessTokenKey 一样 |
| | | const accessToken = res.header['access-token']; |
| | | // 只能叫 x-access-token',不能跟 refreshAccessTokenKey 一样 |
| | | const refreshAccessToken = res.header['x-access-token']; |
| | | |
| | | } |
| | | const RefreshToken = () => { |
| | | // 判断是否是无效 token |
| | | if (accessToken === 'invalid_token') { |
| | | wx.reLaunch({ |
| | | url: '/login/login/index', |
| | | url: '/login/login/index' |
| | | }) |
| | | return |
| | | |
| | | // 获取账户信息 |
| | | // let accountInfo = JSON.parse(wx.getStorageSync("AccountInfo")) |
| | | |
| | | // console.log(accountInfo,60) |
| | | // 获取登录的用户信息 |
| | | // let userInfo = wx.getStorageSync("LoginUserInfo"); |
| | | // let sysInfo = app.globalData.sysInfo; |
| | | // console.log('打印userInfo', userInfo) |
| | | // let data = { |
| | | // SoftType: Constant.SoftType, |
| | | // SoftTag: Constant.SoftTag, |
| | | // LoginName: accountInfo.LoginName, |
| | | // LoginPwd: accountInfo.LoginPwd, |
| | | // CorpID: Constant.CorpID, |
| | | // WxCode: accountInfo.WxCode, |
| | | // UJsCode: accountInfo.UJsCode, //是否就是jsCode有待确认 |
| | | // UAvatarUrl: userInfo == null ? "" : userInfo.avatarUrl, |
| | | // UCity: userInfo == null ? "" : userInfo.city, |
| | | // UProvince: userInfo == null ? "" : userInfo.province, |
| | | // UNickName: userInfo == null ? "" : userInfo.nickName, |
| | | // UGender: userInfo == null ? "" : userInfo.gender + "", |
| | | // SBrand: sysInfo.brand, |
| | | // SModel: sysInfo.model, |
| | | // SPlatform: sysInfo.platform, |
| | | // SVersion: sysInfo.version |
| | | // } |
| | | // console.log(JSON.stringify(data),110) |
| | | // wx.request({ |
| | | // url: Constant.BASE_SERVER_URL + "User/UserLogin/FromCorpWechatApplet@V1.0", |
| | | // method: 'POST', |
| | | // data: data, |
| | | // header: { |
| | | // 'content-type': 'application/json' |
| | | // }, |
| | | // fail: function (err) { //请求失败 |
| | | // wx.showModal({ |
| | | // content: '网络信号不好', |
| | | // showCancel: false, |
| | | // confirmColor: "#39b5de" |
| | | // }); |
| | | // }, |
| | | // success: function (res) { |
| | | // console.log(res, "新的token信息") |
| | | |
| | | // var result = res.data; |
| | | // // console.log(result); |
| | | // if (result.Code != 0) { |
| | | // wx.showToast({ |
| | | // title: 'token获取失败,请重新登录', |
| | | // icon: "none", |
| | | // duration: 2000 |
| | | // }) |
| | | // return; |
| | | // } |
| | | |
| | | // var return_data = result.Data; |
| | | // if (return_data.Status != 0) { |
| | | // wx.showToast({ |
| | | // title: 'token获取失败,请重新登录', |
| | | // icon: "none", |
| | | // duration: 2000 |
| | | // }) |
| | | // return; |
| | | // } |
| | | // let Token = return_data.Token |
| | | // let isNeedToken = false //是否需要Token 当Token失效的时候 判断是否跳转登录 |
| | | // if (Token) { |
| | | // isNeedToken = true |
| | | // } |
| | | // let ExpreTime = new Date().getTime() //获取当前时间戳 |
| | | |
| | | // wx.setStorageSync('AccessToken', { |
| | | // Token: Token, |
| | | // ExpreTime: ExpreTime, |
| | | // isNeedToken: isNeedToken |
| | | // }) |
| | | // wx.setStorageSync("AccountInfo", JSON.stringify(data)) |
| | | // // 获取新的token后重新调用接口 |
| | | // apiCb(params) |
| | | // } |
| | | // }); |
| | | } |
| | | // 判断是否存在刷新 token,如果存在则存储在本地 |
| | | else if (refreshAccessToken && accessToken && accessToken !== 'invalid_token') { |
| | | wx.setStorageSync(ACCESS_TOKEN_KEY, { |
| | | Token: accessToken, |
| | | RefreshToken: refreshAccessToken, |
| | | }); |
| | | } |
| | | let result = res.data; |
| | | if (result.Data!= undefined && result.Data == 401) { |
| | | // 刷新成功后重新发起请求 |
| | | wx.reLaunch({ |
| | | url: '/login/login/index' |
| | | }) |
| | | return; |
| | | } |
| | | if (result.Code && result.Code!= 0) { |
| | | if (result.Code == -1&& result.Error === 'A099') { |
| | | wx.reLaunch({ |
| | | url: '/login/login/index' |
| | | }) |
| | | return; |
| | | |
| | | } |
| | | } |
| | | requestParam.success(res); |
| | | }, |
| | | fail: err => { |
| | | requestParam.fail(err); |
| | | }, |
| | | complete: () => { |
| | | requestParam.complete(); |
| | | } |
| | | }); |
| | | |
| | | } |
| | | const checkTokenExpired = (token, expiredOffset = 0) => { |
| | | const jwt = decryptJWT(token); |
| | | const date = getJWTDate(jwt.exp) |
| | | const exp = date.getTime() + expiredOffset; |
| | | const currentTime = new Date().getTime(); |
| | | const isExpired = currentTime > exp; |
| | | return isExpired; |
| | | }; |
| | | /** |
| | | * 刷新令牌的函数 |
| | | * @returns {Promise} - 刷新令牌的异步操作的 Promise 对象 |
| | | */ |
| | | |
| | | //解密 JWT token 的信息 |
| | | function decryptJWT(token) { |
| | | |
| | | token = weappJwt(token) |
| | | return token |
| | | let jwt_token = defaultClone(token) |
| | | return weappJwt(jwt_token) |
| | | } |
| | | function defaultClone (value){ |
| | | return JSON.parse(JSON.stringify(value)) |
| | | } |
| | | |
| | | //将 JWT 时间戳转换成 Date |