Admin
2022-08-20 006cedaa4188d5bd5f99ef0721572a24c89d7ce4
src/utils/request.ts
@@ -1,27 +1,28 @@
import axios from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Session } from '/@/utils/storage';
import JSONbig from 'json-bigint';
import Cookies from 'js-cookie';
import { useLoginApi } from '/@/api/login';
import { Session } from '/@/utils/storage';
// var jsonBig = require('json-bigint')({ "storeAsString": true });
// 配置新建一个 axios 实例
const service = axios.create({
    baseURL: import.meta.env.VITE_API_URL as any,
    // baseURL: import.meta.env.VITE_API_URL as any,
    timeout: 50000,
    headers: { 'Content-Type': 'application/json' }
});
service.interceptors.request.use(
    (config) => {
        // 在发送请求之前做些什么 token
        // for (let key in config.data) {
        //     if (config.data[key] == null || config.data[key] === undefined) {
        //         delete config.data[key];
        //     }
        // }
        if (Session.get('token')) {
            (<any>config.headers).common['Authorization'] = `${Session.get('token')}`;
            (<any>config.headers).common['uid'] = `${Session.get('uid')}`;
        for (let key in config.data) {
            if (config.data[key] == '' && config.data[key] !== 0) {
                config.data[key] = null;
            }
        }
        if (Cookies.get('token')) {
            (<any>config.headers).common['Authorization'] = `${Cookies.get('token')}`;
            (<any>config.headers).common['uid'] = `${Cookies.get('uid')}`;
        }
        return config;
    },
@@ -51,16 +52,21 @@
        // 对响应数据做点什么
        if (response.data.code && response.data.code === 'A0213') {
            ElMessage.error('用户uid不存在');
            setTimeout(() => {
                Session.clear();
                window.location.href = '/';
            }, 2000);
            useLoginApi()
                .signOut()
                .then(() => {
                    Session.clear();
                    window.location.href = '/';
                });
        } else if (response.data.code && response.data.code === 'A0215') {
            ElMessage.error('token失效');
            setTimeout(() => {
                Session.clear();
                window.location.href = '/';
            }, 2000);
            // logOut;
            useLoginApi()
                .signOut()
                .then(() => {
                    Session.clear();
                    window.location.href = '/';
                });
        }
        // if(response.data.code && response.data.code !== '200'){
        return Promise.resolve(response);
@@ -88,8 +94,16 @@
        // 对响应错误做点什么
        if (error.message.indexOf('timeout') != -1) {
            ElMessage.error('网络超时');
            setTimeout(() => {
                Session.clear();
                window.location.href = '/';
            }, 1000);
        } else if (error.message == 'Network Error') {
            ElMessage.error('网络连接错误');
            setTimeout(() => {
                Session.clear();
                window.location.href = '/';
            }, 1000);
        } else {
            if (error.response.data) ElMessage.error(error.response.data.error);
            else ElMessage.error('接口路径找不到');