154 lines
4.1 KiB
JavaScript
154 lines
4.1 KiB
JavaScript
import axios from "axios";
|
|
import router from "../router";
|
|
import QS from "qs";
|
|
import { showToast } from "vant";
|
|
import { useMiscellaneousStore } from "../pinia/miscellaneous";
|
|
import { useUserStore } from "../pinia/user";
|
|
import { useSavedUsers } from "@/pinia/savedUsers.js";
|
|
|
|
const savedUsersStore = useSavedUsers();
|
|
const miscellaneousStore = useMiscellaneousStore();
|
|
const userStore = useUserStore();
|
|
|
|
function startLoading() {
|
|
miscellaneousStore.setLoading(true);
|
|
}
|
|
|
|
function endLoading() {
|
|
miscellaneousStore.setLoading(false);
|
|
}
|
|
|
|
axios.defaults.timeout = 1000 * 60 * 10;
|
|
// axios.defaults.withCredentials = true;
|
|
axios.interceptors.request.use(
|
|
(config) => {
|
|
if (config.method === "get" || config.method === "GET") {
|
|
if (QS.parse(config.params)?.loading !== "false") startLoading();
|
|
}
|
|
if (config.method === "post" || config.method === "POST") {
|
|
if (QS.parse(config.data)?.loading !== "false") startLoading();
|
|
}
|
|
if (userStore.getUserInfo.TOKEN) {
|
|
// config.headers.common.Token = userStore.getUserInfo.TOKEN;
|
|
config.headers.Token = userStore.getUserInfo.TOKEN;
|
|
}
|
|
return config;
|
|
},
|
|
(error) => Promise.reject(error)
|
|
);
|
|
|
|
axios.interceptors.response.use(
|
|
(config) => {
|
|
if (config.config.method === "get" || config.config.method === "GET") {
|
|
if (QS.parse(config.config.params)?.loading !== "false") endLoading();
|
|
}
|
|
if (config.config.method === "post" || config.config.method === "POST") {
|
|
if (QS.parse(config.config.data)?.loading !== "false") endLoading();
|
|
}
|
|
return config;
|
|
},
|
|
(error) => {
|
|
if (error && error.response) {
|
|
switch (error.response.status) {
|
|
case 0:
|
|
case 302:
|
|
endLoading();
|
|
showToast("登录失效,请重新登陆");
|
|
router.push("/login").then();
|
|
break;
|
|
case 401:
|
|
endLoading();
|
|
showToast("您的账号已在其他端登录");
|
|
localStorage.clear();
|
|
savedUsersStore.$reset();
|
|
router.push("/login").then();
|
|
break;
|
|
default:
|
|
error.message = `连接错误${error.response.status}`;
|
|
showToast(`连接错误${error.response.status}`);
|
|
}
|
|
} else {
|
|
error.message = "连接到服务器失败";
|
|
showToast("连接到服务器失败");
|
|
}
|
|
return Promise.reject(error.message);
|
|
}
|
|
);
|
|
|
|
export function post(url, params) {
|
|
return new Promise((resolve, reject) => {
|
|
axios
|
|
.post(
|
|
url,
|
|
QS.stringify({
|
|
USER_ID: userStore.getUserInfo.USER_ID ?? "",
|
|
...params,
|
|
}),
|
|
{
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
},
|
|
}
|
|
)
|
|
.then((res) => {
|
|
if (res.data.result === "success" && res.data.code !== "9999") {
|
|
resolve(res.data);
|
|
} else {
|
|
showToast(res.data.msg || "系统开小差了");
|
|
reject(res.data);
|
|
}
|
|
})
|
|
.catch((err) => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
|
|
export function get(url, params) {
|
|
return new Promise((resolve, reject) => {
|
|
axios
|
|
.get(url, {
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
},
|
|
params: QS.stringify({
|
|
USER_ID: userStore.getUserInfo.USER_ID ?? "",
|
|
...params,
|
|
}),
|
|
})
|
|
.then((res) => {
|
|
if (res.data.result === "success" && res.data.code !== "9999") {
|
|
resolve(res.data);
|
|
} else {
|
|
showToast(res.data.msg || "系统开小差了");
|
|
reject(res.data);
|
|
}
|
|
})
|
|
.catch((err) => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
|
|
export function upload(url, params) {
|
|
return new Promise((resolve, reject) => {
|
|
axios
|
|
.post(url, params, {
|
|
headers: {
|
|
"Content-Type": "multipart/form-data",
|
|
},
|
|
})
|
|
.then((res) => {
|
|
if (res.data.result === "success" && res.data.code !== "9999") {
|
|
resolve(res.data);
|
|
} else {
|
|
showToast(res.data.msg || "系统开小差了");
|
|
reject(res.data);
|
|
}
|
|
})
|
|
.catch((err) => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|