2024-03-04 07:10:11 +00:00
|
|
|
|
import { useMemberStore } from "@/store/modules/member";
|
|
|
|
|
import ENV_CONFIG from "../config/env";
|
2024-06-04 01:50:18 +00:00
|
|
|
|
import pinia from "@/store";
|
2024-03-04 07:10:11 +00:00
|
|
|
|
|
|
|
|
|
// 基础地址
|
|
|
|
|
let baseUrl = "";
|
|
|
|
|
// #ifdef H5
|
2024-03-09 12:37:17 +00:00
|
|
|
|
(process.env as any).config = ENV_CONFIG;
|
2024-03-04 07:10:11 +00:00
|
|
|
|
// #endif
|
|
|
|
|
|
2024-05-09 02:45:07 +00:00
|
|
|
|
const store = useMemberStore(pinia);
|
2024-06-06 08:47:00 +00:00
|
|
|
|
baseUrl = (process.env as any).config[(process.env as any).NODE_ENV]
|
|
|
|
|
.VITE_APP_BASE_URL;
|
2024-03-04 07:10:11 +00:00
|
|
|
|
|
|
|
|
|
const obj = {
|
|
|
|
|
// 拦截前触发
|
|
|
|
|
invoke(options: UniApp.RequestOptions) {
|
|
|
|
|
// 超时时间
|
|
|
|
|
options.timeout = 10000;
|
|
|
|
|
// 拼接完整路径
|
|
|
|
|
if (!options.url.startsWith("http")) {
|
2024-06-06 08:47:00 +00:00
|
|
|
|
if (options.url.indexOf("/prod/gateway/api/db/getDbInfo") === -1) {
|
2024-06-04 01:50:18 +00:00
|
|
|
|
options.url = baseUrl + store.profile.childPath + options.url;
|
|
|
|
|
} else {
|
|
|
|
|
options.url = baseUrl + options.url;
|
|
|
|
|
}
|
2024-03-04 07:10:11 +00:00
|
|
|
|
}
|
|
|
|
|
// 请求头标识
|
|
|
|
|
// 1.小程序唯一标识
|
|
|
|
|
// 'source-client': 'miniapp'
|
|
|
|
|
options.header = { ...options.header };
|
|
|
|
|
options.header["source-client"] = "miniapp";
|
|
|
|
|
// 2.注入token
|
|
|
|
|
// 2.1 获取token
|
|
|
|
|
const token = store.profile?.token;
|
|
|
|
|
if (token) {
|
|
|
|
|
options.header["x-userToken"] = token;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
// 请求拦截器
|
|
|
|
|
uni.addInterceptor("request", obj);
|
|
|
|
|
uni.addInterceptor("uploadFile", obj);
|
|
|
|
|
|
|
|
|
|
interface Data<T> {
|
|
|
|
|
code: string | number;
|
|
|
|
|
data: T;
|
|
|
|
|
msg: string;
|
|
|
|
|
result: T;
|
|
|
|
|
message: string;
|
|
|
|
|
}
|
|
|
|
|
// 响应拦截器
|
|
|
|
|
/***
|
|
|
|
|
* 1001: 未登录
|
|
|
|
|
* 1002: 登陆过期
|
|
|
|
|
*/
|
|
|
|
|
export const http = <T>(options: UniApp.RequestOptions) => {
|
|
|
|
|
return new Promise<Data<T>>((resolve, reject) => {
|
2024-03-09 12:37:17 +00:00
|
|
|
|
if ((options as any).header?.type === "UPLOAD") {
|
2024-06-06 08:47:00 +00:00
|
|
|
|
// 压缩图片
|
|
|
|
|
uni.compressImage({
|
|
|
|
|
src: (options as any).data.path,
|
|
|
|
|
quality: 80, // 压缩质量
|
|
|
|
|
success: (compressRes) => {
|
|
|
|
|
const compressedTempFilePath = compressRes.tempFilePath;
|
|
|
|
|
uni.uploadFile({
|
|
|
|
|
header: {
|
|
|
|
|
"x-userToken": store.profile?.token,
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
"Content-Type": "multipart/form-data",
|
|
|
|
|
// #endif
|
|
|
|
|
},
|
|
|
|
|
timeout: 1800000,
|
|
|
|
|
url: baseUrl + store.profile.childPath + options.url,//仅为示例,非真实的接口地址
|
|
|
|
|
fileType: "image",
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
files: (options as any).data.files,
|
|
|
|
|
// #endif
|
|
|
|
|
// #ifdef MP-WEIXIN
|
|
|
|
|
filePath: compressedTempFilePath,
|
|
|
|
|
// #endif
|
|
|
|
|
name: "file",
|
|
|
|
|
formData: {
|
|
|
|
|
file: (options as any).data.files,
|
|
|
|
|
}, // HTTP 请求中其他额外的 form data
|
|
|
|
|
success: (res) => {
|
|
|
|
|
resolve(JSON.parse(res.data) as Data<T>);
|
|
|
|
|
},
|
|
|
|
|
fail(err) {
|
|
|
|
|
console.log("上传失败了", err);
|
|
|
|
|
resolve(err as any);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 上传图片
|
|
|
|
|
// uni.uploadFile({
|
|
|
|
|
// url: 'https://your-api-upload-endpoint.com/upload', // 你的上传接口
|
|
|
|
|
// filePath: compressedTempFilePath,
|
|
|
|
|
// name: 'file',
|
|
|
|
|
// formData: {
|
|
|
|
|
// 'user': 'test'
|
|
|
|
|
// },
|
|
|
|
|
// success: (uploadFileRes) => {
|
|
|
|
|
// console.log('upload success:', uploadFileRes);
|
|
|
|
|
// },
|
|
|
|
|
// fail: (uploadFileErr) => {
|
|
|
|
|
// console.error('upload fail:', uploadFileErr);
|
|
|
|
|
// }
|
|
|
|
|
// });
|
2024-03-09 12:37:17 +00:00
|
|
|
|
},
|
2024-06-06 08:47:00 +00:00
|
|
|
|
fail: (compressErr) => {
|
|
|
|
|
console.error("compress fail:", compressErr);
|
2024-03-09 12:37:17 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
2024-06-06 08:47:00 +00:00
|
|
|
|
|
|
|
|
|
// uni.uploadFile({
|
|
|
|
|
// header: {
|
|
|
|
|
// "x-userToken": store.profile?.token,
|
|
|
|
|
// // #ifdef H5
|
|
|
|
|
// "Content-Type": "multipart/form-data",
|
|
|
|
|
// // #endif
|
|
|
|
|
// },
|
|
|
|
|
// timeout: 1800000,
|
|
|
|
|
// url: baseUrl + store.profile.childPath + options.url,//仅为示例,非真实的接口地址
|
|
|
|
|
// fileType: "image",
|
|
|
|
|
// // #ifdef H5
|
|
|
|
|
// files: (options as any).data.files,
|
|
|
|
|
// // #endif
|
|
|
|
|
// // #ifdef MP-WEIXIN
|
|
|
|
|
// filePath: (options as any).data.path,
|
|
|
|
|
// // #endif
|
|
|
|
|
// name: "file",
|
|
|
|
|
// formData: {
|
|
|
|
|
// file: (options as any).data.files,
|
|
|
|
|
// }, // HTTP 请求中其他额外的 form data
|
|
|
|
|
// success: (res) => {
|
|
|
|
|
// resolve(JSON.parse(res.data) as Data<T>);
|
|
|
|
|
// },
|
|
|
|
|
// fail(err) {
|
|
|
|
|
// console.log("上传失败了", err);
|
|
|
|
|
// resolve(err as any);
|
|
|
|
|
// },
|
|
|
|
|
// });
|
2024-03-09 12:37:17 +00:00
|
|
|
|
} else {
|
|
|
|
|
uni.request({
|
|
|
|
|
...options,
|
2024-06-04 01:50:18 +00:00
|
|
|
|
dataType: "string", //1.先将dataType设置为string
|
2024-03-09 12:37:17 +00:00
|
|
|
|
success(res) {
|
2024-06-04 01:50:18 +00:00
|
|
|
|
var json = (res.data as any).replace(
|
|
|
|
|
/:s*([0-9]{15,})s*(,?)/g,
|
|
|
|
|
': "$1" $2'
|
|
|
|
|
);
|
2024-03-09 12:37:17 +00:00
|
|
|
|
//2.根据后端返回的数据调用一次或者两次replace替换
|
2024-06-04 01:50:18 +00:00
|
|
|
|
var json1 = json.replace(/:s*([0-9]{15,})s*(,?)/g, ': "$1" $2');
|
2024-03-09 12:37:17 +00:00
|
|
|
|
//3.手动转换回json数据即可
|
2024-06-04 01:50:18 +00:00
|
|
|
|
let res1: any = JSON.parse(json1);
|
|
|
|
|
console.log(res1);
|
2024-03-09 12:37:17 +00:00
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
|
if (
|
2024-06-14 03:04:19 +00:00
|
|
|
|
[1001,1002,1003,1004].indexOf(res1.code) > -1
|
2024-03-09 12:37:17 +00:00
|
|
|
|
) {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
icon: "none",
|
|
|
|
|
title: (res1 as Data<T>).message || "请求失败",
|
|
|
|
|
});
|
|
|
|
|
store.clearProfile();
|
2024-04-24 06:35:43 +00:00
|
|
|
|
uni.reLaunch({
|
2024-04-11 06:06:04 +00:00
|
|
|
|
url: "/pagesLogin/login/index",
|
2024-03-09 12:37:17 +00:00
|
|
|
|
});
|
|
|
|
|
return;
|
2024-06-14 03:04:19 +00:00
|
|
|
|
} else if ([500,5001,10001].indexOf(res1.code) > -1) {
|
2024-03-09 12:37:17 +00:00
|
|
|
|
uni.showToast({
|
|
|
|
|
icon: "none",
|
|
|
|
|
title: (res1 as Data<T>).message || "请求失败",
|
|
|
|
|
});
|
2024-06-04 01:50:18 +00:00
|
|
|
|
return;
|
2024-03-09 12:37:17 +00:00
|
|
|
|
}
|
|
|
|
|
resolve(res1 as Data<T>);
|
|
|
|
|
} else {
|
2024-03-04 07:10:11 +00:00
|
|
|
|
uni.showToast({
|
|
|
|
|
icon: "none",
|
2024-03-09 12:37:17 +00:00
|
|
|
|
title: (res1 as any).msg || "请求失败",
|
2024-03-04 07:10:11 +00:00
|
|
|
|
});
|
2024-03-09 12:37:17 +00:00
|
|
|
|
reject(res1);
|
2024-03-04 07:10:11 +00:00
|
|
|
|
}
|
2024-03-09 12:37:17 +00:00
|
|
|
|
},
|
|
|
|
|
fail(err) {
|
2024-03-04 07:10:11 +00:00
|
|
|
|
uni.showToast({
|
|
|
|
|
icon: "none",
|
2024-06-04 02:35:28 +00:00
|
|
|
|
// title: JSON.stringify(err),
|
2024-03-09 12:37:17 +00:00
|
|
|
|
title: "网络错误,请检查网络",
|
2024-03-04 07:10:11 +00:00
|
|
|
|
});
|
2024-03-09 12:37:17 +00:00
|
|
|
|
reject(err);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-03-04 07:10:11 +00:00
|
|
|
|
});
|
|
|
|
|
};
|