|
@@ -1,70 +1,70 @@
|
|
|
import {body, get} from "@/utils/https";
|
|
import {body, get} from "@/utils/https";
|
|
|
|
|
|
|
|
-const _tokenQueue: {
|
|
|
|
|
- pending: boolean;
|
|
|
|
|
- list: {
|
|
|
|
|
- resolve: (value: string) => void;
|
|
|
|
|
- reject: (err: any) => void;
|
|
|
|
|
- }[];
|
|
|
|
|
-} = {
|
|
|
|
|
- pending: false,
|
|
|
|
|
- list: [],
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-let _onTokenListener: {
|
|
|
|
|
- cb: (token: string) => void;
|
|
|
|
|
-}[] = [];
|
|
|
|
|
-
|
|
|
|
|
-const _resolveTokenQueue = function (res: any) {
|
|
|
|
|
- _tokenQueue.pending = false;
|
|
|
|
|
- uni.hideLoading();
|
|
|
|
|
- if (res.errMsg) {
|
|
|
|
|
- console.log(res);
|
|
|
|
|
- uni.showModal({
|
|
|
|
|
- title: "登录失败",
|
|
|
|
|
- content: `${res.errMsg}`,
|
|
|
|
|
- });
|
|
|
|
|
- _tokenQueue.list.forEach((item) => {
|
|
|
|
|
- item.reject(res);
|
|
|
|
|
- });
|
|
|
|
|
- } else {
|
|
|
|
|
- uni.showToast({
|
|
|
|
|
- icon: "success",
|
|
|
|
|
- title: "登录成功",
|
|
|
|
|
- });
|
|
|
|
|
- setToken(res as string);
|
|
|
|
|
- _onTokenListener.forEach((item) => {
|
|
|
|
|
- item.cb(res as string);
|
|
|
|
|
- });
|
|
|
|
|
- _tokenQueue.list.forEach((item) => {
|
|
|
|
|
- item.resolve(res as string);
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- _onTokenListener = [];
|
|
|
|
|
- _tokenQueue.list = [];
|
|
|
|
|
-};
|
|
|
|
|
|
|
+// const _tokenQueue: {
|
|
|
|
|
+// pending: boolean;
|
|
|
|
|
+// list: {
|
|
|
|
|
+// resolve: (value: string) => void;
|
|
|
|
|
+// reject: (err: any) => void;
|
|
|
|
|
+// }[];
|
|
|
|
|
+// } = {
|
|
|
|
|
+// pending: false,
|
|
|
|
|
+// list: [],
|
|
|
|
|
+// };
|
|
|
|
|
+//
|
|
|
|
|
+// let _onTokenListener: {
|
|
|
|
|
+// cb: (token: string) => void;
|
|
|
|
|
+// }[] = [];
|
|
|
|
|
+//
|
|
|
|
|
+// const _resolveTokenQueue = function (res: any) {
|
|
|
|
|
+// _tokenQueue.pending = false;
|
|
|
|
|
+// uni.hideLoading();
|
|
|
|
|
+// if (res.errMsg) {
|
|
|
|
|
+// console.log(res);
|
|
|
|
|
+// uni.showModal({
|
|
|
|
|
+// title: "登录失败",
|
|
|
|
|
+// content: `${res.errMsg}`,
|
|
|
|
|
+// });
|
|
|
|
|
+// _tokenQueue.list.forEach((item) => {
|
|
|
|
|
+// item.reject(res);
|
|
|
|
|
+// });
|
|
|
|
|
+// } else {
|
|
|
|
|
+// uni.showToast({
|
|
|
|
|
+// icon: "success",
|
|
|
|
|
+// title: "登录成功",
|
|
|
|
|
+// });
|
|
|
|
|
+// setToken(res as string);
|
|
|
|
|
+// _onTokenListener.forEach((item) => {
|
|
|
|
|
+// item.cb(res as string);
|
|
|
|
|
+// });
|
|
|
|
|
+// _tokenQueue.list.forEach((item) => {
|
|
|
|
|
+// item.resolve(res as string);
|
|
|
|
|
+// });
|
|
|
|
|
+// }
|
|
|
|
|
+// _onTokenListener = [];
|
|
|
|
|
+// _tokenQueue.list = [];
|
|
|
|
|
+// };
|
|
|
|
|
|
|
|
export function login(e: any): Promise<string> {
|
|
export function login(e: any): Promise<string> {
|
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
|
if (/deny|cancel/.test(e.errMsg)) {
|
|
if (/deny|cancel/.test(e.errMsg)) {
|
|
|
- reject();
|
|
|
|
|
|
|
+ reject(e.errMsg);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
if (!e.code) {
|
|
if (!e.code) {
|
|
|
uni.showModal({
|
|
uni.showModal({
|
|
|
title: `${e.errMsg},请重试`,
|
|
title: `${e.errMsg},请重试`,
|
|
|
});
|
|
});
|
|
|
- reject();
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- _tokenQueue.list.push({
|
|
|
|
|
- resolve,
|
|
|
|
|
- reject,
|
|
|
|
|
- });
|
|
|
|
|
- if (_tokenQueue.pending) {
|
|
|
|
|
|
|
+ reject(e);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- _tokenQueue.pending = true;
|
|
|
|
|
|
|
+ // _tokenQueue.list.push({
|
|
|
|
|
+ // resolve,
|
|
|
|
|
+ // reject,
|
|
|
|
|
+ // });
|
|
|
|
|
+ // if (_tokenQueue.pending) {
|
|
|
|
|
+ // return;
|
|
|
|
|
+ // }
|
|
|
|
|
+ // _tokenQueue.pending = true;
|
|
|
uni.showLoading({
|
|
uni.showLoading({
|
|
|
title: "登录中",
|
|
title: "登录中",
|
|
|
mask: true,
|
|
mask: true,
|
|
@@ -77,32 +77,52 @@ export function login(e: any): Promise<string> {
|
|
|
avatar: "",
|
|
avatar: "",
|
|
|
nickname: "",
|
|
nickname: "",
|
|
|
}
|
|
}
|
|
|
- body(`/user/wxLogin`, data).then((res: any) => {
|
|
|
|
|
- const {satoken, userId} = res;
|
|
|
|
|
|
|
+ body(`/user/wxLogin`, data).then(async (res: any) => {
|
|
|
|
|
+ let {satoken, userId} = res;
|
|
|
if (satoken) {
|
|
if (satoken) {
|
|
|
- _resolveTokenQueue(satoken);
|
|
|
|
|
|
|
+ getApp<any>().globalData.user.id=userId
|
|
|
|
|
+ // _resolveTokenQueue(satoken);
|
|
|
setToken(satoken);
|
|
setToken(satoken);
|
|
|
- loadUserInfo();
|
|
|
|
|
-
|
|
|
|
|
|
|
+ await loadUserInfo();
|
|
|
|
|
+ resolve(satoken)
|
|
|
|
|
+ uni.hideLoading();
|
|
|
} else {
|
|
} else {
|
|
|
- _resolveTokenQueue({
|
|
|
|
|
- errMsg: `${JSON.stringify(res)}`,
|
|
|
|
|
|
|
+ uni.showModal({
|
|
|
|
|
+ title: `登录失败`,
|
|
|
|
|
+ icon: 'none',
|
|
|
|
|
+ duration:2000
|
|
|
});
|
|
});
|
|
|
|
|
+ uni.hideLoading();
|
|
|
|
|
+ // _resolveTokenQueue({
|
|
|
|
|
+ // errMsg: `${JSON.stringify(res)}`,
|
|
|
|
|
+ // });
|
|
|
}
|
|
}
|
|
|
}).catch(e => {
|
|
}).catch(e => {
|
|
|
- _resolveTokenQueue({})
|
|
|
|
|
|
|
+ uni.showModal({
|
|
|
|
|
+ title: `${e}`,
|
|
|
|
|
+ icon: 'none',
|
|
|
|
|
+ duration:2000
|
|
|
|
|
+ });
|
|
|
|
|
+ uni.hideLoading();
|
|
|
})
|
|
})
|
|
|
},
|
|
},
|
|
|
- fail: _resolveTokenQueue,
|
|
|
|
|
|
|
+ fail: ()=>{
|
|
|
|
|
+ uni.showModal({
|
|
|
|
|
+ title: `登录失败`,
|
|
|
|
|
+ icon: 'none',
|
|
|
|
|
+ duration:2000
|
|
|
|
|
+ });
|
|
|
|
|
+ uni.hideLoading();
|
|
|
|
|
+ },
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export function tryLogin(){
|
|
|
|
|
|
|
+export function tryLogin() {
|
|
|
return new Promise(((resolve, reject) => {
|
|
return new Promise(((resolve, reject) => {
|
|
|
let ftoken = fetchToken();
|
|
let ftoken = fetchToken();
|
|
|
- console.log("ftoken",ftoken)
|
|
|
|
|
- if(ftoken){
|
|
|
|
|
|
|
+ console.log("ftoken", ftoken)
|
|
|
|
|
+ if (ftoken) {
|
|
|
resolve(ftoken)
|
|
resolve(ftoken)
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -114,11 +134,12 @@ export function tryLogin(){
|
|
|
avatar: "",
|
|
avatar: "",
|
|
|
nickname: "",
|
|
nickname: "",
|
|
|
}
|
|
}
|
|
|
- body(`/user/wxLogin`, data).then((res: any) => {
|
|
|
|
|
|
|
+ body(`/user/wxLogin`, data).then(async (res: any) => {
|
|
|
const {satoken, userId} = res;
|
|
const {satoken, userId} = res;
|
|
|
if (satoken) {
|
|
if (satoken) {
|
|
|
|
|
+ getApp<any>().globalData.user.id=userId
|
|
|
setToken(satoken);
|
|
setToken(satoken);
|
|
|
- loadUserInfo();
|
|
|
|
|
|
|
+ await loadUserInfo();
|
|
|
resolve(satoken)
|
|
resolve(satoken)
|
|
|
} else {
|
|
} else {
|
|
|
console.error("tryLogin")
|
|
console.error("tryLogin")
|
|
@@ -153,7 +174,7 @@ export function checkLogin() {
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export function loadUserInfo() {
|
|
|
|
|
|
|
+export async function loadUserInfo() {
|
|
|
getApp<any>().globalData.manualLogout = false;
|
|
getApp<any>().globalData.manualLogout = false;
|
|
|
get(`/user/me`).then((res: any) => {
|
|
get(`/user/me`).then((res: any) => {
|
|
|
getApp<any>().globalData.user = res;
|
|
getApp<any>().globalData.user = res;
|
|
@@ -203,17 +224,23 @@ export function refresh(): Promise<string> {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export function onLogin(cb: (token: string) => void) {
|
|
|
|
|
- _onTokenListener.push({
|
|
|
|
|
- cb,
|
|
|
|
|
- });
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
export function fetchToken() {
|
|
export function fetchToken() {
|
|
|
- return uni.getStorageSync("token");
|
|
|
|
|
|
|
+ let tk = uni.getStorageSync("token");
|
|
|
|
|
+ console.log(tk)
|
|
|
|
|
+ if(tk.length>16){
|
|
|
|
|
+ return tk;
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export function setToken(token: string) {
|
|
export function setToken(token: string) {
|
|
|
|
|
+ if(!token){
|
|
|
|
|
+ console.error("empty token")
|
|
|
|
|
+ return;
|
|
|
|
|
+ }else{
|
|
|
|
|
+ console.log("set token",token)
|
|
|
|
|
+ }
|
|
|
getApp<any>().globalData.token = token;
|
|
getApp<any>().globalData.token = token;
|
|
|
getApp<any>().globalData.isLogin = true;
|
|
getApp<any>().globalData.isLogin = true;
|
|
|
uni.setStorageSync("token", token)
|
|
uni.setStorageSync("token", token)
|