Files
lunar-mini/mini/utils/request.js
T
gouki 11bfc15141 feat: 添加许愿树功能和Go后端服务
- 新增许愿树页面(Canvas绘制、许愿条展示)
- 新增许愿详情页面
- 新增许愿创建弹窗(免费/付费两种模式)
- 新增网络请求封装 utils/request.js
- 新增Go后端项目结构(Gin + Inertia.js)
- 新增用户、订单、许愿数据模型
- 新增数据库迁移脚本
- 更新.gitignore补全忽略规则
- 更新.env.local配置(robot=2, 版本1.0.1)
- 添加secrets目录说明文档
2026-08-06 12:26:29 +00:00

68 lines
1.6 KiB
JavaScript

// 网络请求封装
const BASE_URL = 'http://localhost:8080'; // 开发环境
// 请求拦截器
function request(options) {
return new Promise((resolve, reject) => {
// 获取 token
const token = wx.getStorageSync('token');
wx.request({
url: BASE_URL + options.url,
method: options.method || 'GET',
data: options.data || {},
header: {
'Content-Type': 'application/json',
'Authorization': token ? `Bearer ${token}` : '',
...options.header,
},
success: (res) => {
if (res.statusCode === 200) {
resolve(res.data);
} else if (res.statusCode === 401) {
// 未授权,跳转登录
wx.navigateTo({ url: '/pages/settings/settings' });
reject(new Error('未授权'));
} else {
reject(new Error(res.data.msg || '请求失败'));
}
},
fail: (err) => {
console.error('请求失败:', err);
reject(err);
},
});
});
}
// 上传文件
function uploadFile(options) {
return new Promise((resolve, reject) => {
const token = wx.getStorageSync('token');
wx.uploadFile({
url: BASE_URL + options.url,
filePath: options.filePath,
name: options.name || 'file',
header: {
'Authorization': token ? `Bearer ${token}` : '',
},
formData: options.formData || {},
success: (res) => {
if (res.statusCode === 200) {
resolve(JSON.parse(res.data));
} else {
reject(new Error('上传失败'));
}
},
fail: reject,
});
});
}
module.exports = {
request,
uploadFile,
BASE_URL,
};