From 6fd062043e02ce03323ed2d8871bc8c280e32b41 Mon Sep 17 00:00:00 2001 From: WindowBird <13870814+windows-bird@user.noreply.gitee.com> Date: Tue, 26 Aug 2025 17:24:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E9=97=AD=E5=85=A8=E5=B1=80=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/request.js | 235 ++++++++++++++++++++++++----------------------- 1 file changed, 119 insertions(+), 116 deletions(-) diff --git a/utils/request.js b/utils/request.js index f15eba2..53354e3 100644 --- a/utils/request.js +++ b/utils/request.js @@ -1,68 +1,67 @@ // 统一请求工具 -import { getTempToken, shouldUseTempToken, getAppId } from '@/config/dev.js' +import { getAppId, getTempToken, shouldUseTempToken } from "@/config/dev.js"; import { - showLoading, - hideLoading, - forceHideLoading, - initGlobalLoadingManager, - config as loadingConfig, - getLoadingStatus, - setLoadingConfig, - getLoadingConfig, - showLoadingWithDelay, - hideLoadingWithDelay, AutoLoadingManager, -} from './loading-manager.js' + forceHideLoading, + getLoadingConfig, + getLoadingStatus, + hideLoading, + hideLoadingWithDelay, + initGlobalLoadingManager, + setLoadingConfig, + showLoading, + showLoadingWithDelay, +} from "./loading-manager.js"; // 环境配置 const ENV_CONFIG = { develop: { // 开发环境 // baseUrl: 'http://192.168.2.136:4501', - baseUrl: 'https://testlu.chuangtewl.com/prod-api', + baseUrl: "https://testlu.chuangtewl.com/prod-api", appId: 1, // TODO: 根据实际后端配置调整 }, trial: { // 体验版 - baseUrl: 'https://testlu.chuangtewl.com/prod-api', + baseUrl: "https://testlu.chuangtewl.com/prod-api", appId: 1, // TODO: 根据实际后端配置调整 }, release: { // 正式版 - baseUrl: 'https://testlu.chuangtewl.com/prod-api', + baseUrl: "https://testlu.chuangtewl.com/prod-api", appId: 1, // TODO: 根据实际后端配置调整 }, -} +}; // 获取当前环境配置 const getCurrentConfig = () => { try { - const { envVersion } = wx.getAccountInfoSync().miniProgram - console.log('当前环境:', envVersion) - const envConfig = ENV_CONFIG[envVersion] || ENV_CONFIG.release + const { envVersion } = wx.getAccountInfoSync().miniProgram; + console.log("当前环境:", envVersion); + const envConfig = ENV_CONFIG[envVersion] || ENV_CONFIG.release; // 确保配置对象包含所有必要属性 return { baseUrl: envConfig.baseUrl, appId: envConfig.appId || 1, // 确保appId有默认值 - } + }; } catch (error) { - console.warn('获取环境失败,默认使用正式环境:', error) - const fallbackConfig = ENV_CONFIG.release + console.warn("获取环境失败,默认使用正式环境:", error); + const fallbackConfig = ENV_CONFIG.release; return { baseUrl: fallbackConfig.baseUrl, appId: fallbackConfig.appId || 1, // 确保appId有默认值 - } + }; } -} +}; -const config = getCurrentConfig() -const BASE_URL = config.baseUrl +const config = getCurrentConfig(); +const BASE_URL = config.baseUrl; // 调试信息 -console.log('HTTP配置:', { +console.log("HTTP配置:", { baseUrl: BASE_URL, config: config, -}) +}); /** * 获取请求头 @@ -70,25 +69,25 @@ console.log('HTTP配置:', { * @returns {Object} 请求头对象 */ function getRequestHeaders(customHeader = {}) { - let token = uni.getStorageSync('token') + let token = uni.getStorageSync("token"); // 开发环境使用临时token if (shouldUseTempToken() && !token) { - token = getTempToken() + token = getTempToken(); } - let authorization = token + let authorization = token; // 平台差异化处理 // #ifdef H5 - authorization = token ? `Bearer ${token}` : '' + authorization = token ? `Bearer ${token}` : ""; // #endif return { - 'Content-Type': 'application/json;charset=UTF-8', + "Content-Type": "application/json;charset=UTF-8", Authorization: authorization, ...customHeader, - } + }; } /** @@ -99,50 +98,50 @@ function getRequestHeaders(customHeader = {}) { function handleResponseError(res, reject, options = {}) { // 先清除loading状态 if (options.showLoading !== false) { - hideLoading() + hideLoading(); } const errorMap = { 401: { - title: '登录已过期,请重新登录', + title: "登录已过期,请重新登录", action: () => { setTimeout(() => { uni.reLaunch({ - url: '/pages/login/login', - }) - }, 1500) + url: "/pages/login/login", + }); + }, 1500); }, }, 403: { - title: '权限不足', + title: "权限不足", action: () => {}, }, 404: { - title: '请求的资源不存在', + title: "请求的资源不存在", action: () => {}, }, 500: { - title: '服务器错误', + title: "服务器错误", action: () => {}, }, - } + }; const error = errorMap[res.statusCode] || { - title: res.data?.msg || '请求失败', + title: res.data?.msg || "请求失败", action: () => {}, - } + }; // 显示错误提示 uni.showToast({ title: error.title, - icon: 'none', + icon: "none", duration: 2000, - }) + }); // 执行错误处理动作 - error.action() + error.action(); - reject(new Error(error.title)) + reject(new Error(error.title)); } // Loading相关函数已从loading-manager.js导入 @@ -164,128 +163,132 @@ function handleResponseError(res, reject, options = {}) { export function request(options = {}) { return new Promise((resolve, reject) => { // 获取token,优先使用本地存储的token,如果没有则使用临时token - const localToken = uni.getStorageSync('token') - let token = localToken + const localToken = uni.getStorageSync("token"); + let token = localToken; // 如果本地没有token且启用了临时token,则使用临时token if (!token && shouldUseTempToken() && !options.noToken) { - token = getTempToken() - console.log('使用临时token进行开发测试') + token = getTempToken(); + console.log("使用临时token进行开发测试"); } // 验证URL格式 - if (!options.url || typeof options.url !== 'string') { - reject(new Error('无效的URL')) - return + if (!options.url || typeof options.url !== "string") { + reject(new Error("无效的URL")); + return; } // 确保URL以/开头 - const url = options.url.startsWith('/') ? options.url : '/' + options.url + const url = options.url.startsWith("/") ? options.url : "/" + options.url; // 构建请求配置 const requestOptions = { url: BASE_URL + url, - method: options.method || 'GET', + method: options.method || "GET", header: getRequestHeaders(options.header), timeout: options.timeout || 60000, // 默认60秒超时 - success: res => { + success: (res) => { // 隐藏加载状态 if (options.showLoading !== false) { - hideLoading() + hideLoading(); } // 请求成功处理 if (res.statusCode === 200) { - resolve(res.data) + resolve(res.data); } else { // 处理错误响应 - handleResponseError(res, reject, options) + handleResponseError(res, reject, options); } }, - fail: err => { + fail: (err) => { // 隐藏加载状态 if (options.showLoading !== false) { - hideLoading() + hideLoading(); } // 请求失败处理 - console.error('请求失败:', { + console.error("请求失败:", { error: err, url: requestOptions.url, method: requestOptions.method, baseUrl: BASE_URL, originalUrl: options.url, - }) + }); // 网络错误处理 - let errorMessage = '网络错误' + let errorMessage = "网络错误"; if (err.errMsg) { - if (err.errMsg.includes('timeout')) { - errorMessage = '请求超时' - } else if (err.errMsg.includes('fail')) { - errorMessage = '网络连接失败' + if (err.errMsg.includes("timeout")) { + errorMessage = "请求超时"; + } else if (err.errMsg.includes("fail")) { + errorMessage = "网络连接失败"; } } uni.showToast({ title: errorMessage, - icon: 'none', + icon: "none", duration: 2000, - }) + }); - reject(err) + reject(err); }, - } + }; // 特殊接口处理(不需要token的接口) - const noTokenUrls = ['/wxLogin', '/user/login'] + const noTokenUrls = ["/wxLogin", "/user/login"]; if (noTokenUrls.includes(url) || options.noToken) { - delete requestOptions.header.Authorization - console.log('跳过token验证的接口:', url) + delete requestOptions.header.Authorization; + console.log("跳过token验证的接口:", url); } // 处理请求参数 if (options.params && Object.keys(options.params).length > 0) { - requestOptions.data = { ...options.params } + requestOptions.data = { ...options.params }; } else if (options.data && Object.keys(options.data).length > 0) { - requestOptions.data = { ...options.data } + requestOptions.data = { ...options.data }; } else { // 如果既没有params也没有data,初始化为空对象 - requestOptions.data = {} + requestOptions.data = {}; } // 自动添加appId到所有请求参数中(除非明确指定不添加) try { - if (!options.noAppId && requestOptions.data && !requestOptions.data.appId) { - const appId = getCurrentAppId() - requestOptions.data.appId = appId - console.log('自动添加appId:', appId, '到请求:', requestOptions.url) + if ( + !options.noAppId && + requestOptions.data && + !requestOptions.data.appId + ) { + const appId = getCurrentAppId(); + requestOptions.data.appId = appId; + console.log("自动添加appId:", appId, "到请求:", requestOptions.url); } } catch (error) { - console.error('添加appId时出错:', error) + console.error("添加appId时出错:", error); // 确保即使出错也有appId if (requestOptions.data) { - requestOptions.data.appId = 1 + requestOptions.data.appId = 1; } } // 发起请求 - console.log('发起请求:', { + console.log("发起请求:", { url: requestOptions.url, method: requestOptions.method, header: requestOptions.header, data: requestOptions.data, timeout: requestOptions.timeout, baseUrl: BASE_URL, - }) + }); - // 显示loading(默认显示,但减少延迟) - if (options.showLoading !== false) { - showLoading(options.loadingText || loadingConfig.loadingText) - } + // // 显示loading(默认显示,但减少延迟) + // if (options.showLoading !== false) { + // showLoading(options.loadingText || loadingConfig.loadingText) + // } - uni.request(requestOptions) - }) + uni.request(requestOptions); + }); } /** @@ -298,10 +301,10 @@ export function request(options = {}) { export function get(url, params = {}, options = {}) { return request({ url, - method: 'GET', + method: "GET", params, ...options, - }) + }); } /** @@ -314,10 +317,10 @@ export function get(url, params = {}, options = {}) { export function post(url, data = {}, options = {}) { return request({ url, - method: 'POST', + method: "POST", data, ...options, - }) + }); } /** @@ -330,10 +333,10 @@ export function post(url, data = {}, options = {}) { export function put(url, data = {}, options = {}) { return request({ url, - method: 'PUT', + method: "PUT", data, ...options, - }) + }); } /** @@ -345,9 +348,9 @@ export function put(url, data = {}, options = {}) { export function del(url, options = {}) { return request({ url, - method: 'DELETE', + method: "DELETE", ...options, - }) + }); } /** @@ -356,10 +359,10 @@ export function del(url, options = {}) { */ export function getCurrentAppId() { try { - return config.appId || getAppId() || 1 + return config.appId || getAppId() || 1; } catch (error) { - console.error('获取appId失败,使用默认值:', error) - return 1 + console.error("获取appId失败,使用默认值:", error); + return 1; } } @@ -368,8 +371,8 @@ export function getCurrentAppId() { * @param {Object} newConfig - 新的配置 */ export function setRequestConfig(newConfig) { - Object.assign(config, newConfig) - console.log('更新请求配置:', config) + Object.assign(config, newConfig); + console.log("更新请求配置:", config); } /** @@ -377,15 +380,15 @@ export function setRequestConfig(newConfig) { * @returns {Object} 当前配置 */ export function getRequestConfig() { - return { ...config } + return { ...config }; } /** * 清除token */ export function clearToken() { - uni.removeStorageSync('token') - console.log('Token已清除') + uni.removeStorageSync("token"); + console.log("Token已清除"); } /** @@ -393,8 +396,8 @@ export function clearToken() { * @param {string} token - token值 */ export function setToken(token) { - uni.setStorageSync('token', token) - console.log('Token已设置') + uni.setStorageSync("token", token); + console.log("Token已设置"); } /** @@ -402,7 +405,7 @@ export function setToken(token) { * @returns {string} token值 */ export function getToken() { - return uni.getStorageSync('token') + return uni.getStorageSync("token"); } // 导出loading相关函数,作为统一入口 @@ -426,9 +429,9 @@ export { // 自动loading管理器类 AutoLoadingManager, -} +}; // Loading管理相关函数已从loading-manager.js导入 // 默认导出request函数,方便API文件导入 -export default request +export default request;