107 lines
3.2 KiB
JavaScript
107 lines
3.2 KiB
JavaScript
// utils/request.js(或你当前文件)
|
||
|
||
import { useUserStore } from '@/store/user';
|
||
|
||
export const baseUrl = import.meta.env.VITE_APP_BASE_URL;
|
||
const appVersion = uni.getSystemInfoSync().appVersion;
|
||
|
||
// 对 uni.request 的封装
|
||
export const request = (options = {}) => {
|
||
return new Promise((resolve, reject) => {
|
||
const userStore = useUserStore();
|
||
|
||
// 👇 关键修改:默认 Content-Type 为 application/json
|
||
const contentType = options.header?.['Content-Type'] || 'application/json';
|
||
|
||
// 公共参数
|
||
const commonParams = {
|
||
__token: userStore.userToken,
|
||
__platform: userStore.platform,
|
||
__language: userStore.lang,
|
||
};
|
||
|
||
// 合并 data
|
||
let requestData = {
|
||
...options.data,
|
||
...commonParams,
|
||
};
|
||
|
||
// 👇 如果是 JSON,data 保持为对象(uni.request 会自动序列化)
|
||
// 如果是 x-www-form-urlencoded,需要手动转成字符串(但不推荐)
|
||
if (contentType === 'application/x-www-form-urlencoded') {
|
||
// 注意:uni.request 在非 H5 平台可能不支持直接传对象 + x-www-form-urlencoded
|
||
// 所以这里需要手动拼接
|
||
const params = new URLSearchParams();
|
||
for (const key in requestData) {
|
||
if (requestData[key] !== undefined && requestData[key] !== null) {
|
||
params.append(key, requestData[key]);
|
||
}
|
||
}
|
||
requestData = params.toString(); // 转为 a=1&b=2 形式
|
||
}
|
||
// 如果是 application/json,requestData 保持为对象即可
|
||
|
||
uni
|
||
.request({
|
||
url: baseUrl + (options.url || ''),
|
||
method: options.method || 'GET',
|
||
data: requestData, // 对象 or 字符串
|
||
header: {
|
||
'Content-Type': contentType,
|
||
'Authorization': 'Bearer ' + userStore.token,
|
||
// version: appVersion, // 如需可加
|
||
...options.header,
|
||
},
|
||
})
|
||
.then((res) => {
|
||
if (res.statusCode === 401) {
|
||
userStore.userToken = '';
|
||
userStore.clearProfile();
|
||
userStore.isLogin = false;
|
||
}
|
||
resolve({
|
||
...res.data,
|
||
statusCode: res.statusCode,
|
||
});
|
||
})
|
||
.catch((error) => {
|
||
if (error.statusCode === 401) {
|
||
userStore.userToken = '';
|
||
userStore.clearProfile();
|
||
userStore.isLogin = false;
|
||
reject({
|
||
...error.data,
|
||
statusCode: error.statusCode,
|
||
});
|
||
} else {
|
||
uni.showToast({
|
||
duration: 2500,
|
||
icon: 'none',
|
||
mask: true,
|
||
title: 'Server exception, please try again later',
|
||
});
|
||
setTimeout(() => {
|
||
uni.hideLoading();
|
||
reject(error.data);
|
||
}, 2000);
|
||
}
|
||
});
|
||
});
|
||
};
|
||
|
||
// 导出方法(保持不变)
|
||
export const get = (url, data = {}, options = {}) => {
|
||
return request({ ...options, url, method: 'GET', data });
|
||
};
|
||
|
||
export const post = (url, data = {}, options = {}) => {
|
||
return request({ ...options, url, method: 'POST', data });
|
||
};
|
||
|
||
export const put = (url, data = {}, options = {}) => {
|
||
return request({ ...options, url, method: 'PUT', data });
|
||
};
|
||
|
||
export const dele = (url, data = {}, options = {}) => {
|
||
return request({ ...options, url, method: 'DELETE', data });
|
||
}; |