http.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import axios from 'axios'
  2. import history from './history'
  3. import { getTokenInfo, removeTokenInfo } from './storage'
  4. import store from '@/store'
  5. import { MessageFu } from './message'
  6. import { domShowFu } from './domShow'
  7. import { isLoginAPI } from '@/store/action/layout'
  8. const envFlag = process.env.NODE_ENV === 'development'
  9. const baseUrlTemp2 = 'https://sit-kelamayi.4dage.com' // 测试环境
  10. // const baseUrlTemp1 = 'http://192.168.20.61:8072' // 开发环境
  11. const baseUrlTemp = window.location.origin
  12. console.log(baseUrlTemp)
  13. // const baseFlag = baseUrlTemp.includes('https://')
  14. // 请求基地址
  15. export const baseURL = envFlag ? baseUrlTemp : baseUrlTemp
  16. // export const baseURL = baseUrlTemp2
  17. // 处理 类型"AxiosResponse<any, any>"上不存在属性"code"
  18. declare module 'axios' {
  19. interface AxiosResponse {
  20. code: number
  21. timestamp: string
  22. // 这里追加你的参数
  23. }
  24. }
  25. // 创建 axios 实例
  26. const http = axios.create({
  27. baseURL: baseURL + '/api/',
  28. timeout: 10000
  29. })
  30. let axajInd = 0
  31. // 请求拦截器
  32. http.interceptors.request.use(
  33. config => {
  34. domShowFu('#AsyncSpinLoding', true)
  35. axajInd++
  36. const token = getTokenInfo()
  37. if (token) config.headers.token = token.token
  38. return config
  39. },
  40. function (err) {
  41. return Promise.reject(err)
  42. }
  43. )
  44. let timeId = -1
  45. // 响应拦截器
  46. http.interceptors.response.use(
  47. function (response) {
  48. // 请求回来的关闭加载提示
  49. axajInd--
  50. if (axajInd === 0) {
  51. domShowFu('#AsyncSpinLoding', false)
  52. }
  53. if (response.data.code === 500) {
  54. isLoginAPI()
  55. }
  56. if (response.data.code === 4008) {
  57. removeTokenInfo()
  58. history.push('/login')
  59. clearTimeout(timeId)
  60. timeId = window.setTimeout(() => {
  61. MessageFu.warning('登录失效!')
  62. }, 200)
  63. } else if (response.data.code === 0) {
  64. // MessageFu.success(response.data.msg);
  65. } else if (response.data.code === 4006) {
  66. // 密码错误
  67. } else if (response.data.code === 4004) {
  68. // 用户名重复
  69. } else if (!!response.data === false) {
  70. return response
  71. } else if (response.data.code !== 0) MessageFu.warning(response.data.message)
  72. return response.data
  73. },
  74. async function (err) {
  75. clearTimeout(timeId)
  76. timeId = window.setTimeout(() => {
  77. axajInd = 0
  78. domShowFu('#AsyncSpinLoding', false)
  79. // 如果因为网络原因,response没有,给提示消息
  80. if (!err.response) {
  81. if (store.getState().A0Layout.closeUpFile.state) MessageFu.warning('取消上传!')
  82. else MessageFu.error('网络繁忙,请稍后重试!')
  83. } else {
  84. if (
  85. err.response &&
  86. err.response.data &&
  87. err.response.data.msg &&
  88. err.response.data.msg.length < 30
  89. ) {
  90. MessageFu.error(err.response.data.msg)
  91. // 没有权限
  92. if (err.response.data.code === 4002) {
  93. removeTokenInfo()
  94. history.push('/login')
  95. }
  96. } else MessageFu.error('响应错误,请联系管理员!')
  97. }
  98. }, 100)
  99. return Promise.reject(err)
  100. }
  101. )
  102. // 导出 axios 实例
  103. export default http