zhouwx
2024-08-30 1f6893d24ba87313d4114c68813073ead53b2e12
src/util/request.js
@@ -1,6 +1,9 @@
import axios from "axios";//原生的axios
import Cookies from 'js-cookie';
import { Session } from '@/util/storage';
import {message} from "ant-design-vue";
import { loginOut } from "@/api/login";
import JSONBig from 'json-bigint';
//用来拦截用的
axios.defaults.headers.post["Content-Type"] = "application/json;charset=utf-8";
//创建一个单例
@@ -8,7 +11,17 @@
const { baseUrl } = require('../../config/env.' + process.env.NODE_ENV)
const http= axios.create({
    baseURL: baseUrl,
    timeout: 5000,//响应时间
    timeout: 50000,//响应时间
    transformResponse: [
        function (data) {
            // 对 data 进行任意转换处理
            try {
                return JSONBig.parse(data)
            } catch (err) {
                return data
            }
        }
    ]
    // headers:{"Content-Type":"application/json;charset=utf-8"},
})
@@ -33,42 +46,44 @@
http.interceptors.response.use(
    (response) => {
        // 对响应数据做点什么
        if (response.data.code && response.data.code === 'A0213') {
            this.$message.error('用户uid不存在');
            // useLoginApi()
            //     .signOut()
            //     .then(() => {
            //         Session.clear();
            //         window.location.href = '/';
            //     });
        if (response.data.code && response.data.code === 401) {
            // message.error('用户不存在')
            setTimeout(()=>{
                loginOut()
                    .then(() => {
                        Session.clear();
                        window.location.href = '/';
                    });
            },2000)
        } else if (response.data.code && response.data.code === 405) {
            this.$message.error('token失效');
            // useLoginApi()
            //     .signOut()
            //     .then(() => {
            //         Session.clear();
            //         window.location.href = '/';
            //     });
            message.error('token失效')
            setTimeout(()=>{
            loginOut()
                .then(() => {
                    Session.clear();
                    window.location.href = '/';
                });
            },2000)
        }
        return Promise.resolve(response);
    },
    (error) => {
        // 对响应错误做点什么
        if (error.message.indexOf('timeout') != -1) {
            this.$message.error('网络超时');
            setTimeout(() => {
                Session.clear();
                window.location.href = '/';
            }, 1000);
            message.error('网络超时')
            // setTimeout(() => {
            //     Session.clear()
            //     window.location.href = '/'
            // }, 1000)
        } else if (error.message == 'Network Error') {
            this.$message.error('网络连接错误');
            setTimeout(() => {
                Session.clear();
                window.location.href = '/';
            }, 1000);
            message.error('网络连接错误')
            // setTimeout(() => {
            //     Session.clear()
            //     window.location.href = '/'
            // }, 1000)
        } else {
            if (error.response.data) this.$message.error(error.response.data.error);
            else this.$message.error('接口路径找不到');
            if (error.response.data) message.error(error.response.data.error);
            else message.error('接口路径找不到');
        }
        return Promise.reject(error);
    }