143 lines
3.3 KiB
JavaScript
143 lines
3.3 KiB
JavaScript
/**
|
||
* 网络请求工具
|
||
* 仅用于登录流程(获取验证码、登录、获取用户信息)
|
||
* 业务流程(MQTT 连接、数据上报)不依赖此模块
|
||
*/
|
||
const envConfig = require('../env.config.js');
|
||
const storage = require('./storage.js');
|
||
|
||
/**
|
||
* 发起 HTTP 请求
|
||
* @param {Object} options - 请求配置
|
||
* @param {string} options.url - 请求地址
|
||
* @param {string} options.method - 请求方法
|
||
* @param {Object} options.data - 请求数据
|
||
* @param {Object} options.header - 请求头
|
||
* @param {boolean} options.auth - 是否需要认证
|
||
* @returns {Promise}
|
||
*/
|
||
function request(options) {
|
||
return new Promise((resolve, reject) => {
|
||
const header = options.header || {};
|
||
|
||
// 添加 Token
|
||
if (options.auth !== false) {
|
||
const token = storage.getToken();
|
||
if (token) {
|
||
header['Authorization'] = 'Bearer ' + token;
|
||
}
|
||
}
|
||
|
||
wx.request({
|
||
url: envConfig.baseUrl + options.url,
|
||
method: options.method || 'GET',
|
||
data: options.data || {},
|
||
header: {
|
||
'Content-Type': 'application/json',
|
||
...header,
|
||
},
|
||
success(res) {
|
||
if (res.statusCode === 200) {
|
||
// yack 统一响应格式
|
||
if (res.data.code === 200) {
|
||
resolve(res.data);
|
||
} else {
|
||
// 业务错误
|
||
const errMsg = res.data.msg || '请求失败';
|
||
console.error('[Request] 业务错误:', errMsg);
|
||
reject(new Error(errMsg));
|
||
}
|
||
} else if (res.statusCode === 401) {
|
||
// Token 过期(仅登录流程使用,业务不依赖 token)
|
||
console.error('[Request] 未授权');
|
||
// 不清除登录态,不强制跳转(业务流程独立于 token)
|
||
reject(new Error('登录已过期'));
|
||
} else {
|
||
console.error('[Request] HTTP 错误:', res.statusCode);
|
||
reject(new Error('网络请求失败'));
|
||
}
|
||
},
|
||
fail(err) {
|
||
console.error('[Request] 网络错误:', err);
|
||
reject(new Error('网络连接失败'));
|
||
},
|
||
});
|
||
});
|
||
}
|
||
|
||
/**
|
||
* GET 请求
|
||
* @param {string} url - 请求地址
|
||
* @param {Object} data - 查询参数
|
||
* @param {Object} options - 其他配置
|
||
*/
|
||
function get(url, data = {}, options = {}) {
|
||
return request({
|
||
url,
|
||
method: 'GET',
|
||
data,
|
||
...options,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* POST 请求
|
||
* @param {string} url - 请求地址
|
||
* @param {Object} data - 请求数据
|
||
* @param {Object} options - 其他配置
|
||
*/
|
||
function post(url, data = {}, options = {}) {
|
||
return request({
|
||
url,
|
||
method: 'POST',
|
||
data,
|
||
...options,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 登录接口
|
||
* @param {string} username
|
||
* @param {string} password
|
||
* @param {string} code - 验证码(可选)
|
||
* @param {string} uuid - 验证码 UUID(可选)
|
||
*/
|
||
function login(username, password, code = '', uuid = '') {
|
||
return post('/login', {
|
||
username,
|
||
password,
|
||
code,
|
||
uuid,
|
||
}, { auth: false });
|
||
}
|
||
|
||
/**
|
||
* 获取用户信息
|
||
*/
|
||
function getInfo() {
|
||
return get('/getInfo');
|
||
}
|
||
|
||
/**
|
||
* 退出登录
|
||
*/
|
||
function logout() {
|
||
return post('/logout');
|
||
}
|
||
|
||
/**
|
||
* 获取验证码
|
||
*/
|
||
function captchaImage() {
|
||
return get('/captchaImage', { showCode: 'false' });
|
||
}
|
||
|
||
module.exports = {
|
||
request,
|
||
get,
|
||
post,
|
||
login,
|
||
getInfo,
|
||
logout,
|
||
captchaImage,
|
||
}; |