213 lines
4.9 KiB
TypeScript
Raw Normal View History

2025-05-30 15:06:49 +08:00
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";
2025-04-22 10:22:33 +08:00
let count: boolean = false;
function _loginExpiredModal() {
2025-05-30 15:06:49 +08:00
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);
},
});
}
},
});
2025-04-22 10:22:33 +08:00
}
export const config = {
2025-05-30 15:06:49 +08:00
baseUrl:
process.env.NODE_ENV == "development"
? HOMEAGENT
? "/base"
: BASE_URL
: BASE_URL,
header: {},
};
2025-04-22 10:22:33 +08:00
export const interceptor = {
2025-05-30 15:06:49 +08:00
request: (
config: UniNamespace.UploadFileOption | UniNamespace.RequestOptions
) => {
const store = useUserStore();
if (store.getToken) {
config.header = {
[AUTH_KEY]: store.getToken,
};
2025-04-22 10:22:33 +08:00
}
2025-05-30 15:06:49 +08:00
},
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) {
2025-09-24 11:34:23 +08:00
if (response.data.resultCode === -9998) {
uni.reLaunch({
url: "/pages/system/login/login",
});
}
2025-05-30 15:06:49 +08:00
return response.data;
} else {
console.log("接口无返回值", response);
}
},
};
2025-04-22 10:22:33 +08:00
2025-05-30 15:06:49 +08:00
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",
2025-07-09 22:22:34 +08:00
timeout: 30000, // 设置30秒超时
2025-05-30 15:06:49 +08:00
},
options
)
).then((res) => {
2025-07-27 21:56:11 +08:00
if (res.resultCode == 1 || res.resultCode == 0) {
2025-05-30 15:06:49 +08:00
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);
}
}
}
2025-07-09 22:22:34 +08:00
}).catch((error) => {
// 处理超时错误
if (error.errMsg && error.errMsg.includes('timeout')) {
showToast("请求超时,请检查网络连接或稍后重试");
} else {
showToast(error.message || "请求失败");
}
reject(error);
2025-05-30 15:06:49 +08:00
});
});
2025-04-22 10:22:33 +08:00
}
2025-05-30 15:06:49 +08:00
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",
2025-07-09 22:22:34 +08:00
timeout: 300000, // 设置5分钟超时
2025-05-30 15:06:49 +08:00
},
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);
}
}
}
2025-07-09 22:22:34 +08:00
}).catch((error) => {
// 处理超时错误
if (error.errMsg && error.errMsg.includes('timeout')) {
showToast("请求超时,请检查网络连接或稍后重试");
} else {
showToast(error.message || "请求失败");
}
reject(error);
2025-05-30 15:06:49 +08:00
});
});
2025-04-22 10:22:33 +08:00
}
2025-05-30 15:06:49 +08:00
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);
}
}
}
});
});
2025-04-22 10:22:33 +08:00
}