119 lines
3.7 KiB
JavaScript
119 lines
3.7 KiB
JavaScript
|
|
"use strict";
|
||
|
|
const common_vendor = require("../common/vendor.js");
|
||
|
|
const utils_authority = require("./authority.js");
|
||
|
|
const utils_config = require("./config.js");
|
||
|
|
const utils_sm2 = require("./sm2.js");
|
||
|
|
const utils_tools = require("./tools.js");
|
||
|
|
const HttpRequest = function(url, method, data) {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
const token = utils_authority.getCache("token");
|
||
|
|
let _header = {};
|
||
|
|
if (url.includes("/auth/loginByPwd") || url.includes("/auth/logout")) {
|
||
|
|
_header = {
|
||
|
|
"content-type": "application/json"
|
||
|
|
};
|
||
|
|
} else {
|
||
|
|
_header = {
|
||
|
|
"content-type": "application/json",
|
||
|
|
"Authorization": token
|
||
|
|
};
|
||
|
|
}
|
||
|
|
let endata = null;
|
||
|
|
if (method.toLowerCase() == "get") {
|
||
|
|
if (url.includes("?")) {
|
||
|
|
let nurl = url.substring(0, url.indexOf("?") + 1);
|
||
|
|
var params = utils_tools.getUrlParams(url);
|
||
|
|
for (const key in params) {
|
||
|
|
let keyData = params[key];
|
||
|
|
if (params.hasOwnProperty(key)) {
|
||
|
|
if (nurl.toLowerCase().endsWith("?")) {
|
||
|
|
nurl += key + "=" + utils_sm2.SM2.sm2Encrypt(keyData, utils_config.config.sm2PublicKey);
|
||
|
|
} else {
|
||
|
|
nurl += "&" + key + "=" + utils_sm2.SM2.sm2Encrypt(keyData, utils_config.config.sm2PublicKey);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
url = nurl;
|
||
|
|
} else {
|
||
|
|
let nurl = url;
|
||
|
|
if (data) {
|
||
|
|
if (Object.keys(data).length > 0) {
|
||
|
|
nurl = url + "?";
|
||
|
|
}
|
||
|
|
Object.keys(data).forEach((key) => {
|
||
|
|
let keyData = data[key];
|
||
|
|
if (data.hasOwnProperty(key)) {
|
||
|
|
if (nurl.toLowerCase().endsWith("?")) {
|
||
|
|
nurl += key + "=" + utils_sm2.SM2.sm2Encrypt(keyData, utils_config.config.sm2PublicKey);
|
||
|
|
} else {
|
||
|
|
nurl += "&" + key + "=" + utils_sm2.SM2.sm2Encrypt(keyData, utils_config.config.sm2PublicKey);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
url = nurl;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
if (data["content-type"]) {
|
||
|
|
if (data["content-type"] == "application/x-www-form-urlencoded") {
|
||
|
|
_header = {
|
||
|
|
"content-type": "application/x-www-form-urlencoded",
|
||
|
|
"Authorization": token
|
||
|
|
};
|
||
|
|
let formData = new Formdata();
|
||
|
|
Object.keys(data).forEach((key) => {
|
||
|
|
if (key.toLowerCase() == "content-type")
|
||
|
|
return false;
|
||
|
|
let enKeyData = utils_sm2.SM2.sm2Encrypt(data[key], utils_config.config.sm2PublicKey);
|
||
|
|
formData.append(key, enKeyData);
|
||
|
|
});
|
||
|
|
endata = formData;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
endata = utils_sm2.SM2.sm2Encrypt(JSON.stringify(data), utils_config.config.sm2PublicKey);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
common_vendor.index.request({
|
||
|
|
url,
|
||
|
|
data: endata,
|
||
|
|
method,
|
||
|
|
header: _header,
|
||
|
|
success: (res) => {
|
||
|
|
if (res.statusCode == 200) {
|
||
|
|
resolve(res.data);
|
||
|
|
} else {
|
||
|
|
reject(res);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
fail: (err) => {
|
||
|
|
reject(err);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
};
|
||
|
|
const HttpFileUpload = function(url, file) {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
common_vendor.index.uploadFile({
|
||
|
|
url,
|
||
|
|
filePath: file.path,
|
||
|
|
// 选择的文件路径
|
||
|
|
name: "multipartFile",
|
||
|
|
// 后端接收文件时的字段名,通常是 'file'
|
||
|
|
formData: {},
|
||
|
|
success: (res) => {
|
||
|
|
if (res.statusCode == 200) {
|
||
|
|
resolve(JSON.parse(res.data));
|
||
|
|
} else {
|
||
|
|
reject(res);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
fail: (err) => {
|
||
|
|
common_vendor.index.__f__("log", "at utils/request.js:114", "上传失败:", err);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
};
|
||
|
|
exports.HttpFileUpload = HttpFileUpload;
|
||
|
|
exports.HttpRequest = HttpRequest;
|
||
|
|
//# sourceMappingURL=../../.sourcemap/mp-weixin/utils/request.js.map
|