"use strict"; 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 = {}, isShowLoading = true } = options; const token = utils_auth.getStorageFun(utils_auth.TOKEN_NAME); const defaultHeaders = { "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) => { if (isShowLoading) { common_vendor.index.showLoading({ title: "加载中..." }); } common_vendor.index.request({ url: utils_config.BASE_URL + url, method, data, header: { ...defaultHeaders, ...headers // 使用展开运算符合并默认头和自定义头 }, success: (res) => { 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: "登录失效,请重新登录", icon: "none", // 可选 success/loading/none duration: 2e3, // 持续时长,单位ms mask: false, // 是否显示透明蒙层,防止触摸穿透 position: "middle" // 位置,可选 top/middle/bottom }); } else { common_vendor.index.showToast({ title: data2.msg, icon: "none", // 可选 success/loading/none duration: 2e3, // 持续时长,单位ms mask: false, // 是否显示透明蒙层,防止触摸穿透 position: "middle" // 位置,可选 top/middle/bottom }); } if ([201, 200, 205].includes(data2.errcode)) { setTimeout(() => { common_vendor.index.navigateTo({ url: "/pages/login/login" }); }, 2e3); } } else { common_vendor.index.showToast({ title: data2.msg, icon: "none", // 可选 success/loading/none duration: 2e3 // 持续时长,单位ms }); reject(data2); } }, fail: (error) => { if (isShowLoading) { common_vendor.index.hideLoading(); } common_vendor.index.showToast({ title: "服务器异常,稍后再试", icon: "none", // 可选 success/loading/none duration: 2e3 // 持续时长,单位ms }); } }); }); } exports.request = request; //# sourceMappingURL=../../.sourcemap/mp-weixin/utils/request.js.map