1、调整接口调用的通用重试登录

2、调整登录校验openId有效性
This commit is contained in:
ywyonui 2025-09-26 13:05:02 +08:00
parent 02adf11b40
commit 8e5a894267
2 changed files with 51 additions and 5 deletions

View File

@ -160,13 +160,15 @@ export const useUserStore = defineStore({
async loginByOpenId(openId: string) { async loginByOpenId(openId: string) {
try { try {
// 先校验当前登录的用户的openid如果和传入的openid匹配直接返回true // 先校验当前登录的用户的openid如果和传入的openid匹配直接返回true
// if (this.userdata && this.userdata.openId && openId && this.userdata.openId === openId) { if (this.userdata && this.userdata.openId && this.userdata.userType === "JZ"
// return true; && openId && this.userdata.openId === openId
// } ) {
return true;
}
// 如果当前登录的用户的openid和传入的openid不匹配则调用checkOpenId接口进行校验 // 如果当前登录的用户的openid和传入的openid不匹配则调用checkOpenId接口进行校验
const res = await checkOpenId({ const res = await checkOpenId({
openId, openId,
appCode: "JS", appCode: "JZ",
}); });
if (res.resultCode == 1 && res.result) { if (res.resultCode == 1 && res.result) {
this.afterLoginAction(res.result); this.afterLoginAction(res.result);

View File

@ -62,6 +62,32 @@ export const interceptor = {
return JSON.parse(response.data); return JSON.parse(response.data);
} }
if (response.data) { if (response.data) {
if (response.data.resultCode === -9998) {
const store = useUserStore();
const userData = store.getUser || {};
const openId = userData.openId || '';
store.logout();
if (openId && openId.length) {
// 返回一个新的Promise用于重新登录并重新调用接口
return new Promise((resolve, reject) => {
store.loginByOpenId(openId).then(() => {
// 重新登录成功后,重新发送原始请求
// 使用原始请求配置,但确保包含所有必要参数
const originalConfig = response.config;
request(Object.assign({}, originalConfig)).then((newResponse: any) => {
resolve(newResponse);
}).catch((error) => {
reject(error);
});
}).catch((error) => {
// 重新登录失败,显示登录过期模态框
console.error("重新登录失败:", error);
_loginExpiredModal();
reject(error);
});
});
}
}
return response.data; return response.data;
} else { } else {
console.log("接口无返回值", response); console.log("接口无返回值", response);
@ -84,11 +110,12 @@ export function get<T = any>(
method: "GET", method: "GET",
dataType: "json", dataType: "json",
responseType: "text", responseType: "text",
timeout: 30000, // 设置30秒超时
}, },
options options
) )
).then((res) => { ).then((res) => {
if (res.resultCode == 1) { if (res.resultCode == 1 || res.resultCode == 0) {
resolve(res); resolve(res);
} else { } else {
if (res.resultCode) { if (res.resultCode) {
@ -105,6 +132,14 @@ export function get<T = any>(
} }
} }
} }
}).catch((error) => {
// 处理超时错误
if (error.errMsg && error.errMsg.includes('timeout')) {
showToast("请求超时,请检查网络连接或稍后重试");
} else {
showToast(error.message || "请求失败");
}
reject(error);
}); });
}); });
} }
@ -124,6 +159,7 @@ export function post<T = any>(
method: "POST", method: "POST",
dataType: "json", dataType: "json",
responseType: "text", responseType: "text",
timeout: 300000, // 设置5分钟超时
}, },
options options
) )
@ -145,6 +181,14 @@ export function post<T = any>(
} }
} }
} }
}).catch((error) => {
// 处理超时错误
if (error.errMsg && error.errMsg.includes('timeout')) {
showToast("请求超时,请检查网络连接或稍后重试");
} else {
showToast(error.message || "请求失败");
}
reject(error);
}); });
}); });
} }