refactor(upload): 重构文件上传逻辑,改用OSS直传方案 refactor(vendor): 禁用开发模式下的日志WebSocket连接 style: 统一文件末尾换行符和代码格式 chore: 更新项目配置中的appid和编译设置
60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
const baseUrl = 'https://mini-chat.1024tool.vip/api/'
|
||
/**
|
||
* 发送 HTTP 请求到后端接口
|
||
*
|
||
* 功能描述:
|
||
* - 封装 wx.request,统一设置 JSON 请求头与基础地址
|
||
* - 成功时返回后端 res.data(已由微信自动 JSON.parse)
|
||
* - 业务错误(res.data.code 存在)统一弹 Toast 并 reject
|
||
* - 网络错误或服务器异常走 fail 并直接 reject
|
||
*
|
||
* 参数说明:
|
||
* @param {string} url - 相对接口路径,例如 'app/send_message'
|
||
* @param {string} [method='GET'] - HTTP 方法,支持 'GET'、'POST' 等
|
||
* @param {Object} [data={}] - 请求参数对象,将作为请求体/查询参数发送
|
||
*
|
||
* 返回值:
|
||
* @returns {Promise<any>} - 成功时 resolve 为后端返回的数据对象,失败时 reject 错误信息
|
||
*/
|
||
function request(url, method = 'GET', data = {}) {
|
||
|
||
const header = {
|
||
'content-type': 'application/json',
|
||
// 有其他content-type需求加点逻辑判断处理即可
|
||
};
|
||
|
||
return new Promise((resolve, reject) => {
|
||
wx.request({
|
||
url: baseUrl + url,
|
||
method,
|
||
data,
|
||
dataType: 'json', // 微信官方文档中介绍会对数据进行一次JSON.parse
|
||
header,
|
||
success(res) {
|
||
if (res.data.code) {
|
||
if (res.data.code == 10103) {
|
||
|
||
reject(res.data);
|
||
return;
|
||
}
|
||
wx.showToast({
|
||
title: res.data.message,
|
||
icon: 'none'
|
||
});
|
||
reject(res.data);
|
||
} else {
|
||
resolve(res.data);
|
||
}
|
||
|
||
},
|
||
fail(err) {
|
||
console.log(err)
|
||
// 断网、服务器挂了都会fail回调,直接reject即可
|
||
reject(err);
|
||
},
|
||
});
|
||
});
|
||
}
|
||
|
||
// 导出请求函数(CommonJS 导出,兼容微信小程序解析环境)
|
||
module.exports = request; |