234 lines
5.9 KiB
TypeScript
234 lines
5.9 KiB
TypeScript
import { AUTH_KEY, BASE_URL, HOMEAGENT, RESULT_CODE_NOT_LOGIN } from "@/config";
|
||
import { useUserStore } from "@/store/modules/user";
|
||
import { getLogin } from "../permission";
|
||
import { request } from "@/utils/request/request";
|
||
import { showToast } from "@/utils/uniapp";
|
||
|
||
let count: boolean = false;
|
||
|
||
function _loginExpiredModal() {
|
||
const store = useUserStore();
|
||
store.logout();
|
||
uni.showModal({
|
||
title: "提示",
|
||
content: "登录过期,请重新登录",
|
||
showCancel: false,
|
||
success: function (res) {
|
||
if (res.confirm) {
|
||
count = false;
|
||
getLogin();
|
||
} else if (res.cancel) {
|
||
count = false;
|
||
uni.navigateBack({
|
||
delta: 1,
|
||
fail: (err) => {
|
||
console.log(err);
|
||
},
|
||
});
|
||
}
|
||
},
|
||
});
|
||
}
|
||
|
||
export const config = {
|
||
baseUrl:
|
||
process.env.NODE_ENV == "development"
|
||
? HOMEAGENT
|
||
? "/base"
|
||
: BASE_URL
|
||
: BASE_URL,
|
||
header: {},
|
||
};
|
||
export const interceptor = {
|
||
request: (
|
||
config: UniNamespace.UploadFileOption | UniNamespace.RequestOptions
|
||
) => {
|
||
const store = useUserStore();
|
||
if (store.getToken) {
|
||
config.header = {
|
||
[AUTH_KEY]: store.getToken,
|
||
};
|
||
}
|
||
},
|
||
response: (response: any) => {
|
||
if (response.data && RESULT_CODE_NOT_LOGIN == response.data.resultCode) {
|
||
if (!count) {
|
||
_loginExpiredModal();
|
||
}
|
||
count = true;
|
||
return;
|
||
}
|
||
if (response.config.name === "files") {
|
||
return JSON.parse(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;
|
||
} else {
|
||
console.log("接口无返回值", response);
|
||
}
|
||
},
|
||
};
|
||
|
||
export function get<T = any>(
|
||
url: string,
|
||
data?: any,
|
||
options?: UniNamespace.RequestOptions
|
||
): Promise<Requests<T>> {
|
||
return new Promise((resolve, reject) => {
|
||
request(
|
||
Object.assign(
|
||
{},
|
||
{
|
||
url,
|
||
data,
|
||
method: "GET",
|
||
dataType: "json",
|
||
responseType: "text",
|
||
timeout: 1200000, // 设置20分钟超时(PDF生成需要较长时间)
|
||
},
|
||
options
|
||
)
|
||
).then((res) => {
|
||
if (res.resultCode == 1 || res.resultCode == 0) {
|
||
resolve(res);
|
||
} else {
|
||
if (res.resultCode) {
|
||
if (res.resultCode != RESULT_CODE_NOT_LOGIN) {
|
||
showToast(res.message || "接口异常");
|
||
reject(res);
|
||
}
|
||
} else {
|
||
if (res.rows) {
|
||
resolve(res);
|
||
} else {
|
||
showToast(res.message || "接口异常");
|
||
reject(res);
|
||
}
|
||
}
|
||
}
|
||
}).catch((error) => {
|
||
// 处理超时错误
|
||
if (error.errMsg && error.errMsg.includes('timeout')) {
|
||
showToast("请求超时,请检查网络连接或稍后重试");
|
||
} else {
|
||
showToast(error.message || "请求失败");
|
||
}
|
||
reject(error);
|
||
});
|
||
});
|
||
}
|
||
|
||
export function post<T = any>(
|
||
url: string,
|
||
data?: any,
|
||
options?: UniNamespace.RequestOptions
|
||
): Promise<Requests<T>> {
|
||
return new Promise((resolve, reject) => {
|
||
request(
|
||
Object.assign(
|
||
{},
|
||
{
|
||
url,
|
||
data,
|
||
method: "POST",
|
||
dataType: "json",
|
||
responseType: "text",
|
||
timeout: 1200000, // 设置20分钟超时(PDF生成需要较长时间)
|
||
},
|
||
options
|
||
)
|
||
).then((res) => {
|
||
if (res.resultCode == 1) {
|
||
resolve(res);
|
||
} else {
|
||
if (res.resultCode) {
|
||
if (res.resultCode != RESULT_CODE_NOT_LOGIN) {
|
||
showToast(res.message || "接口异常");
|
||
reject(res);
|
||
}
|
||
} else {
|
||
if (res.rows) {
|
||
resolve(res);
|
||
} else {
|
||
showToast(res.message || "接口异常");
|
||
reject(res);
|
||
}
|
||
}
|
||
}
|
||
}).catch((error) => {
|
||
// 处理超时错误
|
||
if (error.errMsg && error.errMsg.includes('timeout')) {
|
||
showToast("请求超时,请检查网络连接或稍后重试");
|
||
} else {
|
||
showToast(error.message || "请求失败");
|
||
}
|
||
reject(error);
|
||
});
|
||
});
|
||
}
|
||
|
||
export function file(
|
||
url: string,
|
||
data: Blob,
|
||
options?: UniNamespace.UploadFileOption
|
||
) {
|
||
return new Promise((resolve, reject) => {
|
||
request(
|
||
Object.assign(
|
||
{},
|
||
{
|
||
url,
|
||
filePath: data,
|
||
name: "files",
|
||
},
|
||
options
|
||
),
|
||
true
|
||
).then((res) => {
|
||
if (res.resultCode == 1) {
|
||
resolve(res);
|
||
} else {
|
||
if (res.resultCode) {
|
||
if (res.resultCode != RESULT_CODE_NOT_LOGIN) {
|
||
showToast(res.message || "接口异常");
|
||
reject(res);
|
||
}
|
||
} else {
|
||
if (res.rows) {
|
||
resolve(res);
|
||
} else {
|
||
showToast(res.message || "接口异常");
|
||
reject(res);
|
||
}
|
||
}
|
||
}
|
||
});
|
||
});
|
||
}
|