第一次提交:微信小程序前端最新完整代码
This commit is contained in:
50
utils/imAuth/imRequest.js
Normal file
50
utils/imAuth/imRequest.js
Normal file
@@ -0,0 +1,50 @@
|
||||
// utils/imAuth/imRequest.js
|
||||
// 专用于 IM 鉴权模块的请求封装
|
||||
// 与 utils/request.js 功能完全一致,但不依赖 getApp(),API 域名硬编码
|
||||
const API_BASE_URL = 'https://www.abas.asia/hqhd';
|
||||
|
||||
function imRequest(options) {
|
||||
const token = wx.getStorageSync('token');
|
||||
const header = {
|
||||
'Content-Type': 'application/json',
|
||||
...options.header,
|
||||
};
|
||||
if (token) {
|
||||
header['Authorization'] = 'Bearer ' + token;
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: API_BASE_URL + options.url,
|
||||
method: options.method,
|
||||
data: options.data || {},
|
||||
header,
|
||||
success: async (res) => {
|
||||
if (res.statusCode === 401 || res.statusCode === 403) {
|
||||
// 如果全局有登录 Promise 在等待,则等待完成后重试
|
||||
const app = getApp();
|
||||
if (app && app.globalData && app.globalData.loginPromise) {
|
||||
try {
|
||||
await app.globalData.loginPromise;
|
||||
const retryRes = await imRequest(options);
|
||||
resolve(retryRes);
|
||||
} catch (err) {
|
||||
// 重试失败,静默处理,不再弹窗
|
||||
resolve(res);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 静默处理,不弹窗,不跳转
|
||||
resolve(res);
|
||||
return;
|
||||
}
|
||||
// 正常响应
|
||||
resolve(res);
|
||||
},
|
||||
fail: reject,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = imRequest;
|
||||
Reference in New Issue
Block a user