2025-07-29 11:07:32 +08:00
|
|
|
|
// 统一请求工具
|
2025-10-15 17:39:37 +08:00
|
|
|
|
import { getAppId } from "@/config/dev.js";
|
2025-09-22 10:56:01 +08:00
|
|
|
|
import debounce from "uview-ui/libs/function/debounce";
|
2025-07-29 11:07:32 +08:00
|
|
|
|
|
2025-08-02 10:35:11 +08:00
|
|
|
|
// 环境配置
|
|
|
|
|
|
const ENV_CONFIG = {
|
2025-08-14 11:22:53 +08:00
|
|
|
|
release: {
|
|
|
|
|
|
// 正式版
|
2025-11-25 14:46:15 +08:00
|
|
|
|
// baseUrl: "http://192.168.1.2:4501",
|
2025-11-20 16:37:14 +08:00
|
|
|
|
baseUrl: "https://tech-ape.top/prod-api",
|
2025-09-23 10:15:59 +08:00
|
|
|
|
// baseUrl: "https://tech-ape.top/prod-api",
|
2025-08-27 11:08:54 +08:00
|
|
|
|
appId: 1,
|
2025-08-14 11:22:53 +08:00
|
|
|
|
},
|
2025-08-26 17:24:06 +08:00
|
|
|
|
};
|
2025-08-02 10:35:11 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取当前环境配置
|
|
|
|
|
|
const getCurrentConfig = () => {
|
2025-09-22 17:15:48 +08:00
|
|
|
|
// try {
|
|
|
|
|
|
// // 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;
|
|
|
|
|
|
return {
|
|
|
|
|
|
baseUrl: fallbackConfig.baseUrl,
|
|
|
|
|
|
appId: fallbackConfig.appId || 1, // 确保appId有默认值
|
|
|
|
|
|
};
|
|
|
|
|
|
// }
|
2025-08-26 17:24:06 +08:00
|
|
|
|
};
|
2025-08-02 10:35:11 +08:00
|
|
|
|
|
2025-08-26 17:24:06 +08:00
|
|
|
|
const config = getCurrentConfig();
|
|
|
|
|
|
const BASE_URL = config.baseUrl;
|
2025-08-02 10:35:11 +08:00
|
|
|
|
|
|
|
|
|
|
// 调试信息
|
2025-08-26 17:24:06 +08:00
|
|
|
|
console.log("HTTP配置:", {
|
2025-08-02 10:35:11 +08:00
|
|
|
|
baseUrl: BASE_URL,
|
2025-08-14 11:22:53 +08:00
|
|
|
|
config: config,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
});
|
2025-08-02 10:35:11 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取请求头
|
|
|
|
|
|
* @param {Object} customHeader - 自定义请求头
|
|
|
|
|
|
* @returns {Object} 请求头对象
|
|
|
|
|
|
*/
|
|
|
|
|
|
function getRequestHeaders(customHeader = {}) {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
let token = uni.getStorageSync("token");
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-26 17:24:06 +08:00
|
|
|
|
let authorization = token;
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-02 10:35:11 +08:00
|
|
|
|
// 平台差异化处理
|
|
|
|
|
|
// #ifdef H5
|
2025-08-26 17:24:06 +08:00
|
|
|
|
authorization = token ? `Bearer ${token}` : "";
|
2025-08-02 10:35:11 +08:00
|
|
|
|
// #endif
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-02 10:35:11 +08:00
|
|
|
|
return {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
"Content-Type": "application/json;charset=UTF-8",
|
2025-08-14 11:22:53 +08:00
|
|
|
|
Authorization: authorization,
|
|
|
|
|
|
...customHeader,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
};
|
2025-08-02 10:35:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 处理响应错误
|
|
|
|
|
|
* @param {Object} res - 响应对象
|
|
|
|
|
|
* @param {Function} reject - Promise reject函数
|
|
|
|
|
|
*/
|
|
|
|
|
|
function handleResponseError(res, reject, options = {}) {
|
|
|
|
|
|
// 先清除loading状态
|
|
|
|
|
|
if (options.showLoading !== false) {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
hideLoading();
|
2025-08-02 10:35:11 +08:00
|
|
|
|
}
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-02 10:35:11 +08:00
|
|
|
|
const errorMap = {
|
|
|
|
|
|
401: {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
title: "登录已过期,请重新登录",
|
2025-08-02 10:35:11 +08:00
|
|
|
|
action: () => {
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
uni.reLaunch({
|
2025-08-26 17:24:06 +08:00
|
|
|
|
url: "/pages/login/login",
|
|
|
|
|
|
});
|
|
|
|
|
|
}, 1500);
|
2025-08-14 11:22:53 +08:00
|
|
|
|
},
|
2025-08-02 10:35:11 +08:00
|
|
|
|
},
|
|
|
|
|
|
403: {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
title: "权限不足",
|
2025-08-14 11:22:53 +08:00
|
|
|
|
action: () => {},
|
2025-08-02 10:35:11 +08:00
|
|
|
|
},
|
|
|
|
|
|
404: {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
title: "请求的资源不存在",
|
2025-08-14 11:22:53 +08:00
|
|
|
|
action: () => {},
|
2025-08-02 10:35:11 +08:00
|
|
|
|
},
|
2025-09-16 16:20:50 +08:00
|
|
|
|
500: {
|
|
|
|
|
|
title: "服务器错误",
|
|
|
|
|
|
action: () => {},
|
|
|
|
|
|
},
|
2025-08-26 17:24:06 +08:00
|
|
|
|
};
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-09-16 16:20:50 +08:00
|
|
|
|
const error = errorMap[res.statusCode] || {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
title: res.data?.msg || "请求失败",
|
2025-08-14 11:22:53 +08:00
|
|
|
|
action: () => {},
|
2025-08-26 17:24:06 +08:00
|
|
|
|
};
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-02 10:35:11 +08:00
|
|
|
|
// 显示错误提示
|
|
|
|
|
|
uni.showToast({
|
|
|
|
|
|
title: error.title,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
icon: "none",
|
2025-08-14 11:22:53 +08:00
|
|
|
|
duration: 2000,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
});
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-02 10:35:11 +08:00
|
|
|
|
// 执行错误处理动作
|
2025-08-26 17:24:06 +08:00
|
|
|
|
error.action();
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-26 17:24:06 +08:00
|
|
|
|
reject(new Error(error.title));
|
2025-08-02 10:35:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-02 14:46:43 +08:00
|
|
|
|
// Loading相关函数已从loading-manager.js导入
|
2025-07-29 11:07:32 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 统一请求方法
|
|
|
|
|
|
* @param {Object} options - 请求配置
|
|
|
|
|
|
* @param {string} options.url - 请求地址
|
|
|
|
|
|
* @param {string} options.method - 请求方法
|
|
|
|
|
|
* @param {Object} options.params - 查询参数
|
|
|
|
|
|
* @param {Object} options.data - 请求体数据
|
|
|
|
|
|
* @param {Object} options.header - 请求头
|
2025-08-02 10:35:11 +08:00
|
|
|
|
* @param {number} options.timeout - 超时时间
|
2025-08-02 14:03:12 +08:00
|
|
|
|
* @param {boolean} options.showLoading - 是否显示加载状态(默认true)
|
2025-08-02 10:35:11 +08:00
|
|
|
|
* @param {string} options.loadingText - 加载提示文字
|
|
|
|
|
|
* @param {boolean} options.noToken - 是否需要token
|
2025-07-29 11:07:32 +08:00
|
|
|
|
* @returns {Promise} 返回请求结果
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function request(options = {}) {
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
// 获取token,优先使用本地存储的token,如果没有则使用临时token
|
2025-10-14 14:06:31 +08:00
|
|
|
|
|
|
|
|
|
|
let token = uni.getStorageSync("token");
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-02 10:35:11 +08:00
|
|
|
|
// 验证URL格式
|
2025-08-26 17:24:06 +08:00
|
|
|
|
if (!options.url || typeof options.url !== "string") {
|
|
|
|
|
|
reject(new Error("无效的URL"));
|
|
|
|
|
|
return;
|
2025-08-02 10:35:11 +08:00
|
|
|
|
}
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-02 10:35:11 +08:00
|
|
|
|
// 确保URL以/开头
|
2025-08-26 17:24:06 +08:00
|
|
|
|
const url = options.url.startsWith("/") ? options.url : "/" + options.url;
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-07-29 11:07:32 +08:00
|
|
|
|
// 构建请求配置
|
|
|
|
|
|
const requestOptions = {
|
2025-08-02 10:35:11 +08:00
|
|
|
|
url: BASE_URL + url,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
method: options.method || "GET",
|
2025-08-02 10:35:11 +08:00
|
|
|
|
header: getRequestHeaders(options.header),
|
2025-08-01 17:53:56 +08:00
|
|
|
|
timeout: options.timeout || 60000, // 默认60秒超时
|
2025-08-26 17:24:06 +08:00
|
|
|
|
success: (res) => {
|
2025-08-02 10:35:11 +08:00
|
|
|
|
// 隐藏加载状态
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-09-22 10:56:01 +08:00
|
|
|
|
console.log("@@@@@@@@@@@@@", res);
|
2025-09-16 16:20:50 +08:00
|
|
|
|
if (res.statusCode === 200) {
|
2025-09-22 09:51:12 +08:00
|
|
|
|
if (res.data.code === 401) {
|
2025-09-22 10:56:01 +08:00
|
|
|
|
Expired();
|
2025-09-22 09:51:12 +08:00
|
|
|
|
}
|
2025-08-26 17:24:06 +08:00
|
|
|
|
resolve(res.data);
|
2025-07-29 11:07:32 +08:00
|
|
|
|
} else {
|
2025-08-02 10:35:11 +08:00
|
|
|
|
// 处理错误响应
|
2025-08-26 17:24:06 +08:00
|
|
|
|
handleResponseError(res, reject, options);
|
2025-07-29 11:07:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
2025-08-26 17:24:06 +08:00
|
|
|
|
fail: (err) => {
|
2025-08-02 10:35:11 +08:00
|
|
|
|
// 隐藏加载状态
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-07-29 11:07:32 +08:00
|
|
|
|
// 请求失败处理
|
2025-08-26 17:24:06 +08:00
|
|
|
|
console.error("请求失败:", {
|
2025-08-02 10:35:11 +08:00
|
|
|
|
error: err,
|
|
|
|
|
|
url: requestOptions.url,
|
|
|
|
|
|
method: requestOptions.method,
|
|
|
|
|
|
baseUrl: BASE_URL,
|
2025-08-14 11:22:53 +08:00
|
|
|
|
originalUrl: options.url,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
});
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-02 14:03:12 +08:00
|
|
|
|
// 网络错误处理
|
2025-08-26 17:24:06 +08:00
|
|
|
|
let errorMessage = "网络错误";
|
2025-08-02 14:03:12 +08:00
|
|
|
|
if (err.errMsg) {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
if (err.errMsg.includes("timeout")) {
|
|
|
|
|
|
errorMessage = "请求超时";
|
|
|
|
|
|
} else if (err.errMsg.includes("fail")) {
|
|
|
|
|
|
errorMessage = "网络连接失败";
|
2025-08-02 14:03:12 +08:00
|
|
|
|
}
|
2025-08-02 10:35:11 +08:00
|
|
|
|
}
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-07-29 11:07:32 +08:00
|
|
|
|
uni.showToast({
|
2025-08-02 10:35:11 +08:00
|
|
|
|
title: errorMessage,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
icon: "none",
|
2025-08-14 11:22:53 +08:00
|
|
|
|
duration: 2000,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
});
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-26 17:24:06 +08:00
|
|
|
|
reject(err);
|
2025-08-14 11:22:53 +08:00
|
|
|
|
},
|
2025-08-26 17:24:06 +08:00
|
|
|
|
};
|
2025-09-22 10:56:01 +08:00
|
|
|
|
const Expired = () => {
|
|
|
|
|
|
debounce(
|
|
|
|
|
|
function () {
|
|
|
|
|
|
uni.showModal({
|
|
|
|
|
|
title: "登录已过期",
|
|
|
|
|
|
content: "请先登录后再进行操作",
|
|
|
|
|
|
confirmText: "去登录",
|
|
|
|
|
|
cancelText: "取消",
|
|
|
|
|
|
success: (res) => {
|
|
|
|
|
|
if (res.confirm) {
|
|
|
|
|
|
uni.navigateTo({ url: "/pages/login/login" });
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
},
|
|
|
|
|
|
1000,
|
|
|
|
|
|
true,
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-02 14:03:12 +08:00
|
|
|
|
// 特殊接口处理(不需要token的接口)
|
2025-08-26 17:24:06 +08:00
|
|
|
|
const noTokenUrls = ["/wxLogin", "/user/login"];
|
2025-08-02 14:03:12 +08:00
|
|
|
|
if (noTokenUrls.includes(url) || options.noToken) {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
delete requestOptions.header.Authorization;
|
|
|
|
|
|
console.log("跳过token验证的接口:", url);
|
2025-07-29 11:07:32 +08:00
|
|
|
|
}
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-02 14:03:12 +08:00
|
|
|
|
// 处理请求参数
|
|
|
|
|
|
if (options.params && Object.keys(options.params).length > 0) {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
requestOptions.data = { ...options.params };
|
2025-08-06 11:46:08 +08:00
|
|
|
|
} else if (options.data && Object.keys(options.data).length > 0) {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
requestOptions.data = { ...options.data };
|
2025-08-06 11:46:08 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 如果既没有params也没有data,初始化为空对象
|
2025-08-26 17:24:06 +08:00
|
|
|
|
requestOptions.data = {};
|
2025-07-29 11:07:32 +08:00
|
|
|
|
}
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-06 11:46:08 +08:00
|
|
|
|
// 自动添加appId到所有请求参数中(除非明确指定不添加)
|
|
|
|
|
|
try {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
if (
|
|
|
|
|
|
!options.noAppId &&
|
|
|
|
|
|
requestOptions.data &&
|
|
|
|
|
|
!requestOptions.data.appId
|
|
|
|
|
|
) {
|
|
|
|
|
|
const appId = getCurrentAppId();
|
|
|
|
|
|
requestOptions.data.appId = appId;
|
|
|
|
|
|
console.log("自动添加appId:", appId, "到请求:", requestOptions.url);
|
2025-08-06 11:46:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
console.error("添加appId时出错:", error);
|
2025-08-06 11:46:08 +08:00
|
|
|
|
// 确保即使出错也有appId
|
|
|
|
|
|
if (requestOptions.data) {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
requestOptions.data.appId = 1;
|
2025-08-06 11:46:08 +08:00
|
|
|
|
}
|
2025-07-29 11:07:32 +08:00
|
|
|
|
}
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-07-29 11:07:32 +08:00
|
|
|
|
// 发起请求
|
2025-08-26 17:24:06 +08:00
|
|
|
|
console.log("发起请求:", {
|
2025-08-02 10:35:11 +08:00
|
|
|
|
url: requestOptions.url,
|
|
|
|
|
|
method: requestOptions.method,
|
|
|
|
|
|
header: requestOptions.header,
|
|
|
|
|
|
data: requestOptions.data,
|
|
|
|
|
|
timeout: requestOptions.timeout,
|
2025-08-14 11:22:53 +08:00
|
|
|
|
baseUrl: BASE_URL,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
});
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-26 17:24:06 +08:00
|
|
|
|
// // 显示loading(默认显示,但减少延迟)
|
|
|
|
|
|
// if (options.showLoading !== false) {
|
|
|
|
|
|
// showLoading(options.loadingText || loadingConfig.loadingText)
|
|
|
|
|
|
// }
|
2025-08-14 11:22:53 +08:00
|
|
|
|
|
2025-08-26 17:24:06 +08:00
|
|
|
|
uni.request(requestOptions);
|
|
|
|
|
|
});
|
2025-07-29 11:07:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* GET请求
|
|
|
|
|
|
* @param {string} url - 请求地址
|
|
|
|
|
|
* @param {Object} params - 查询参数
|
2025-08-02 14:03:12 +08:00
|
|
|
|
* @param {Object} options - 请求配置
|
2025-07-29 11:07:32 +08:00
|
|
|
|
* @returns {Promise} 返回请求结果
|
|
|
|
|
|
*/
|
2025-08-02 10:35:11 +08:00
|
|
|
|
export function get(url, params = {}, options = {}) {
|
2025-07-29 11:07:32 +08:00
|
|
|
|
return request({
|
|
|
|
|
|
url,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
method: "GET",
|
2025-07-29 11:07:32 +08:00
|
|
|
|
params,
|
2025-08-14 11:22:53 +08:00
|
|
|
|
...options,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
});
|
2025-07-29 11:07:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* POST请求
|
|
|
|
|
|
* @param {string} url - 请求地址
|
|
|
|
|
|
* @param {Object} data - 请求体数据
|
2025-08-02 14:03:12 +08:00
|
|
|
|
* @param {Object} options - 请求配置
|
2025-07-29 11:07:32 +08:00
|
|
|
|
* @returns {Promise} 返回请求结果
|
|
|
|
|
|
*/
|
2025-08-02 10:35:11 +08:00
|
|
|
|
export function post(url, data = {}, options = {}) {
|
2025-07-29 11:07:32 +08:00
|
|
|
|
return request({
|
|
|
|
|
|
url,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
method: "POST",
|
2025-07-29 11:07:32 +08:00
|
|
|
|
data,
|
2025-08-14 11:22:53 +08:00
|
|
|
|
...options,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
});
|
2025-07-29 11:07:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* PUT请求
|
|
|
|
|
|
* @param {string} url - 请求地址
|
|
|
|
|
|
* @param {Object} data - 请求体数据
|
2025-08-02 14:03:12 +08:00
|
|
|
|
* @param {Object} options - 请求配置
|
2025-07-29 11:07:32 +08:00
|
|
|
|
* @returns {Promise} 返回请求结果
|
|
|
|
|
|
*/
|
2025-08-02 10:35:11 +08:00
|
|
|
|
export function put(url, data = {}, options = {}) {
|
2025-07-29 11:07:32 +08:00
|
|
|
|
return request({
|
|
|
|
|
|
url,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
method: "PUT",
|
2025-07-29 11:07:32 +08:00
|
|
|
|
data,
|
2025-08-14 11:22:53 +08:00
|
|
|
|
...options,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
});
|
2025-07-29 11:07:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* DELETE请求
|
|
|
|
|
|
* @param {string} url - 请求地址
|
2025-08-02 14:03:12 +08:00
|
|
|
|
* @param {Object} options - 请求配置
|
2025-07-29 11:07:32 +08:00
|
|
|
|
* @returns {Promise} 返回请求结果
|
|
|
|
|
|
*/
|
2025-08-02 10:35:11 +08:00
|
|
|
|
export function del(url, options = {}) {
|
2025-07-29 11:07:32 +08:00
|
|
|
|
return request({
|
|
|
|
|
|
url,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
method: "DELETE",
|
2025-08-14 11:22:53 +08:00
|
|
|
|
...options,
|
2025-08-26 17:24:06 +08:00
|
|
|
|
});
|
2025-08-02 10:35:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-06 11:46:08 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取当前环境的appId
|
|
|
|
|
|
* @returns {number} 当前环境的appId
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function getCurrentAppId() {
|
|
|
|
|
|
try {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
return config.appId || getAppId() || 1;
|
2025-08-06 11:46:08 +08:00
|
|
|
|
} catch (error) {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
console.error("获取appId失败,使用默认值:", error);
|
|
|
|
|
|
return 1;
|
2025-08-06 11:46:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-02 10:35:11 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 设置请求配置
|
2025-08-02 14:03:12 +08:00
|
|
|
|
* @param {Object} newConfig - 新的配置
|
2025-08-02 10:35:11 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export function setRequestConfig(newConfig) {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
Object.assign(config, newConfig);
|
|
|
|
|
|
console.log("更新请求配置:", config);
|
2025-08-02 10:35:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-08-02 14:03:12 +08:00
|
|
|
|
* 获取请求配置
|
|
|
|
|
|
* @returns {Object} 当前配置
|
2025-08-02 10:35:11 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export function getRequestConfig() {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
return { ...config };
|
2025-08-02 10:35:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 清除token
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function clearToken() {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
uni.removeStorageSync("token");
|
|
|
|
|
|
console.log("Token已清除");
|
2025-08-02 10:35:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置token
|
|
|
|
|
|
* @param {string} token - token值
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function setToken(token) {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
uni.setStorageSync("token", token);
|
|
|
|
|
|
console.log("Token已设置");
|
2025-08-02 10:35:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取token
|
|
|
|
|
|
* @returns {string} token值
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function getToken() {
|
2025-08-26 17:24:06 +08:00
|
|
|
|
return uni.getStorageSync("token");
|
2025-08-02 10:35:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-04 10:04:26 +08:00
|
|
|
|
// Loading管理相关函数已从loading-manager.js导入
|
|
|
|
|
|
|
|
|
|
|
|
// 默认导出request函数,方便API文件导入
|
2025-08-26 17:24:06 +08:00
|
|
|
|
export default request;
|