Files

153 lines
4.5 KiB
JavaScript
Raw Permalink Normal View History

2026-02-03 21:46:29 +08:00
import { getStorageFun, TOKEN_NAME, USER_DATA } from "./auth.js";
import { BASE_URL, Authorization } from "@/utils/config.js";
2025-08-05 15:25:23 +08:00
// utils/http.js (修改后的版本)
// const baseUrl = 'http://192.168.0.108:8087';
2026-07-24 22:06:19 +08:00
let requestCount = 0;
function showLoading() {
if (requestCount === 0) {
uni.showLoading({
title: "加载中...",
mask: false
});
}
requestCount++;
}
function hideLoading() {
if (requestCount > 0) {
requestCount--;
}
if (requestCount === 0) {
uni.hideLoading();
}
}
2025-08-05 15:25:23 +08:00
function request(options) {
2026-02-03 21:46:29 +08:00
const {
url,
method = "GET",
data = {},
headers = {},
isShowLoading = true,
timeout = 15000,
2026-02-03 21:46:29 +08:00
} = options;
2025-08-23 14:25:27 +08:00
2026-07-24 22:06:19 +08:00
const token = getStorageFun(TOKEN_NAME);
2026-02-03 21:46:29 +08:00
let defaultHeaders = {
Authorization: Authorization,
2026-07-24 22:06:19 +08:00
"HSM-AUTH": token ? token : "",
2026-02-03 21:46:29 +08:00
"content-type": ["post", "POST"].includes(method)
? "application/x-www-form-urlencoded"
: "application/json",
};
if (options.cha) {
defaultHeaders["content-type"] = "application/json";
}
if (token) {
defaultHeaders["HSM-AUTH"] = token;
}
return new Promise((resolve, reject) => {
if (isShowLoading) {
2026-07-24 22:06:19 +08:00
showLoading();
2026-02-03 21:46:29 +08:00
}
uni.request({
url: BASE_URL + url,
method,
data,
header: {
2026-02-03 21:46:29 +08:00
...defaultHeaders,
...headers,
},
timeout,
2026-02-03 21:46:29 +08:00
success: (res) => {
if (isShowLoading) {
2026-07-24 22:06:19 +08:00
hideLoading();
2026-02-03 21:46:29 +08:00
}
2025-08-06 20:25:41 +08:00
2026-02-03 21:46:29 +08:00
let data = res.data;
if (data && typeof data === "string") {
try { data = JSON.parse(data); }
catch (_) { reject(new Error('服务响应格式异常')); return; }
}
if (res.statusCode < 200 || res.statusCode >= 300 || !data) {
reject(new Error('服务请求失败,请稍后重试'));
return;
}
2026-02-03 21:46:29 +08:00
if (data.status === 500) {
uni.showToast({
title: "系统异常,请稍后再试",
icon: "none", // 可选 success/loading/none
duration: 2000, // 持续时长,单位ms
mask: false, // 是否显示透明蒙层,防止触摸穿透
});
reject(new Error('系统异常,请稍后再试'));
return;
2026-02-03 21:46:29 +08:00
}
// 601 微信登录没注册
if (data.bizcode === 100) {
resolve(data); // 成功时返回数据
} else if (data.bizcode === 201) {
if ([201, 200].includes(data.errcode)) {
uni.showToast({
title: "登录失效,请重新登录",
icon: "none", // 可选 success/loading/none
duration: 2000, // 持续时长,单位ms
mask: false, // 是否显示透明蒙层,防止触摸穿透
position: "middle", // 位置,可选 top/middle/bottom
});
} else {
uni.showToast({
title: data.msg,
icon: "none", // 可选 success/loading/none
duration: 2000, // 持续时长,单位ms
mask: false, // 是否显示透明蒙层,防止触摸穿透
position: "middle", // 位置,可选 top/middle/bottom
});
}
/**
* 205:已经有账号,返回登录页面
* 200 or 201:token失效,返回登录页面
*/
if ([201, 200, 205].includes(data.errcode)) {
uni.removeStorageSync(TOKEN_NAME);
uni.removeStorageSync(USER_DATA);
let timer = setTimeout(() => {
clearTimeout(timer);
timer = null;
uni.navigateTo({
url: "/pages/login_package/login/login",
});
}, 2000);
}
reject(data);
} else {
2026-02-03 21:46:29 +08:00
console.log("错误-1", data, url);
uni.showToast({
title: data.msg || "系统错误,请稍后再试",
icon: "none", // 可选 success/loading/none
duration: 2000, // 持续时长,单位ms
});
reject(data); // 其他错误情况返回错误信息
}
},
fail: (error) => {
console.log("错误-2", url);
if (isShowLoading) {
2026-07-24 22:06:19 +08:00
hideLoading();
2026-02-03 21:46:29 +08:00
}
uni.showToast({
title: "服务器异常,稍后再试",
icon: "none", // 可选 success/loading/none
duration: 2000, // 持续时长,单位ms
});
reject(error);
2026-02-03 21:46:29 +08:00
},
});
});
2025-08-05 15:25:23 +08:00
}
2026-02-03 21:46:29 +08:00
export default request;