"use strict"; const common_vendor = require("../common/vendor.js"); const utils_config = require("./config.js"); const { baseURL } = utils_config.getConfig(); let isRedirectingToLogin = false; common_vendor.index.__isSSOHandling = false; const showToast = (message) => { var _a; if ((_a = common_vendor.index.$u) == null ? void 0 : _a.toast) { common_vendor.index.$u.toast(message); } else { common_vendor.index.showToast({ title: message, icon: "none" }); } }; common_vendor.index.__f__("log", "at utils/request.js:16", baseURL, "baseURL"); const config = { baseURL, timeout: 6e4, header: { "Content-Type": "application/json", "X-Business-System": "medical-system" }, loginPage: "/pages/login/Login", tokenExpiredCode: 401, noPermissionCode: 403, whiteList: ["/login", "/getToken", "/GiLink/getCode"] }; const isInWhiteList = (url) => { const relativeUrl = url.replace(config.baseURL, "").split("?")[0]; return config.whiteList.some((path) => { if (path.endsWith("/")) return relativeUrl.startsWith(path); return relativeUrl === path; }); }; const navigateToLogin = () => { if (isRedirectingToLogin || common_vendor.index.__isSSOHandling) { common_vendor.index.__f__("log", "at utils/request.js:42", "正在跳转登录页或SSO处理中,跳过"); return; } isRedirectingToLogin = true; const pages = getCurrentPages(); const currentPage = pages[pages.length - 1]; const isLoginPage = currentPage && currentPage.route && currentPage.route.includes("login/Login"); if (isLoginPage) { common_vendor.index.__f__("log", "at utils/request.js:56", "当前已在登录页,不跳转"); isRedirectingToLogin = false; return; } common_vendor.index.__f__("log", "at utils/request.js:61", "跳转到登录页"); setTimeout(() => { common_vendor.index.redirectTo({ url: config.loginPage + "?redirect=" + encodeURIComponent(getCurrentPagePath()), success: () => { common_vendor.index.__f__("log", "at utils/request.js:69", "跳转登录页成功"); isRedirectingToLogin = false; }, fail: () => { common_vendor.index.__f__("log", "at utils/request.js:73", "跳转登录页失败"); isRedirectingToLogin = false; } }); }, 100); }; const getCurrentPagePath = () => { var _a; const pages = getCurrentPages(); return ((_a = pages[pages.length - 1]) == null ? void 0 : _a.route) || ""; }; const checkTokenExpired = (statusCode) => { if (statusCode === config.tokenExpiredCode) { showToast("登录已过期,请重新登录"); navigateToLogin(); return true; } return false; }; const requestInterceptor = (options) => { const token = common_vendor.index.getStorageSync("token"); if (isInWhiteList(options.url)) { common_vendor.index.__f__("log", "at utils/request.js:98", "白名单接口,跳过token校验:", options.url); return options; } if (!token) { const pages = getCurrentPages(); const currentPage = pages[pages.length - 1]; const isLoginPage = currentPage && currentPage.route && (currentPage.route.includes("login/Login") || currentPage.route.includes("login/DingTalkLogin")); if (isLoginPage) { common_vendor.index.__f__("log", "at utils/request.js:112", "当前是登录页,允许无token请求"); return options; } common_vendor.index.__f__("log", "at utils/request.js:116", "未登录且不在登录页,跳转到登录页"); navigateToLogin(); throw new Error("未登录"); } options.header = { ...options.header, Authorization: `Bearer ${token}` }; return options; }; const responseInterceptor = (response) => { const { statusCode, data } = response; if (checkTokenExpired(statusCode)) return Promise.reject(data); if (statusCode === config.noPermissionCode) { showToast("无权限访问"); return Promise.reject(data); } if (statusCode !== 200) { showToast(`请求失败: ${statusCode}`); return Promise.reject(data); } if ((data == null ? void 0 : data.code) !== 200) { showToast((data == null ? void 0 : data.msg) || "操作失败"); return Promise.reject(data); } return data; }; const request = async (options) => { try { options = { ...config, ...options, url: config.baseURL + options.url, header: { ...config.header, ...options.header } }; options = await requestInterceptor(options); return new Promise((resolve, reject) => { common_vendor.index.request({ ...options, success: (res) => resolve(responseInterceptor(res)), fail: (err) => { showToast("网络错误,请重试"); reject(err); } }); }); } catch (err) { return Promise.reject(err); } }; const http = { get(url, data = {}, options = {}) { return request({ url, data, method: "GET", ...options }); }, post(url, data = {}, options = {}) { return request({ url, data, method: "POST", ...options }); }, put(url, data = {}, options = {}) { return request({ url, data, method: "PUT", ...options }); }, delete(url, data = {}, options = {}) { return request({ url, data, method: "DELETE", ...options }); }, upload(url, filePath, name = "file", formData = {}) { return request({ url, method: "POST", filePath, name, formData, header: { "Content-Type": "multipart/form-data" } }); } }; common_vendor.index.$uapi = http; //# sourceMappingURL=../../.sourcemap/mp-weixin/utils/request.js.map