194 lines
6.4 KiB
JavaScript
194 lines
6.4 KiB
JavaScript
"use strict";
|
||
const common_vendor = require("../common/vendor.js");
|
||
const utils_config = require("./config.js");
|
||
require("./mock.js");
|
||
class RequestOptions extends UTS.UTSType {
|
||
static get$UTSMetadata$() {
|
||
return {
|
||
kind: 2,
|
||
get fields() {
|
||
return {
|
||
url: { type: String, optional: false },
|
||
method: { type: "Unknown", optional: true },
|
||
data: { type: "Unknown", optional: true },
|
||
header: { type: "Unknown", optional: true },
|
||
showLoading: { type: Boolean, optional: true },
|
||
loadingText: { type: String, optional: true }
|
||
};
|
||
},
|
||
name: "RequestOptions"
|
||
};
|
||
}
|
||
constructor(options, metadata = RequestOptions.get$UTSMetadata$(), isJSONParse = false) {
|
||
super();
|
||
this.__props__ = UTS.UTSType.initProps(options, metadata, isJSONParse);
|
||
this.url = this.__props__.url;
|
||
this.method = this.__props__.method;
|
||
this.data = this.__props__.data;
|
||
this.header = this.__props__.header;
|
||
this.showLoading = this.__props__.showLoading;
|
||
this.loadingText = this.__props__.loadingText;
|
||
delete this.__props__;
|
||
}
|
||
}
|
||
class ResponseData extends UTS.UTSType {
|
||
static get$UTSMetadata$() {
|
||
return {
|
||
kind: 2,
|
||
get fields() {
|
||
return {
|
||
code: { type: Number, optional: false },
|
||
message: { type: String, optional: false },
|
||
data: { type: "Any", optional: true }
|
||
};
|
||
},
|
||
name: "ResponseData"
|
||
};
|
||
}
|
||
constructor(options, metadata = ResponseData.get$UTSMetadata$(), isJSONParse = false) {
|
||
super();
|
||
this.__props__ = UTS.UTSType.initProps(options, metadata, isJSONParse);
|
||
this.code = this.__props__.code;
|
||
this.message = this.__props__.message;
|
||
this.data = this.__props__.data;
|
||
delete this.__props__;
|
||
}
|
||
}
|
||
const requestInterceptor = (options) => {
|
||
const token = common_vendor.index.getStorageSync(utils_config.STORAGE_KEYS.TOKEN);
|
||
if (token != "") {
|
||
if (options.header == null) {
|
||
options.header = new UTSJSONObject({});
|
||
}
|
||
options.header["Authorization"] = `Bearer ${token}`;
|
||
}
|
||
return options;
|
||
};
|
||
const responseInterceptor = (response) => {
|
||
var _a, _b, _c, _d, _e, _f;
|
||
const statusCode = response["statusCode"];
|
||
const data = response["data"];
|
||
common_vendor.index.__f__("log", "at utils/request.uts:47", "响应拦截器 - statusCode:", statusCode);
|
||
common_vendor.index.__f__("log", "at utils/request.uts:48", "响应拦截器 - data:", data);
|
||
if (data == null) {
|
||
return new ResponseData({
|
||
code: statusCode,
|
||
message: "响应数据为空",
|
||
data: null
|
||
});
|
||
}
|
||
if (statusCode == 200) {
|
||
const code = (_a = data["code"]) !== null && _a !== void 0 ? _a : 0;
|
||
common_vendor.index.__f__("log", "at utils/request.uts:61", "响应拦截器 - 解析的 code:", code);
|
||
if (code == 0 || code == 200) {
|
||
const result = new ResponseData({
|
||
code: 0,
|
||
message: (_b = data["message"]) !== null && _b !== void 0 ? _b : "success",
|
||
data: (_c = data["data"]) !== null && _c !== void 0 ? _c : null
|
||
});
|
||
common_vendor.index.__f__("log", "at utils/request.uts:68", "响应拦截器 - 返回成功结果:", result);
|
||
return result;
|
||
} else if (code == 401) {
|
||
common_vendor.index.removeStorageSync(utils_config.STORAGE_KEYS.TOKEN);
|
||
common_vendor.index.showToast({
|
||
title: "请重新登录",
|
||
icon: "none"
|
||
});
|
||
return new ResponseData({
|
||
code,
|
||
message: (_d = data["message"]) !== null && _d !== void 0 ? _d : "请重新登录",
|
||
data: null
|
||
});
|
||
} else {
|
||
return new ResponseData({
|
||
code,
|
||
message: (_e = data["message"]) !== null && _e !== void 0 ? _e : "请求失败",
|
||
data: (_f = data["data"]) !== null && _f !== void 0 ? _f : null
|
||
});
|
||
}
|
||
} else {
|
||
return new ResponseData({
|
||
code: statusCode,
|
||
message: "网络请求失败",
|
||
data: null
|
||
});
|
||
}
|
||
};
|
||
const request = (options) => {
|
||
return new Promise((resolve, reject) => {
|
||
var _b, _c;
|
||
const finalOptions = requestInterceptor(options);
|
||
if (finalOptions.showLoading == true) {
|
||
common_vendor.index.showLoading({
|
||
title: (_b = finalOptions.loadingText) !== null && _b !== void 0 ? _b : "加载中..."
|
||
});
|
||
}
|
||
const config = utils_config.getEnvConfig();
|
||
const baseUrl = config["baseUrl"];
|
||
if (finalOptions.data && typeof finalOptions.data === "object") {
|
||
for (const key in finalOptions.data) {
|
||
if (finalOptions.data[key] === void 0) {
|
||
delete finalOptions.data[key];
|
||
}
|
||
}
|
||
}
|
||
common_vendor.index.request({
|
||
url: baseUrl + finalOptions.url,
|
||
method: (_c = finalOptions.method) !== null && _c !== void 0 ? _c : "GET",
|
||
data: finalOptions.data,
|
||
header: finalOptions.header,
|
||
success: (res) => {
|
||
if (finalOptions.showLoading == true) {
|
||
common_vendor.index.hideLoading();
|
||
}
|
||
common_vendor.index.__f__("log", "at utils/request.uts:150", "请求成功原始响应:", res);
|
||
const result = responseInterceptor(res);
|
||
common_vendor.index.__f__("log", "at utils/request.uts:152", "拦截器处理后结果:", result);
|
||
common_vendor.index.__f__("log", "at utils/request.uts:153", "result.code 类型:", typeof result.code, "值:", result.code);
|
||
if (result.code == 0) {
|
||
common_vendor.index.__f__("log", "at utils/request.uts:155", "判断为成功,resolve");
|
||
resolve(result);
|
||
} else {
|
||
common_vendor.index.__f__("log", "at utils/request.uts:158", "判断为失败,显示 toast 并 reject");
|
||
common_vendor.index.showToast({
|
||
title: result.message,
|
||
icon: "none"
|
||
});
|
||
reject(result);
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
if (finalOptions.showLoading == true) {
|
||
common_vendor.index.hideLoading();
|
||
}
|
||
common_vendor.index.showToast({
|
||
title: "网络连接失败",
|
||
icon: "none"
|
||
});
|
||
reject(new ResponseData({
|
||
code: -1,
|
||
message: "网络连接失败",
|
||
data: null
|
||
}));
|
||
}
|
||
});
|
||
});
|
||
};
|
||
const get = (url, data = null) => {
|
||
return request(new RequestOptions({
|
||
url,
|
||
method: "GET",
|
||
data
|
||
}));
|
||
};
|
||
const post = (url, data = null) => {
|
||
return request(new RequestOptions({
|
||
url,
|
||
method: "POST",
|
||
data
|
||
}));
|
||
};
|
||
exports.get = get;
|
||
exports.post = post;
|
||
//# sourceMappingURL=../../.sourcemap/mp-weixin/utils/request.js.map
|