Files
APP/utils/request.js
微微一笑 6257f9d84b 增加mqtt
2025-07-16 11:16:19 +08:00

50 lines
1.6 KiB
JavaScript

import config from '../config/index.js';
const env = 'development';//production //开发of线上 改这里就行
const BASE = config[env];
const request = (options) => {
console.log("options"+JSON.stringify(options),BASE.BASE_URL)
return new Promise((resolve, reject) => {
// 处理GET请求参数
let url = BASE.BASE_URL + options.url;
console.log("url"+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}`;
}
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);
});
};
// 导出基础URL以便其他地方使用
export const baseURL = BASE.BASE_URL
export const getToken = () => uni.getStorageSync('token') // 获取token的方法
export const clientid =() => uni.getStorageSync('clientID');
export default request;