index.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import useUserStore from '@/store/modules/user'
  2. import { ApiInstance } from './type'
  3. import axios from 'axios'
  4. // import qs from 'qs'
  5. import Message from 'vue-m-message'
  6. // 导入 API 模块
  7. import appApi from './modules/app'
  8. import userApi from './modules/user'
  9. import roleApi from './modules/role'
  10. import institutionApi from './modules/institution'
  11. import areaApi from "./modules/area"
  12. import operationApi from './modules/operation.ts'
  13. import menuApi from './modules/menu.ts'
  14. import commonApi from "./modules/common.ts";
  15. import teamApi from "@/api/modules/team.ts";
  16. import representativeApi from "@/api/modules/representative.ts";
  17. import contentApi from "@/api/modules/content.ts";
  18. // import { useRouter } from 'vue-router'
  19. // const router = useRouter()
  20. import router from "@/router"
  21. import lotteryApi from '@/api/modules/lottery.ts';
  22. const axiosInstance:ApiInstance = axios.create({
  23. baseURL: (import.meta.env.DEV && import.meta.env.VITE_OPEN_PROXY === 'true') ? '/proxy/' : import.meta.env.VITE_APP_API_BASEURL,
  24. timeout: 1000 * 60,
  25. responseType: 'json',
  26. })
  27. axiosInstance.interceptors.request.use(
  28. (request) => {
  29. // 全局拦截请求发送前提交的参数
  30. const userStore = useUserStore()
  31. // 设置请求头
  32. if (request.headers) {
  33. if (userStore.isLogin) {
  34. request.headers.Authorization = userStore.token
  35. }
  36. }
  37. // 是否将 POST 请求参数进行字符串化处理
  38. if (request.method === 'post') {
  39. // request.data = qs.stringify(request.data, {
  40. // arrayFormat: 'brackets',
  41. // })
  42. }
  43. return request
  44. },
  45. )
  46. axiosInstance.interceptors.response.use(
  47. (response) => {
  48. /**
  49. * 全局拦截请求发送后返回的数据,如果数据有报错则在这做全局的错误提示
  50. * 假设返回数据格式为:{ status: 1, error: '', data: '' }
  51. * 规则是当 status 为 1 时表示请求成功,为 0 时表示接口需要登录或者登录状态失效,需要重新登录
  52. * 请求出错时 error 会返回错误信息
  53. */
  54. if (response.data.code === 500) {
  55. if (response.data.msg !== '') {
  56. // 错误提示
  57. Message.error(response.data.msg, {
  58. zIndex: 2000,
  59. })
  60. return Promise.resolve(response.data)
  61. }
  62. }
  63. else {
  64. if (response.data.code === 401) {
  65. // console.log(router)
  66. localStorage.setItem("token",'')
  67. // router.push({
  68. // path:"/login"
  69. // })
  70. // console.log(localStorage.getItem('token'))
  71. Message({
  72. message: '登录信息已过期,请重新登录!!!',
  73. type: 'error',
  74. duration: 2000,
  75. })
  76. // useUserStore().logout()
  77. }
  78. // useUserStore().logout()
  79. }
  80. return Promise.resolve(response.data)
  81. },
  82. (error) => {
  83. let message = error.message
  84. if (message === 'Network Error') {
  85. message = '后端网络故障'
  86. }
  87. else if (message.includes('timeout')) {
  88. message = '接口请求超时'
  89. }
  90. else if (message.includes('Request failed with status code')) {
  91. message = `接口${message.substr(message.length - 3)}异常`
  92. }
  93. Message.error(message, {
  94. zIndex: 2000,
  95. })
  96. return Promise.reject(error)
  97. },
  98. )
  99. // API 模块
  100. const api = {
  101. appApi: appApi(axiosInstance),
  102. userApi: userApi(axiosInstance),
  103. roleApi: roleApi(axiosInstance),
  104. institutionApi:institutionApi(axiosInstance),
  105. areaApi:areaApi(axiosInstance),
  106. operationApi: operationApi(axiosInstance),
  107. menuApi:menuApi(axiosInstance),
  108. commonApi: commonApi(axiosInstance),
  109. teamApi: teamApi(axiosInstance),
  110. representativeApi: representativeApi(axiosInstance),
  111. lotteryApi: lotteryApi(axiosInstance),
  112. contentApi:contentApi(axiosInstance)
  113. }
  114. export default api