http.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import axios from 'axios'
  2. import { getTokenInfo, removeTokenInfo } from './storage'
  3. import store from '@/store'
  4. import { MessageFu } from './message'
  5. import { domShowFu } from './domShow'
  6. import { loginOutFu } from '@/components/DingLogin/data'
  7. import history from './history'
  8. // 是否是钉钉登录
  9. export const isDing = isDingTemp
  10. export const envFlag = process.env.NODE_ENV === 'development'
  11. const baseUrlTemp = baseUrlTempOne // 测试环境
  12. // export const baseUrlTemp = 'http://192.168.20.61:8096' // 线下环境
  13. const baseFlag = baseUrlTemp.includes('https://') || !envFlag
  14. // 请求基地址
  15. export const baseURL = envFlag ? `${baseUrlTemp}${baseFlag ? '' : '/api/'}` : baseUrlTemp
  16. // 处理 类型“AxiosResponse<any, any>”上不存在属性“code”
  17. declare module 'axios' {
  18. interface AxiosResponse {
  19. code: number
  20. timestamp: string
  21. // 这里追加你的参数
  22. }
  23. }
  24. // 创建 axios 实例
  25. const http = axios.create({
  26. baseURL: `${baseURL}${baseFlag ? '/api/' : ''}`,
  27. timeout: 30000
  28. })
  29. let axajInd = 0
  30. // 请求拦截器
  31. http.interceptors.request.use(
  32. function (config: any) {
  33. // if (isMyAlert) alert(`开始发送接口:${JSON.stringify(config)}`)
  34. // 发请求前打开加载提示
  35. domShowFu('#AsyncSpinLoding', true)
  36. axajInd++
  37. const { token } = getTokenInfo()
  38. // console.log(token)
  39. // 如果 token 存在,将其作为查询参数添加到 URL
  40. // if (token) {
  41. // // 检查 URL 是否已有查询参数
  42. // const separator = config.url.includes('?') ? '&' : '?'
  43. // config.url = `${config.url}${separator}tkToken=${encodeURIComponent(token)}`
  44. // }
  45. if (token) config.headers.token = token
  46. return config
  47. },
  48. function (err) {
  49. return Promise.reject(err)
  50. }
  51. )
  52. let timeId = -1
  53. // 响应拦截器
  54. http.interceptors.response.use(
  55. function (response) {
  56. // if (isMyAlert) alert(`发送请求成功:${JSON.stringify(response)}`)
  57. // 请求回来的关闭加载提示
  58. axajInd--
  59. if (axajInd === 0) {
  60. domShowFu('#AsyncSpinLoding', false)
  61. }
  62. if (response.data.code === 5001 || response.data.code === 5002) {
  63. removeTokenInfo()
  64. loginOutFu()
  65. clearTimeout(timeId)
  66. // timeId = window.setTimeout(() => {
  67. // MessageFu.warning('登录失效!')
  68. // }, 200)
  69. } else if (response.data.code === 0) {
  70. // MessageFu.success(response.data.msg);
  71. } else if (![3014, 3101, 3510].includes(response.data.code)) {
  72. if (response.data.code === -1 && response.data.msg === '对象不存在') {
  73. history.replace('/abnormal?k=5')
  74. } else MessageFu.warning(response.data.msg)
  75. }
  76. if (response.data.code === 3510) history.replace('/abnormal')
  77. return response.data
  78. },
  79. async function (err) {
  80. // if (isMyAlert) alert(`发送请求失败:${err}`)
  81. clearTimeout(timeId)
  82. timeId = window.setTimeout(() => {
  83. axajInd = 0
  84. domShowFu('#AsyncSpinLoding', false)
  85. // 如果因为网络原因,response没有,给提示消息
  86. if (!err.response) {
  87. if (store.getState().A0Layout.closeUpFile.state) MessageFu.warning('取消上传!')
  88. else {
  89. MessageFu.error('网络繁忙,请稍后重试!')
  90. history.replace('/abnormal?k=2')
  91. }
  92. } else {
  93. if (
  94. err.response &&
  95. err.response.data &&
  96. err.response.data.msg &&
  97. err.response.data.msg.length < 30
  98. ) {
  99. MessageFu.error(err.response.data.msg)
  100. // 没有权限
  101. if (err.response.data.code === 5003) {
  102. removeTokenInfo()
  103. loginOutFu()
  104. } else history.replace('/abnormal?k=1')
  105. } else {
  106. MessageFu.error('响应错误,请联系管理员!')
  107. history.replace('/abnormal?k=1')
  108. }
  109. }
  110. }, 100)
  111. return Promise.reject(err)
  112. }
  113. )
  114. // 导出 axios 实例
  115. export default http