axios.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // *** 转换msg为message 正常是没有的 这里2个系统所以合并一下
  51. response.data.msg && (response.data.message = response.data.msg)
  52. //如果返回401则跳转到登录页
  53. if (response.data.code === CODE401) {
  54. //如果token登录状态 才提示和退出
  55. if (TokenInvalidFlag) {
  56. const responseData: apiResDataType = response.data
  57. ElMessage.error(responseData.message)
  58. //登出
  59. userStore().resetUser()
  60. }
  61. }
  62. return response
  63. },
  64. error => {
  65. console.log(error)
  66. Nprogress.done()
  67. // if (error.response) {
  68. // // 响应错误之后的操作
  69. // switch (error.response.status) {
  70. // case 401:
  71. // }
  72. // }
  73. const rejectData: apiResDataType = {
  74. code: 500,
  75. message: "系统运行异常,请刷新页面",
  76. data: null
  77. }
  78. return Promise.reject(rejectData) // 返回接口返回的错误信息
  79. }
  80. )
  81. }
  82. //创建实例
  83. private createInstance() {
  84. const axiosObj = this.axiosObj
  85. return axios.create(
  86. axiosObj?.tokenName
  87. ? {
  88. baseURL: this.URL,
  89. headers: {
  90. [axiosObj.tokenName]: axiosObj.getTokenFun && axiosObj.getTokenFun()
  91. }
  92. }
  93. : {
  94. baseURL: this.URL
  95. }
  96. )
  97. }
  98. /**
  99. axios请求方法
  100. ```
  101. {
  102. data: data, //get方法的时候为params:data
  103. method: "post",
  104. url: '/auth/loginIn',
  105. headers:{
  106. 'Content-Type':'application/json',
  107. },
  108. responseType: 'blob',
  109. }
  110. ```
  111. */
  112. axioseRquest(opt: AxiosConfigType) {
  113. const instance = this.createInstance()
  114. this.httpInterceptor(instance)
  115. return instance(opt)
  116. }
  117. }
  118. export default HttpAsynAxios