var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { ResponseError: () => ResponseError, User: () => User, default: () => src_default, doRequest: () => doRequest, req: () => req }); module.exports = __toCommonJS(src_exports); var import_umi_request = require("umi-request"); var import_lodash_es = require("lodash-es"); var import_antd = require("antd"); var import_base_64 = __toESM(require("base-64")); var User = /* @__PURE__ */ ((User2) => { User2["TOKEN_KEY"] = "USER-TOKEN"; User2["USER_KEY"] = "USER"; return User2; })(User || {}); var ResponseError = class extends Error { constructor(response, text, data, request, type = "ResponseError") { super(text || response.statusText); this.name = "ResponseError"; this.data = data; this.response = response; this.request = request; this.type = type; } }; var req = (0, import_umi_request.extend)({ getResponse: true, // timeout: 1000, parseResponse: false }); req.use(async (ctx, next) => { const { req: req2 } = ctx; const { toast = true } = (req2 == null ? void 0 : req2.options) || {}; try { await next(); const { res } = ctx; const d = await res.text(); if (res.status === 401) { localStorage.removeItem("USER-TOKEN" /* TOKEN_KEY */); localStorage.removeItem("USER" /* USER_KEY */); import_antd.message.warning("登录过期,请重新登录!"); return; } const isEmptyRes = d === ""; if (!res) return; const body = !isEmptyRes ? JSON.parse(d) : d; if (res.status >= 200 && res.status < 300) { ctx.res = body; } else { let errMsg = res.headers.get("Grpc-Metadata-Errorx-Message"); if (errMsg) { errMsg = import_base_64.default.decode(errMsg); } else if (!errMsg && (0, import_lodash_es.get)(body, "message")) { errMsg = `${(0, import_lodash_es.get)(body, "message")}`; } else { errMsg = "您的网络发生异常,无法连接服务器"; } toast && import_antd.message.error(errMsg); throw new ResponseError(res, errMsg, d, req2, "CustomError"); } } catch (error) { if ((0, import_lodash_es.get)(error, "type") !== "CustomError") { toast && import_antd.message.error("您的网络发生异常,无法连接服务器"); } throw error; } }); var doRequest = (cgi, option) => { const { method, url, baseUrl, data = {}, useBaseUrl = true, originUrl = false, refererSuffix = "" } = cgi; const token = localStorage.getItem("USER-TOKEN" /* TOKEN_KEY */); let newUrl = ""; if (useBaseUrl) { newUrl = `${baseUrl}${url}`; } else { newUrl = `http://10.0.0.7:32223${url}`; } if (originUrl) { newUrl = url; } const regex = /\/:(\w+)/g; const params = []; let matches; while ((matches = regex.exec(newUrl)) != null) { if (matches[1]) { params.push(matches[1]); } } params.forEach(function(name) { let d = data == null ? void 0 : data[name]; if (d == null) { d = ""; } newUrl = newUrl.replace(`:${name}`, d); }); const newData = (0, import_lodash_es.omit)(data, params); const paramObj = method.toLowerCase() === "get" ? { params: newData } : { data: newData }; return req(newUrl, { method, ...paramObj, ...option, headers: { authorization: token, ...refererSuffix ? { zhst_referer: `${baseUrl}${refererSuffix}` } : {} } }); }; var src_default = doRequest; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ResponseError, User, doRequest, req });