main.js 4.0 KB

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