"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: false } }; }, 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; const statusCode = response["statusCode"]; const data = response["data"]; if (statusCode == 200) { const code = (_a = data["code"]) !== null && _a !== void 0 ? _a : 0; if (code == 0 || code == 200) { return new ResponseData({ code: 0, message: "success", data: data["data"] }); } 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: (_b = data["message"]) !== null && _b !== void 0 ? _b : "请重新登录", data: null }); } else { return new ResponseData({ code, message: (_c = data["message"]) !== null && _c !== void 0 ? _c : "请求失败", data: 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"]; 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(); } const result = responseInterceptor(res); if (result.code == 0) { resolve(result); } else { 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