2024-03-04 07:10:11 +00:00
|
|
|
|
import { useMemberStore } from "@/store/modules/member";
|
|
|
|
|
import ENV_CONFIG from "../config/env";
|
|
|
|
|
|
|
|
|
|
// 基础地址
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
baseUrl = (process.env as any).config[(process.env as any).NODE_ENV]
|
|
|
|
|
.VITE_APP_BASE_URL;
|
|
|
|
|
|
|
|
|
|
const store = useMemberStore();
|
|
|
|
|
|
|
|
|
|
const obj = {
|
|
|
|
|
// 拦截前触发
|
|
|
|
|
invoke(options: UniApp.RequestOptions) {
|
|
|
|
|
// 超时时间
|
|
|
|
|
options.timeout = 10000;
|
|
|
|
|
// 拼接完整路径
|
|
|
|
|
if (!options.url.startsWith("http")) {
|
|
|
|
|
options.url = baseUrl + options.url;
|
|
|
|
|
}
|
|
|
|
|
// 请求头标识
|
|
|
|
|
// 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
|
|
|
|
console.log(options);
|
|
|
|
|
if ((options as any).header?.type === "UPLOAD") {
|
|
|
|
|
uni.uploadFile({
|
|
|
|
|
header: {
|
|
|
|
|
"x-userToken": store.profile?.token,
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
"Content-Type": "multipart/form-data",
|
|
|
|
|
// #endif
|
|
|
|
|
},
|
|
|
|
|
url: baseUrl + 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);
|
2024-03-21 05:52:15 +00:00
|
|
|
|
resolve(err as any)
|
2024-03-09 12:37:17 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
uni.request({
|
|
|
|
|
...options,
|
|
|
|
|
dataType:'string', //1.先将dataType设置为string
|
|
|
|
|
success(res) {
|
|
|
|
|
var json=(res.data as any).replace(/:s*([0-9]{15,})s*(,?)/g, ': "$1" $2')
|
|
|
|
|
//2.根据后端返回的数据调用一次或者两次replace替换
|
|
|
|
|
var json1=json.replace(/:s*([0-9]{15,})s*(,?)/g, ': "$1" $2')
|
|
|
|
|
//3.手动转换回json数据即可
|
|
|
|
|
let res1 = JSON.parse(json1);
|
|
|
|
|
console.log(res1)
|
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
|
if (
|
|
|
|
|
(res1 as any).code === 1001 ||
|
|
|
|
|
(res1 as any).code === 1002
|
|
|
|
|
) {
|
|
|
|
|
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-03-21 05:52:15 +00:00
|
|
|
|
}else if ((res1 as any).code === 10001) {
|
2024-03-09 12:37:17 +00:00
|
|
|
|
uni.showToast({
|
|
|
|
|
icon: "none",
|
|
|
|
|
title: (res1 as Data<T>).message || "请求失败",
|
|
|
|
|
});
|
|
|
|
|
return
|
2024-03-21 05:52:15 +00:00
|
|
|
|
} else if ((res1 as any).code === 5001) {
|
|
|
|
|
uni.showToast({
|
|
|
|
|
icon: "none",
|
|
|
|
|
title: (res1 as Data<T>).message || "请求失败",
|
|
|
|
|
});
|
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-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
|
|
|
|
});
|
|
|
|
|
};
|