"use strict"; const common_vendor = require("../../common/vendor.js"); const utils_request_http = require("./http.js"); const enums_requestEnums = require("../../enums/requestEnums.js"); const stores_user = require("../../stores/user.js"); const router_index = require("../../router/index.js"); const config_index = require("../../config/index.js"); const requestHooks = { requestInterceptorsHook(options, config) { const { urlPrefix, baseUrl, withToken } = config; options.header = options.header || {}; if (urlPrefix) { options.url = `${urlPrefix}${options.url}`; } if (baseUrl) { options.url = `${baseUrl}${options.url}`; } const token = stores_user.useUserStore().token || null; if (withToken && !options.header.token) { options.header.token = token; } options.header.version = config_index.config.version; return options; }, async responseInterceptorsHook(response, config, options) { var _a; const { isTransformResponse, isReturnDefaultResponse, isAuth } = config; if (isReturnDefaultResponse) { return response; } if (!isTransformResponse) { return response.data; } const { logout } = stores_user.useUserStore(); const { code, data, msg, show } = response.data; switch (code) { case enums_requestEnums.RequestCodeEnum.SUCCESS: msg && show && common_vendor.index.$u.toast(msg); return data; case enums_requestEnums.RequestCodeEnum.FAILED: msg && common_vendor.index.$u.toast(msg); return Promise.reject(msg); case enums_requestEnums.RequestCodeEnum.TOKEN_INVALID: logout(); if (isAuth && ((_a = options.method) == null ? void 0 : _a.toUpperCase()) !== "GET") { router_index.router.navigateTo({ path: "/pages/login/login" }); } return Promise.reject(msg); default: return data; } }, async responseInterceptorsCatchHook(options, error) { var _a; if (((_a = options.method) == null ? void 0 : _a.toUpperCase()) == enums_requestEnums.RequestMethodsEnum.POST) { common_vendor.index.$u.toast("请求失败,请重试"); } return error.errMsg || error; } }; const defaultOptions = { requestOptions: { timeout: config_index.config.timeout }, baseUrl: config_index.config.baseUrl, //是否返回默认的响应 isReturnDefaultResponse: false, // 需要对返回数据进行处理 isTransformResponse: true, // 接口拼接地址 urlPrefix: config_index.config.urlPrefix, // 忽略重复请求 ignoreCancel: false, // 是否携带token withToken: true, isAuth: false, retryCount: 2, retryTimeout: 1e3, requestHooks }; function createRequest(opt) { return new utils_request_http.HttpRequest( // 深度合并 common_vendor.merge(defaultOptions, opt || {}) ); } const request = createRequest(); exports.request = request;