2025-07-16 10:23:06 +08:00
|
|
|
|
import config from '../config/index.js';
|
2025-11-17 15:30:57 +08:00
|
|
|
|
export const env = 'development'; //production development //开发of线上 改这里就行
|
2025-07-16 11:16:19 +08:00
|
|
|
|
const BASE = config[env];
|
2025-07-09 13:41:42 +08:00
|
|
|
|
const request = (options) => {
|
2025-11-12 09:51:35 +08:00
|
|
|
|
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}`;
|
|
|
|
|
|
}
|
2025-07-05 14:49:26 +08:00
|
|
|
|
|
2025-11-12 09:51:35 +08:00
|
|
|
|
const config = {
|
|
|
|
|
|
url: url,
|
|
|
|
|
|
method: options.method || 'GET',
|
|
|
|
|
|
data: options.method !== 'GET' ? options.data : {},
|
|
|
|
|
|
header: options.header || {},
|
|
|
|
|
|
timeout: 30000,
|
|
|
|
|
|
success: (res) => {
|
|
|
|
|
|
console.log(res, 'resss');
|
|
|
|
|
|
if (res.data.code === 401) {
|
|
|
|
|
|
uni.removeStorageSync('token');
|
|
|
|
|
|
uni.removeStorageSync('clientID');
|
|
|
|
|
|
uni.showToast({
|
|
|
|
|
|
title: '登录已过期,请重新登录',
|
|
|
|
|
|
icon: 'none',
|
|
|
|
|
|
duration: 2000,
|
|
|
|
|
|
complete: () => {
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
uni.reLaunch({
|
|
|
|
|
|
url: '/pages/common/login/index'
|
|
|
|
|
|
});
|
|
|
|
|
|
}, 3000);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
// 返回一个pending的Promise,中断当前的Promise链
|
|
|
|
|
|
return new Promise(() => { });
|
|
|
|
|
|
}
|
|
|
|
|
|
resolve(res.data);
|
|
|
|
|
|
},
|
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
|
console.log("ex=", err);
|
|
|
|
|
|
reject(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2025-07-09 13:41:42 +08:00
|
|
|
|
|
2025-11-12 09:51:35 +08:00
|
|
|
|
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);
|
|
|
|
|
|
});
|
2025-07-09 13:41:42 +08:00
|
|
|
|
};
|
2025-07-14 16:10:57 +08:00
|
|
|
|
// 导出基础URL以便其他地方使用
|
2025-09-24 10:18:31 +08:00
|
|
|
|
export const baseURL = BASE.BASE_URL;
|
|
|
|
|
|
export const getToken = () => uni.getStorageSync('token'); // 获取token的方法
|
2025-11-12 09:51:35 +08:00
|
|
|
|
export const clientid = () => uni.getStorageSync('clientID');
|
|
|
|
|
|
export default request;
|