Files
APP/utils/request.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

2025-07-11 16:11:40 +08:00
const BASE_URL = 'http://192.168.110.169:9001';
2025-07-07 11:27:24 +08:00
const request = (options) => {
2025-07-11 16:11:40 +08:00
console.log("options"+JSON.stringify(options))
return new Promise((resolve, reject) => {
// 处理GET请求参数
let url = BASE_URL + options.url;
if (options.method === 'GET' && options.data) {
// 使用qs序列化参数
const params = Object.keys(options.data)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(options.data[key])}`)
.join('&');
url += `?${params}`;
}
2025-07-05 14:49:26 +08:00
const config = {
url: url,
method: options.method || 'GET',
data: options.method !== 'GET' ? options.data : {},
header: options.header || {},
timeout: 10000,
success: (res) => {
resolve(res.data);
},
fail: (err) => {
reject(err);
}
};
if (!options.url.includes('/login')) {
const token = uni.getStorageSync('token');
const clientid = uni.getStorageSync('clientID');
if (token) {
config.header['Authorization'] = 'Bearer ' + token;
config.header['clientid'] = clientid;
}
}
if (!config.header['Content-Type']) {
config.header['Content-Type'] = 'application/json';
}
uni.request(config);
});
};
export default request;