|
|
@@ -20,6 +20,18 @@ switch (version) {
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
+// 添加登录锁和请求队列
|
|
|
+let isLoggingIn = false;
|
|
|
+let loginPromise = null;
|
|
|
+let pendingRequests = [];
|
|
|
+
|
|
|
+// 执行等待队列中的请求
|
|
|
+const executePendingRequests = () => {
|
|
|
+ const requests = [...pendingRequests];
|
|
|
+ pendingRequests = [];
|
|
|
+ return Promise.all(requests.map(req => req()));
|
|
|
+};
|
|
|
+
|
|
|
// 普通POST请求
|
|
|
export const post = (url, body = {}) => {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
@@ -73,7 +85,15 @@ export function uploadTrackLog(url, body) {
|
|
|
}
|
|
|
|
|
|
export const requestLogin = (data) => {
|
|
|
+ console.log('开始登录请求,参数:', data);
|
|
|
return new Promise((resolve, reject) => {
|
|
|
+ // 检查是否已经有token
|
|
|
+ const existingToken = wx.getStorageSync('accessToken');
|
|
|
+ if (existingToken) {
|
|
|
+ console.log('已存在token,跳过登录');
|
|
|
+ return resolve({ accessToken: existingToken });
|
|
|
+ }
|
|
|
+
|
|
|
wx.request({
|
|
|
url: httpServiceURL + '/user/loginWx',
|
|
|
data: data,
|
|
|
@@ -82,31 +102,31 @@ export const requestLogin = (data) => {
|
|
|
header: {},
|
|
|
success: function (res) {
|
|
|
if (res.data.code == 200) {
|
|
|
- console.log(res);
|
|
|
+ console.log('登录成功,保存token');
|
|
|
+ wx.setStorageSync('accessToken', res.data.data.accessToken);
|
|
|
resolve(res.data.data);
|
|
|
} else {
|
|
|
+ console.log('登录失败,错误信息:', res.data);
|
|
|
wx.showToast({
|
|
|
title: res.data.msg || res.data.error || '登录失败...',
|
|
|
icon: 'none',
|
|
|
});
|
|
|
- console.log('login fail', res);
|
|
|
reject(res.data);
|
|
|
}
|
|
|
},
|
|
|
fail: function (res) {
|
|
|
- console.log('login fail', res);
|
|
|
+ console.log('登录请求失败:', res);
|
|
|
wx.showToast({
|
|
|
title: '登录失败...',
|
|
|
icon: 'none',
|
|
|
});
|
|
|
+ reject(res);
|
|
|
},
|
|
|
});
|
|
|
});
|
|
|
};
|
|
|
|
|
|
export const requestAll = (requestUrl, body, method = 'POST', customHeaders = {}) => {
|
|
|
- console.log("body",body)
|
|
|
- let app = getApp().globalData;
|
|
|
let url = httpServiceURL + requestUrl;
|
|
|
if (method === 'GET') {
|
|
|
if (body) {
|
|
|
@@ -120,6 +140,9 @@ export const requestAll = (requestUrl, body, method = 'POST', customHeaders = {}
|
|
|
// 封装请求函数
|
|
|
const doRequest = () => {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
+ // 优先使用传入的token,其次使用storage中的token
|
|
|
+ const token = customHeaders.token || wx.getStorageSync('accessToken') || '';
|
|
|
+
|
|
|
wx.request({
|
|
|
url: url,
|
|
|
data: body,
|
|
|
@@ -127,12 +150,10 @@ export const requestAll = (requestUrl, body, method = 'POST', customHeaders = {}
|
|
|
dataType: 'json',
|
|
|
header: {
|
|
|
...customHeaders,
|
|
|
- token: customHeaders.token || app.token || '',
|
|
|
+ token
|
|
|
},
|
|
|
success: function (res) {
|
|
|
if (res.data.code == 200) {
|
|
|
- // 是否需要解密
|
|
|
- console.log(url, '<------>', res.data.data);
|
|
|
if (typeof res.data.data === 'number') {
|
|
|
resolve(res.data.data);
|
|
|
} else {
|
|
|
@@ -140,11 +161,9 @@ export const requestAll = (requestUrl, body, method = 'POST', customHeaders = {}
|
|
|
}
|
|
|
}
|
|
|
else if(res.data.code==20001){
|
|
|
- //令牌过期,重新登录
|
|
|
reject({code: 20001, msg: '令牌过期'});
|
|
|
}
|
|
|
else {
|
|
|
- console.log('error:', res.data, url);
|
|
|
reject(res.data);
|
|
|
}
|
|
|
},
|
|
|
@@ -161,14 +180,40 @@ export const requestAll = (requestUrl, body, method = 'POST', customHeaders = {}
|
|
|
|
|
|
// 执行请求,如果token过期则重新登录后重试
|
|
|
return doRequest().catch(async error => {
|
|
|
- if (error.code === 20001) {
|
|
|
- // 重新登录
|
|
|
- const app = getApp();
|
|
|
+ if (error.code == 20001) {
|
|
|
+ // 如果已经在登录中,将请求添加到队列
|
|
|
+ if (isLoggingIn) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ pendingRequests.push(() => doRequest().then(resolve).catch(reject));
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果没有在登录,开始登录流程
|
|
|
+ isLoggingIn = true;
|
|
|
+
|
|
|
try {
|
|
|
- await app.login();
|
|
|
- // 重新执行请求
|
|
|
+ // 使用单例登录Promise
|
|
|
+ if (!loginPromise) {
|
|
|
+ wx.removeStorageSync('accessToken');
|
|
|
+ loginPromise = getApp().login();
|
|
|
+ }
|
|
|
+ await loginPromise;
|
|
|
+
|
|
|
+ // 重置登录状态和Promise
|
|
|
+ isLoggingIn = false;
|
|
|
+ loginPromise = null;
|
|
|
+
|
|
|
+ // 执行所有请求
|
|
|
+ if (pendingRequests.length > 0) {
|
|
|
+ pendingRequests.push(() => doRequest());
|
|
|
+ return executePendingRequests();
|
|
|
+ }
|
|
|
return doRequest();
|
|
|
} catch (loginError) {
|
|
|
+ // 登录失败,重置状态
|
|
|
+ isLoggingIn = false;
|
|
|
+ loginPromise = null;
|
|
|
+ pendingRequests = [];
|
|
|
console.error('重新登录失败:', loginError);
|
|
|
throw loginError;
|
|
|
}
|
|
|
@@ -176,58 +221,3 @@ export const requestAll = (requestUrl, body, method = 'POST', customHeaders = {}
|
|
|
throw error;
|
|
|
});
|
|
|
};
|
|
|
-
|
|
|
-/**
|
|
|
- * 图文服务
|
|
|
- */
|
|
|
-export function tuwenRequest(url, body) {
|
|
|
- if (!body) {
|
|
|
- body = {};
|
|
|
- }
|
|
|
- return tuwenPost('https://kratos.mokamrp.com' + url, body);
|
|
|
-}
|
|
|
-
|
|
|
-export const tuwenPost = (url, body = {}) => {
|
|
|
- const app = getApp()
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- wx.request({
|
|
|
- url: url,
|
|
|
- data: body,
|
|
|
- method: 'POST',
|
|
|
- dataType: 'json',
|
|
|
- header: {
|
|
|
- 'Authorization': 'tanlongjiefuckdick'
|
|
|
- },
|
|
|
- success: (res) => {
|
|
|
- if (res.statusCode == 200 && res.data.code == 200) {
|
|
|
- let result = JSON.parse(res.data.data)
|
|
|
- resolve(result)
|
|
|
- } else {
|
|
|
- reject(res)
|
|
|
- }
|
|
|
- },
|
|
|
- fail: (res) => {
|
|
|
- console.log(url, 'error--------', res)
|
|
|
- reject(res)
|
|
|
- }
|
|
|
- })
|
|
|
- })
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * 投诉
|
|
|
- */
|
|
|
-export function workGet(url, body) {
|
|
|
- if (!body) {
|
|
|
- body = {}
|
|
|
- }
|
|
|
- return get('https://moka-volta-kratos-workwx-h5.mokamrp.com' + url, body)
|
|
|
-}
|
|
|
-
|
|
|
-export function workPost(url, body) {
|
|
|
- if (!body) {
|
|
|
- body = {}
|
|
|
- }
|
|
|
- return post('https://moka-volta-kratos-workwx-h5.mokamrp.com' + url, body)
|
|
|
-}
|
|
|
-
|