45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
"use strict";
|
|||
|
|
const common_vendor = require("../common/vendor.js");
|
||
|
|
const utils_auth = require("./auth.js");
|
||
|
|
const baseUrl = "http://192.168.0.108:8087";
|
||
|
|
function request(options) {
|
||
|
|
const { url, method = "GET", data = {}, headers = {} } = options;
|
||
|
|
const token = utils_auth.getStorageFun(utils_auth.TOKEN_NAME);
|
||
|
|
const defaultHeaders = {
|
||
|
|
"HSM-AUTH": token ? token : "",
|
||
|
|
// 如果存在token, 则添加到请求头中
|
||
|
|
"content-type": ["post", "POST"].includes(method) ? "application/x-www-form-urlencoded" : "application/json"
|
||
|
|
};
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
common_vendor.index.showLoading({
|
||
|
|
title: "加载中..."
|
||
|
|
});
|
||
|
|
common_vendor.index.request({
|
||
|
|
url: baseUrl + url,
|
||
|
|
method,
|
||
|
|
data,
|
||
|
|
header: {
|
||
|
|
...defaultHeaders,
|
||
|
|
...headers
|
||
|
|
// 使用展开运算符合并默认头和自定义头
|
||
|
|
},
|
||
|
|
success: (res) => {
|
||
|
|
common_vendor.index.hideLoading();
|
||
|
|
if (res.statusCode === 200) {
|
||
|
|
resolve(res.data);
|
||
|
|
} else if (res.statusCode === 201) {
|
||
|
|
reject(new Error("Unauthorized"));
|
||
|
|
} else {
|
||
|
|
reject(res);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
fail: (error) => {
|
||
|
|
common_vendor.index.hideLoading();
|
||
|
|
reject(error);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
exports.request = request;
|
||
|
|
//# sourceMappingURL=../../.sourcemap/mp-weixin/utils/request.js.map
|