index.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import axios from 'axios'
  2. import type { AxiosInstance, AxiosError, AxiosRequestConfig, InternalAxiosRequestConfig, AxiosResponse } from 'axios'
  3. import { ElMessage, ElLoading, ElNotification } from 'element-plus'
  4. import { ResultEnum, ResultData, ContentType } from './interface/index'
  5. import { GlobalStore } from '@/stores/index'
  6. import router from '@/router/index'
  7. type LoadingType = {
  8. close?: () => void
  9. }
  10. let loadingInstance: LoadingType = {}
  11. const config = {
  12. // 默认地址请求地址,可在 .env.*** 文件中修改
  13. // baseURL: import.meta.env.VITE_BZD_ERP_APP_API as string,
  14. // 设置超时时间(10s)
  15. timeout: ResultEnum.TIMEOUT as number,
  16. // 跨域时候允许携带凭证
  17. withCredentials: true,
  18. headers: {
  19. 'Content-Type': ContentType.URLENCODED
  20. }
  21. }
  22. class RequestHttp {
  23. service: AxiosInstance
  24. public constructor(config: AxiosRequestConfig) {
  25. // 实例化axios
  26. this.service = axios.create(config)
  27. /**
  28. * @description 请求拦截器
  29. * 客户端发送请求 -> [请求拦截器] -> 服务器
  30. * token校验(JWT) : 接受服务器返回的token,存储到vuex/pinia/本地储存当中
  31. */
  32. this.service.interceptors.request.use(
  33. (config: InternalAxiosRequestConfig) => {
  34. // const globalStore = GlobalStore()
  35. // 开始loading
  36. // if (!globalStore.GET_isloading) {
  37. // loadingInstance = ElLoading.service({
  38. // lock: true,
  39. // text: 'Loading',
  40. // background: 'rgba(0, 0, 0, 0.7)'
  41. // })
  42. // }
  43. // 加入token
  44. // if (globalStore.GET_User_tokey) {
  45. // config.headers['User_tokey'] = globalStore.GET_User_tokey
  46. // }
  47. return config
  48. },
  49. (err: any) => err
  50. )
  51. /**
  52. * @description 响应拦截器
  53. * 服务器换返回信息 -> [拦截统一处理] -> 客户端JS获取到信息
  54. */
  55. this.service.interceptors.response.use(
  56. (response: AxiosResponse) => {
  57. const { data } = response
  58. const globalStore = GlobalStore()
  59. // * 在请求结束后,并关闭请求 loading
  60. // loadingInstance.close && loadingInstance.close()
  61. // 无权限访问 || 登录密码错误(code == 202)
  62. // if (data.Code === ResultEnum.ROLE) {
  63. // return Promise.reject(data)
  64. // }
  65. // * 登陆失效(code == 201)
  66. if (data.Code === ResultEnum.OVERDUE) {
  67. ElNotification.error({
  68. title: '登陆失效',
  69. message: data.Msg,
  70. position: 'bottom-right'
  71. })
  72. globalStore.SET_User_Tokey('')
  73. localStorage.clear()
  74. router.replace('/login')
  75. return Promise.reject(data)
  76. }
  77. // * 全局错误信息拦截(防止下载文件得时候返回数据流,没有code,直接报错)
  78. if (data.Code && data.Code !== ResultEnum.SUCCESS) {
  79. ElMessage.error(data.Msg)
  80. return Promise.reject(data)
  81. }
  82. // * 成功请求(在页面上除非特殊情况,否则不用在页面处理失败逻辑)
  83. return data
  84. },
  85. async (error: AxiosError) => {
  86. loadingInstance.close && loadingInstance.close()
  87. // 请求超时 && 网络错误单独判断,没有 response
  88. if (error.message.indexOf('timeout') !== -1) ElMessage.error('请求超时!请您稍后重试')
  89. if (error.message.indexOf('Network Error') !== -1) ElMessage.error('网络错误!请您稍后重试')
  90. return Promise.reject(error)
  91. }
  92. )
  93. }
  94. // * 常用请求方法封装
  95. get<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> {
  96. return this.service.get(url, { params, ..._object })
  97. }
  98. post<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> {
  99. return this.service.post(url, params, _object)
  100. }
  101. put<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> {
  102. return this.service.put(url, params, _object)
  103. }
  104. delete<T>(url: string, params?: any, _object = {}): Promise<ResultData<T>> {
  105. return this.service.delete(url, { params, ..._object })
  106. }
  107. }
  108. export default new RequestHttp(config)