游雁
2024-01-16 2ed3f46f40cd5da19cad76a97b52c46b2869d5ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import Vue from 'vue'
// import queryString from 'query-string'
import app from '@/views/app/index.vue'
import { router } from '@/router/index.js'
import store from '@/store/index.js'
// 请求对象
import Axios from 'axios'
// 项目本地组件全局注册
import globalComponents from './globalComponents'
// 项目本地全局方法
import globalFunctions from './globalFunctions'
 
// 第三方组件
// ant-design-vue 组件 按需加载
import { ConfigProvider, Input } from 'ant-design-vue'
 
Vue.use(globalComponents)
Vue.use(globalFunctions)
 
Vue.use(ConfigProvider)
Vue.use(Input)
 
const axiosInstance = Axios.create({
    timeout: 60000
})
 
// 加载项目基础样式文件
require('@/assets/css/normalize.css')
// 加载项目组件覆盖样式,全局模块样式
require('@/assets/css/index.scss')
// 轮播插件swiper样式
require('swiper/css/swiper.min.css')
 
// 设置为 false 以阻止 vue 在启动时生成生产提示
Vue.config.productionTip = false
process.env.VUE_APP_env === 'devmock' && require('../mock')
 
Vue.prototype.$axios = axiosInstance
 
// axios 配置 请求和响应拦截
axiosInstance.interceptors.request.use(
    (config) => {
        // 禁用令牌
        if (
            typeof config.headers.disabletoken !== 'undefined' &&
            config.headers.disabletoken === true
        ) {
            delete config.headers.disabletoken
            return config
        }
 
        if (
            typeof config.headers.token === 'undefined' &&
            localStorage.getItem('token') !== null
        ) {
            config.headers.token = localStorage.getItem('token')
        }
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
)
// 异常处理
axiosInstance.interceptors.response.use(
    (config) => {
        if (typeof config.headers.token !== 'undefined') {
            localStorage.setItem('token', config.headers.token)
        }
        if (
            config.data &&
            config.data.statusCode &&
            config.data.statusCode !== '00'
        ) {
            // 业务异常处理
            let msg = 'biz error, statusCode: ' + config.data.statusCode
            if (config.data.statusMsg) {
                msg = msg + ', statusMsg: ' + config.data.statusMsg
            }
 
            // 打印异常信息
            console.log(msg)
        }
        return config
    },
    (error) => {
        // 打印异常信息
        console.log(error)
 
        if (
            typeof error.response !== 'undefined' &&
            error.response.status === 401
        ) {
            console.log(error)
        }
 
        return Promise.reject(error)
    }
)
 
// 获取必要的数据
const checkNecessaryData = (callbackFun) => {
    const promiseList = []
    if (promiseList.length > 0) {
        Promise.all(promiseList)
            .then((res) => {
                localStorage.removeItem('getInitDataErrorCount')
                callbackFun && callbackFun()
            })
            .catch((res) => {
                const errorCount = localStorage.getItem(
                    'getInitDataErrorCount'
                )
                if (errorCount) {
                    let count = Number.parseInt(errorCount)
                    if (count <= 3) {
                        localStorage.setItem('getInitDataErrorCount', ++count)
                        // 菜单或者用户角色列表数据请求失败,就刷新页面
                        window.location.reload(window.location.href)
                    }
                } else {
                    localStorage.setItem('getInitDataErrorCount', 1)
                    // 菜单或者用户角色列表数据请求失败,就刷新页面
                    window.location.reload(window.location.href)
                }
            })
    } else {
        callbackFun && callbackFun()
    }
}
 
router.beforeEach((to, from, next) => {
    checkNecessaryData(() => {
        next()
    })
})
 
new Vue({
    render: (h) => h(app),
    router: router,
    store: store
}).$mount('#app')