"use strict"; const common_vendor = require("../common/vendor.js"); const utils_errorHandler = require("./errorHandler.js"); const BASE_URL = "https://minlong.raycos.com.cn"; const TIMEOUT = 6e4; const requestInterceptor = (config) => { const token = common_vendor.index.getStorageSync("token"); const csrfToken = common_vendor.index.getStorageSync("csrfToken"); if (!config.header) { config.header = {}; } if (token) { config.header["Authorization"] = `Bearer ${token}`; } if (csrfToken) { config.header["X-CSRF-Token"] = csrfToken; } if (!config.header["Content-Type"]) { config.header["Content-Type"] = "application/json"; } if (!config.url.startsWith("http")) { config.url = BASE_URL + config.url; } if (config.method === "POST" && config.data) { config.data._csrf = csrfToken; } return config; }; const showError = (message, duration = 2e3) => { if (showError.lastMessage === message && Date.now() - showError.lastTime < 3e3) { return; } common_vendor.index.showToast({ title: message, icon: "none", duration }); showError.lastMessage = message; showError.lastTime = Date.now(); }; showError.lastMessage = ""; showError.lastTime = 0; const responseInterceptor = (response) => { if (response.statusCode === 200) { const { data } = response; if (data.status === 999 || data.code === 999) { const error = new Error(data.message || "微信登录失败: code无效"); error.status = 999; return Promise.reject(error); } if (data.status === 2e3 || data.code === 0 || data.code === 2e3) { if (data.data) { return data.data; } if (data.openid || data.userId || data.user_id) { return data; } return data; } else { const error = utils_errorHandler.errorHandler.handleBusinessError(data); return Promise.reject(error); } } else { const error = utils_errorHandler.errorHandler.handleHttpError(response.statusCode, response); return Promise.reject(error); } }; const request = (options = {}) => { return new Promise((resolve, reject) => { options = requestInterceptor(options); if (!options.timeout) { options.timeout = TIMEOUT; } common_vendor.index.request({ ...options, success: (res) => { try { const data = responseInterceptor(res); resolve(data); } catch (error) { utils_errorHandler.errorHandler.logError(error, "request.responseInterceptor"); reject(error); } }, fail: (err) => { const error = utils_errorHandler.errorHandler.handleRequestFail(err); utils_errorHandler.errorHandler.logError(error, "request.fail"); reject(error); } }); }); }; 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 = {}, options = {}) { return new Promise((resolve, reject) => { const token = common_vendor.index.getStorageSync("token"); const header = options.header || {}; if (token) { header["Authorization"] = `Bearer ${token}`; } if (!url.startsWith("http")) { url = BASE_URL + url; } common_vendor.index.uploadFile({ url, filePath, name, formData, header, success: (res) => { try { if (typeof res.data === "string") { res.data = JSON.parse(res.data); } const data = responseInterceptor({ statusCode: res.statusCode, data: res.data }); resolve(data); } catch (error) { reject(error); } }, fail: (err) => { showError("文件上传失败"); reject(err); } }); }); } }; exports.http = http;