暂存code
This commit is contained in:
+36
-17
@@ -3,19 +3,23 @@ const common_vendor = require("../common/vendor.js");
|
||||
const utils_auth = require("./auth.js");
|
||||
const utils_config = require("./config.js");
|
||||
function request(options) {
|
||||
const { url, method = "GET", data = {}, headers = {} } = options;
|
||||
const { url, method = "GET", data = {}, headers = {}, isShowLoading = true } = options;
|
||||
const token = utils_auth.getStorageFun(utils_auth.TOKEN_NAME);
|
||||
const defaultHeaders = {
|
||||
// 'HSM-AUTH': token ? token : '', // 如果存在token, 则添加到请求头中
|
||||
"Authorization": utils_config.Authorization,
|
||||
"HSM-AUTH": token ? token : "",
|
||||
// 如果存在token, 则添加到请求头中
|
||||
"content-type": ["post", "POST"].includes(method) ? "application/x-www-form-urlencoded" : "application/json"
|
||||
};
|
||||
if (token) {
|
||||
defaultHeaders["HSM-AUTH"] = token;
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
common_vendor.index.showLoading({
|
||||
title: "加载中..."
|
||||
});
|
||||
if (isShowLoading) {
|
||||
common_vendor.index.showLoading({
|
||||
title: "加载中..."
|
||||
});
|
||||
}
|
||||
common_vendor.index.request({
|
||||
url: utils_config.BASE_URL + url,
|
||||
method,
|
||||
@@ -26,14 +30,31 @@ function request(options) {
|
||||
// 使用展开运算符合并默认头和自定义头
|
||||
},
|
||||
success: (res) => {
|
||||
common_vendor.index.hideLoading();
|
||||
const data2 = res.data;
|
||||
if (isShowLoading) {
|
||||
common_vendor.index.hideLoading();
|
||||
}
|
||||
let data2 = res.data;
|
||||
if (data2 && typeof data2 === "string") {
|
||||
data2 = JSON.parse(data2);
|
||||
}
|
||||
if (data2.status === 500) {
|
||||
common_vendor.index.showToast({
|
||||
title: "系统异常,请稍后再试",
|
||||
icon: "none",
|
||||
// 可选 success/loading/none
|
||||
duration: 2e3,
|
||||
// 持续时长,单位ms
|
||||
mask: false
|
||||
// 是否显示透明蒙层,防止触摸穿透
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (data2.bizcode === 100) {
|
||||
resolve(data2);
|
||||
} else if (data2.bizcode === 201) {
|
||||
if ([201, 200].includes(data2.errcode)) {
|
||||
common_vendor.index.showToast({
|
||||
title: "登录失效,请从新登录",
|
||||
title: "登录失效,请重新登录",
|
||||
icon: "none",
|
||||
// 可选 success/loading/none
|
||||
duration: 2e3,
|
||||
@@ -66,26 +87,24 @@ function request(options) {
|
||||
} else {
|
||||
common_vendor.index.showToast({
|
||||
title: data2.msg,
|
||||
icon: "error",
|
||||
icon: "none",
|
||||
// 可选 success/loading/none
|
||||
duration: 2e3,
|
||||
duration: 2e3
|
||||
// 持续时长,单位ms
|
||||
mask: false
|
||||
// 是否显示透明蒙层,防止触摸穿透
|
||||
});
|
||||
reject(data2);
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
common_vendor.index.hideLoading();
|
||||
if (isShowLoading) {
|
||||
common_vendor.index.hideLoading();
|
||||
}
|
||||
common_vendor.index.showToast({
|
||||
title: "服务器异常,稍后再试",
|
||||
icon: "error",
|
||||
icon: "none",
|
||||
// 可选 success/loading/none
|
||||
duration: 2e3,
|
||||
duration: 2e3
|
||||
// 持续时长,单位ms
|
||||
mask: false
|
||||
// 是否显示透明蒙层,防止触摸穿透
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user