123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import axios from "axios";
- import router from "@/router";
- import { ElLoading, ElMessage, ElMessageBox } from "element-plus";
- import { getToken, setToken, removeToken } from "@/utils/auth";
- import { AxiosConfigTy, AxiosReqTy, ObjTy } from "~/common";
- import { useUserStore } from "@/store/user";
- let reqConfig: any;
- let loadingE: any;
- const service: any = axios.create();
- // 请求拦截
- service.interceptors.request.use(
- (request: AxiosReqTy) => {
- const authMap = useUserStore().authMap;
- const data = request.data!;
- if (data.btnAuth) {
- if (authMap[data.btnAuth]) {
- request.headers["auth-id"] = authMap[data.btnAuth];
- sessionStorage.setItem("auth-id", authMap[data.btnAuth]);
- }
- delete data.btnAuth;
- } else {
- const route = router.currentRoute;
- if (!data.id || data.id !== DATACONTENT_ID.loginId) {
- const role = route.value.meta?.roles?.[0];
- if (role && authMap[role]) {
- request.headers["auth-id"] = authMap[role];
- }
- }
- }
- // token setting
- request.headers["token"] = getToken();
- /* download file*/
- if (request.isDownLoadFile) {
- request.responseType = "blob";
- }
- /* upload file*/
- if (request.isUploadFile) {
- request.headers["Content-Type"] = "application/json";
- }
- reqConfig = request;
- if (request.bfLoading) {
- loadingE = ElLoading.service({
- lock: true,
- text: "数据载入中",
- // spinner: 'el-icon-ElLoading',
- background: "rgba(0, 0, 0, 0.1)",
- });
- }
- /*
- *params会拼接到url上
- * */
- if (request.isParams) {
- request.params = request.data;
- request.data = {};
- }
- return request;
- },
- (err: any) => {
- Promise.reject(err);
- }
- );
- // 响应拦截
- service.interceptors.response.use(
- (response) => {
- const res = response.data;
- // if the custom code is not 20000, it is judged as an error.
- if (res.code != "0") {
- if (res.message && res.message.includes("Duplicate entry")) {
- ElMessage({
- message: "执行失败",
- type: "error",
- duration: 5 * 1000,
- });
- } else {
- ElMessage({
- message: res.message ?? "执行失败",
- type: "error",
- duration: 5 * 1000,
- });
- }
- return Promise.reject(res.message || res.returnData?.message || "Error");
- } else {
- return res;
- }
- },
- (error) => {
- const des500 = `${error}`.split(" ").includes("500");
- if (des500) {
- ElMessage({
- message: "身份令牌过期或失效,即将重新登录",
- type: "error",
- duration: 3 * 1000,
- onClose: async () => {
- sessionStorage.clear();
- removeToken(); // must remove token first
- removeToken("codeToken");
- removeToken("systemSet");
- removeToken("userid");
- removeToken("auth-id");
- // store.dispatch('app/toggleOutflag', false)
- // await store.dispatch('user/resetToken')
- useUserStore().resetState();
- router.push(`/login`);
- location.reload();
- },
- });
- } else {
- ElMessage({
- message: error.message,
- type: "error",
- duration: 5 * 1000,
- });
- }
- return Promise.reject(error);
- }
- );
- export function axiosReq({
- url,
- data,
- method,
- headers,
- isParams,
- bfLoading,
- afHLoading,
- isUploadFile,
- isDownLoadFile,
- baseURL,
- timeout,
- isAlertErrorMsg = true,
- }: AxiosConfigTy): any {
- return service({
- url: url,
- method: method ?? "get",
- data: data ?? {},
- headers: headers ?? {},
- isParams: isParams ?? false,
- bfLoading: bfLoading ?? false,
- afHLoading: afHLoading ?? true,
- isUploadFile: isUploadFile ?? false,
- isDownLoadFile: isDownLoadFile ?? false,
- isAlertErrorMsg: isAlertErrorMsg,
- // baseURL: baseURL ?? import.meta.env.VITE_APP_BASE_URL,
- baseURL: baseURL ?? PLATFROM_CONFIG.baseNewUrl,
- timeout: timeout ?? 15000,
- });
- }
- export default axiosReq;
|