2026-03-03 16:57:49 +08:00
|
|
|
|
/**
|
2026-03-11 10:07:07 +08:00
|
|
|
|
* 网络请求工具 - 支持本地/云托管切换
|
2026-03-03 16:57:49 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
2026-03-11 10:07:07 +08:00
|
|
|
|
// ============================================
|
|
|
|
|
|
// 环境配置(切换这里即可)
|
|
|
|
|
|
// ============================================
|
|
|
|
|
|
const ENV = 'cloud'; // 'local' = 本地后端, 'cloud' = 微信云托管
|
|
|
|
|
|
|
|
|
|
|
|
const CONFIG = {
|
|
|
|
|
|
local: {
|
2026-03-11 12:10:19 +08:00
|
|
|
|
baseUrl: 'http://172.20.10.8:8000/api' // 局域网IP,真机测试用
|
2026-03-11 10:07:07 +08:00
|
|
|
|
},
|
|
|
|
|
|
cloud: {
|
|
|
|
|
|
env: 'prod-6gjx1rd4c40f5884',
|
|
|
|
|
|
serviceName: 'express-fuvd'
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2026-03-03 16:57:49 +08:00
|
|
|
|
|
2026-03-11 12:10:19 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取存储的 Token
|
|
|
|
|
|
*/
|
|
|
|
|
|
function getToken() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const userInfo = wx.getStorageSync('userInfo');
|
|
|
|
|
|
return userInfo?.token || '';
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
return '';
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-03 16:57:49 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 发送HTTP请求
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function request(options) {
|
2026-03-11 10:07:07 +08:00
|
|
|
|
if (ENV === 'local') {
|
|
|
|
|
|
return requestLocal(options);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return requestCloud(options);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 本地后端请求(wx.request)
|
|
|
|
|
|
*/
|
|
|
|
|
|
function requestLocal(options) {
|
2026-03-03 16:57:49 +08:00
|
|
|
|
return new Promise((resolve, reject) => {
|
2026-03-11 12:10:19 +08:00
|
|
|
|
const timeoutMs = options.timeout || 30000;
|
|
|
|
|
|
|
|
|
|
|
|
// 自动添加 Token 到请求头
|
|
|
|
|
|
const token = getToken();
|
|
|
|
|
|
const header = {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
...options.header
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
|
|
header['Authorization'] = `Bearer ${token}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理 URL 查询参数
|
|
|
|
|
|
let url = CONFIG.local.baseUrl + options.url;
|
|
|
|
|
|
if (options.params) {
|
|
|
|
|
|
const queryString = Object.entries(options.params)
|
|
|
|
|
|
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
|
|
|
|
.join('&');
|
|
|
|
|
|
url += (url.includes('?') ? '&' : '?') + queryString;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-03 16:57:49 +08:00
|
|
|
|
wx.request({
|
2026-03-11 12:10:19 +08:00
|
|
|
|
url,
|
2026-03-03 16:57:49 +08:00
|
|
|
|
method: options.method || 'GET',
|
|
|
|
|
|
data: options.data || {},
|
2026-03-11 12:10:19 +08:00
|
|
|
|
timeout: timeoutMs,
|
|
|
|
|
|
header,
|
2026-03-03 16:57:49 +08:00
|
|
|
|
success(res) {
|
2026-03-11 12:10:19 +08:00
|
|
|
|
// 处理 401 未授权错误
|
|
|
|
|
|
if (res.statusCode === 401) {
|
|
|
|
|
|
// Token 过期或无效,清除本地存储
|
|
|
|
|
|
wx.removeStorageSync('userInfo');
|
|
|
|
|
|
reject(new Error('登录已过期,请重新登录'));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-11 10:07:07 +08:00
|
|
|
|
if (res.data && res.data.code === 0) {
|
2026-03-03 16:57:49 +08:00
|
|
|
|
resolve(res.data.data);
|
|
|
|
|
|
} else {
|
2026-03-11 10:07:07 +08:00
|
|
|
|
reject(new Error(res.data?.message || '请求失败'));
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
fail(err) {
|
|
|
|
|
|
console.error('[HTTP-Local] 请求失败:', err);
|
|
|
|
|
|
reject(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 云托管请求(wx.cloud.callContainer)
|
|
|
|
|
|
*/
|
|
|
|
|
|
function requestCloud(options) {
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2026-03-11 12:10:19 +08:00
|
|
|
|
// 自动添加 Token 到请求头
|
|
|
|
|
|
const token = getToken();
|
|
|
|
|
|
const header = {
|
|
|
|
|
|
'X-WX-SERVICE': CONFIG.cloud.serviceName,
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
...options.header
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
|
|
header['Authorization'] = `Bearer ${token}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理 URL 查询参数
|
|
|
|
|
|
let path = '/api' + options.url;
|
|
|
|
|
|
if (options.params) {
|
|
|
|
|
|
const queryString = Object.entries(options.params)
|
|
|
|
|
|
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
|
|
|
|
.join('&');
|
|
|
|
|
|
path += (path.includes('?') ? '&' : '?') + queryString;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-11 10:07:07 +08:00
|
|
|
|
wx.cloud.callContainer({
|
|
|
|
|
|
config: {
|
|
|
|
|
|
env: CONFIG.cloud.env
|
|
|
|
|
|
},
|
2026-03-11 12:10:19 +08:00
|
|
|
|
path,
|
2026-03-11 10:07:07 +08:00
|
|
|
|
method: options.method || 'GET',
|
|
|
|
|
|
data: options.data || {},
|
2026-03-11 12:10:19 +08:00
|
|
|
|
header,
|
2026-03-11 10:07:07 +08:00
|
|
|
|
success(res) {
|
2026-03-11 12:10:19 +08:00
|
|
|
|
// 处理 401 未授权错误
|
|
|
|
|
|
if (res.statusCode === 401) {
|
|
|
|
|
|
wx.removeStorageSync('userInfo');
|
|
|
|
|
|
reject(new Error('登录已过期,请重新登录'));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-11 10:07:07 +08:00
|
|
|
|
if (res.data && res.data.code === 0) {
|
|
|
|
|
|
resolve(res.data.data);
|
|
|
|
|
|
} else if (res.data) {
|
2026-03-03 16:57:49 +08:00
|
|
|
|
reject(new Error(res.data.message || '请求失败'));
|
2026-03-11 10:07:07 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
reject(new Error('响应数据异常'));
|
2026-03-03 16:57:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
fail(err) {
|
2026-03-11 10:07:07 +08:00
|
|
|
|
console.error('[HTTP-Cloud] 请求失败:', err);
|
2026-03-03 16:57:49 +08:00
|
|
|
|
reject(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* GET请求
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function get(url, data) {
|
|
|
|
|
|
return request({ url, method: 'GET', data });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* POST请求
|
|
|
|
|
|
*/
|
2026-03-05 15:57:51 +08:00
|
|
|
|
export function post(url, data, options = {}) {
|
|
|
|
|
|
return request({ url, method: 'POST', data, ...options });
|
2026-03-03 16:57:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-10 12:44:55 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* DELETE请求
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function del(url, data) {
|
|
|
|
|
|
return request({ url, method: 'DELETE', data });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default { request, get, post, del };
|