169 lines
5.2 KiB
JavaScript
169 lines
5.2 KiB
JavaScript
import { getStorageFun, TOKEN_NAME, USER_DATA } from "./auth.js";
|
||
import { BASE_URL, Authorization } from "@/utils/config.js";
|
||
|
||
// utils/http.js (修改后的版本)
|
||
// const baseUrl = 'http://192.168.0.108:8087';
|
||
|
||
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();
|
||
}
|
||
}
|
||
|
||
function request(options) {
|
||
const {
|
||
url,
|
||
method = "GET",
|
||
data = {},
|
||
headers = {},
|
||
isShowLoading = true,
|
||
timeout = 15000,
|
||
} = options;
|
||
|
||
const token = getStorageFun(TOKEN_NAME);
|
||
let defaultHeaders = {
|
||
Authorization: Authorization,
|
||
"HSM-AUTH": token ? token : "",
|
||
"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) {
|
||
showLoading();
|
||
}
|
||
uni.request({
|
||
url: BASE_URL + url,
|
||
method,
|
||
data,
|
||
header: {
|
||
...defaultHeaders,
|
||
...headers,
|
||
},
|
||
timeout,
|
||
success: (res) => {
|
||
if (isShowLoading) {
|
||
hideLoading();
|
||
}
|
||
|
||
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;
|
||
}
|
||
if (data.status === 500) {
|
||
uni.showToast({
|
||
title: "系统异常,请稍后再试",
|
||
icon: "none", // 可选 success/loading/none
|
||
duration: 2000, // 持续时长,单位ms
|
||
mask: false, // 是否显示透明蒙层,防止触摸穿透
|
||
});
|
||
reject(new Error('系统异常,请稍后再试'));
|
||
return;
|
||
}
|
||
// 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;
|
||
let redirectParam = "";
|
||
try {
|
||
const pages = getCurrentPages();
|
||
if (pages && pages.length) {
|
||
const cur = pages[pages.length - 1];
|
||
if (cur && cur.route && !cur.route.includes("login_package/login/login")) {
|
||
let path = "/" + cur.route;
|
||
const opts = cur.options || {};
|
||
const qs = Object.keys(opts)
|
||
.map((k) => `${k}=${encodeURIComponent(opts[k])}`)
|
||
.join("&");
|
||
if (qs) path += "?" + qs;
|
||
redirectParam = "?redirect=" + encodeURIComponent(path);
|
||
}
|
||
}
|
||
} catch (e) {}
|
||
uni.navigateTo({
|
||
url: `/pages/login_package/login/login${redirectParam}`,
|
||
});
|
||
}, 2000);
|
||
}
|
||
reject(data);
|
||
} else {
|
||
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) {
|
||
hideLoading();
|
||
}
|
||
uni.showToast({
|
||
title: "服务器异常,稍后再试",
|
||
icon: "none", // 可选 success/loading/none
|
||
duration: 2000, // 持续时长,单位ms
|
||
});
|
||
reject(error);
|
||
},
|
||
});
|
||
});
|
||
}
|
||
|
||
export default request;
|