http.ts 3.0 KB

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