axios.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /** http请求类 */
  2. import axios from "axios"
  3. import type { AxiosInstance, AxiosRequestConfig, Method, AxiosPromise } from "axios"
  4. import Nprogress from "@/plugins/nprogress"
  5. import { ElMessage } from "element-plus"
  6. import { TokenInvalidFlag, CODE401, CODE_ERR_CANCELED } from "@/libs/auth"
  7. import userStore from "@/store/user"
  8. // 重写 axios 传参
  9. interface AxiosConfigType extends AxiosRequestConfig {
  10. method: Method // method 必须为Method 格式
  11. }
  12. // axiosObj 传值
  13. type axiosObjType = {
  14. getTokenFun?: () => string | undefined // 获取token的函数
  15. tokenName?: string // headers token的名字
  16. nprogress?: boolean // 是否显示滚动条
  17. }
  18. /** api接口 */
  19. export interface axiosApiType {
  20. (...param: any[]): AxiosPromise<any>
  21. }
  22. class HttpAsynAxios {
  23. constructor(url: string, axiosObj?: axiosObjType) {
  24. this.URL = url
  25. this.axiosObj = axiosObj
  26. }
  27. private URL = ""
  28. private axiosObj: axiosObjType | undefined = undefined
  29. private httpInterceptor(instance: AxiosInstance) {
  30. ;(this.axiosObj ? !(this.axiosObj.nprogress === false) : true) && Nprogress.start() // 开启
  31. // http request 请求拦截器
  32. instance.interceptors.request.use(
  33. config => {
  34. return config
  35. },
  36. error => {
  37. console.log(error)
  38. Nprogress.done()
  39. const rejectData: apiResDataType = {
  40. code: 500,
  41. message: "系统运行异常,请联系管理员处理",
  42. data: null
  43. }
  44. return Promise.reject(rejectData)
  45. }
  46. )
  47. // http response 结果拦截器
  48. instance.interceptors.response.use(
  49. response => {
  50. Nprogress.done()
  51. // 如果返回401则跳转到登录页
  52. if (response.data.code === CODE401) {
  53. // 如果token登录状态 才提示和退出
  54. if (TokenInvalidFlag) {
  55. // const responseData: apiResDataType = response.data
  56. // ElMessage.error(responseData.message)
  57. // 登出
  58. userStore().resetUser()
  59. }
  60. }
  61. return response
  62. },
  63. error => {
  64. console.log(error)
  65. Nprogress.done()
  66. // if (error.response) {
  67. // // 响应错误之后的操作
  68. // switch (error.response.status) {
  69. // case 401:
  70. // }
  71. // }
  72. const rejectData: apiResDataType =
  73. error.code === CODE_ERR_CANCELED
  74. ? {
  75. code: error.code,
  76. message: "系统取消接口",
  77. data: null
  78. }
  79. : {
  80. code: 500,
  81. message: "系统运行异常,请联系管理员处理",
  82. data: null
  83. }
  84. return Promise.reject(rejectData) // 返回接口返回的错误信息
  85. }
  86. )
  87. }
  88. // 创建实例
  89. private createInstance() {
  90. const axiosObj = this.axiosObj
  91. return axios.create(
  92. axiosObj?.tokenName
  93. ? {
  94. baseURL: this.URL,
  95. headers: {
  96. [axiosObj.tokenName]: axiosObj.getTokenFun && axiosObj.getTokenFun()
  97. }
  98. }
  99. : {
  100. baseURL: this.URL
  101. }
  102. )
  103. }
  104. /**
  105. axios请求方法
  106. ```
  107. {
  108. data: data, //get方法的时候为params:data
  109. method: "post",
  110. url: '/auth/loginIn',
  111. headers:{
  112. 'Content-Type':'application/json',
  113. },
  114. responseType: 'blob',
  115. }
  116. ```
  117. */
  118. axioseRquest(opt: AxiosConfigType) {
  119. const instance = this.createInstance()
  120. this.httpInterceptor(instance)
  121. return instance(opt)
  122. }
  123. }
  124. export default HttpAsynAxios