初始化登录功能,注册功能
This commit is contained in:
1 parent
63726d8200
commit
76ba02d29b
16 files changed
+2090
-1025
No files matched your search
+49
-78
@@ -1,84 +1,73 @@
|
||||
// utils/request.js(或你当前文件)
|
||||
|
||||
import { useUserStore } from '@/store/user';
|
||||
|
||||
export const baseUrl = import.meta.env.VITE_APP_BASE_URL;
|
||||
const appVersion = uni.getSystemInfoSync().appVersion;
|
||||
// 添加拦截器
|
||||
// const httpInterceptor = {
|
||||
// // 拦截前触发
|
||||
// invoke(options) {
|
||||
// // 1. 非 http 开头需拼接地址
|
||||
// if (!options.url.startsWith('http')) {
|
||||
// options.url = baseUrl + options.url;
|
||||
// }
|
||||
// // 2. 请求超时, 默认 10s
|
||||
// options.timeout = 10000;
|
||||
// options.header = {
|
||||
// ...options.header,
|
||||
// 'Content-Type': 'application/x-www-form-urlencoded',
|
||||
// };
|
||||
// const userStore = useUserStore();
|
||||
// options.data = {
|
||||
// ...options.data,
|
||||
// __token: userStore.userToken,
|
||||
// __platform: userStore.platform,
|
||||
// };
|
||||
// },
|
||||
// };
|
||||
// uni.addInterceptor('request', httpInterceptor);
|
||||
// // #ifndef H5
|
||||
// uni.addInterceptor('uploadFile', httpInterceptor);
|
||||
// // #endif
|
||||
|
||||
// 对uni原生的request做的一个封装
|
||||
// 对 uni.request 的封装
|
||||
export const request = (options = {}) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const userStore = useUserStore();
|
||||
// 请求头
|
||||
options.header = {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
// version: appVersion,
|
||||
};
|
||||
var params = {
|
||||
|
||||
// 👇 关键修改:默认 Content-Type 为 application/json
|
||||
const contentType = options.header?.['Content-Type'] || 'application/json';
|
||||
|
||||
// 公共参数
|
||||
const commonParams = {
|
||||
__token: userStore.userToken,
|
||||
__platform: userStore.platform,
|
||||
__language: userStore.lang,
|
||||
__channelId: 33,
|
||||
};
|
||||
var data = options.data;
|
||||
data = {
|
||||
...data,
|
||||
...params,
|
||||
|
||||
// 合并 data
|
||||
let requestData = {
|
||||
...options.data,
|
||||
...commonParams,
|
||||
};
|
||||
// uni原生request
|
||||
|
||||
// 👇 如果是 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 || '',
|
||||
url: baseUrl + (options.url || ''),
|
||||
method: options.method || 'GET',
|
||||
data: data || {},
|
||||
header: options.header || {},
|
||||
data: requestData, // 对象 or 字符串
|
||||
header: {
|
||||
'Content-Type': contentType,
|
||||
// version: appVersion, // 如需可加
|
||||
...options.header,
|
||||
},
|
||||
})
|
||||
.then((res) => {
|
||||
if (res.statusCode === 401) {
|
||||
// usetToken无效,移除本地缓存
|
||||
userStore.userToken = '';
|
||||
userStore.clearProfile();
|
||||
userStore.isLogin = false;
|
||||
resolve({
|
||||
...res.data,
|
||||
statusCode: res.statusCode,
|
||||
});
|
||||
} else {
|
||||
resolve({
|
||||
...res.data,
|
||||
statusCode: res.statusCode,
|
||||
});
|
||||
}
|
||||
resolve({
|
||||
...res.data,
|
||||
statusCode: res.statusCode,
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error.statusCode === 401) {
|
||||
// usetToken无效,移除本地缓存
|
||||
userStore.userToken = '';
|
||||
userStore.clearProfile();
|
||||
userStore.isLogin = false;
|
||||
reject({
|
||||
...error.data,
|
||||
statusCode: error.statusCode,
|
||||
@@ -99,37 +88,19 @@ export const request = (options = {}) => {
|
||||
});
|
||||
};
|
||||
|
||||
// 定义get请求
|
||||
// 导出方法(保持不变)
|
||||
export const get = (url, data = {}, options = {}) => {
|
||||
options.url = url;
|
||||
options.method = 'GET';
|
||||
options.data = data;
|
||||
|
||||
return request(options);
|
||||
return request({ ...options, url, method: 'GET', data });
|
||||
};
|
||||
// 定义post请求
|
||||
|
||||
export const post = (url, data = {}, options = {}) => {
|
||||
options.url = url;
|
||||
options.method = 'POST';
|
||||
options.data = data;
|
||||
|
||||
return request(options);
|
||||
return request({ ...options, url, method: 'POST', data });
|
||||
};
|
||||
|
||||
// 定义put请求
|
||||
export const put = (url, data = {}, options = {}) => {
|
||||
options.url = url;
|
||||
options.method = 'PUT';
|
||||
options.data = data;
|
||||
|
||||
return request(options);
|
||||
return request({ ...options, url, method: 'PUT', data });
|
||||
};
|
||||
|
||||
// 定义delete请求
|
||||
export const dele = (url, data = {}, options = {}) => {
|
||||
options.url = url;
|
||||
options.method = 'DELETE';
|
||||
options.data = data;
|
||||
|
||||
return request(options);
|
||||
};
|
||||
return request({ ...options, url, method: 'DELETE', data });
|
||||
};
|
||||
Reference in new issue
Block a user