|
@@ -1,5 +1,6 @@
|
|
|
import {baseUrl} from './config.js'
|
|
|
import store from '@/store/index.js'
|
|
|
+import {apiWXLogin} from '@/api/user.js'
|
|
|
// 请求错误消息提示
|
|
|
const showError=error=>{
|
|
|
let errMsg=''
|
|
@@ -18,6 +19,50 @@ const showError=error=>{
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+// 刷新token 微信登录
|
|
|
+const wechatLogin=()=>{
|
|
|
+ return new Promise((resolve,reject)=>{
|
|
|
+ uni.login({
|
|
|
+ provider: 'weixin',
|
|
|
+ success: function (loginRes) {
|
|
|
+ apiWXLogin({Code:loginRes.code}).then(res=>{
|
|
|
+ if(res.code===200){
|
|
|
+ store.commit('addToken', res.data.Authorization)
|
|
|
+ resolve(res.data)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ fail:function(loginErr){
|
|
|
+ uni.showToast({
|
|
|
+ title:"微信登录失败",
|
|
|
+ icon:"none"
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+let requestList=[]//存放token失效时请求队列
|
|
|
+let isRefreshing=false//是否正在刷新token
|
|
|
+/**
|
|
|
+ * 刷新token
|
|
|
+ */
|
|
|
+const refreshToken=async (url,params,method,resolve)=>{
|
|
|
+ requestList.push(()=>{resolve(http(url,params,method))})
|
|
|
+ if(!isRefreshing){
|
|
|
+ isRefreshing=true
|
|
|
+ const wechatLoginRes=await wechatLogin()
|
|
|
+ console.log(wechatLoginRes);
|
|
|
+ // 重新请求队列
|
|
|
+ requestList.map(MT=>{MT()})
|
|
|
+ requestList=[]
|
|
|
+ isRefreshing=false
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
// 请求数
|
|
|
let LOADINGCOUNT = 0;
|
|
|
const http=(url,params,method)=>{
|
|
@@ -39,12 +84,16 @@ const http=(url,params,method)=>{
|
|
|
Authorization:store.state.token,
|
|
|
},
|
|
|
success(res) {
|
|
|
- if(res.data.code!==200){
|
|
|
+ if(![200,401,403].includes(res.data.code)){
|
|
|
setTimeout(()=>{showError(res.data)},0)//解决 hideloading 冲突问题
|
|
|
}
|
|
|
//401 代表token异常,用户需要重新静默授权,获取最新的token
|
|
|
//403 用户需要进行绑定操作,需要跳转到输入账号密码绑定页面用户需要进行绑定操作
|
|
|
- if(res.data.code===401||res.data.code===403){
|
|
|
+ if(res.data.code===401){
|
|
|
+ refreshToken(url,params,method,resolve)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if(res.data.code===403){
|
|
|
uni.reLaunch({
|
|
|
url:"/pages/login/login"
|
|
|
})
|