axios.ts 3.6 KB

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