request.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import axios from 'axios'
  2. import {
  3. MessageBox,
  4. Message
  5. } from 'element-ui'
  6. import store from '@/store'
  7. import {
  8. getToken
  9. } from '@/utils/auth'
  10. // create an axios instance
  11. const service = axios.create({
  12. baseURL: 'https://ht.9026.com/', // url = base url + request url
  13. // withCredentials: true, // send cookies when cross-domain requests
  14. timeout: 10 * 10000, // request timeout
  15. method: 'post',
  16. headers: {
  17. 'Content-Type': 'application/json'
  18. }
  19. })
  20. service.interceptors.request.use(
  21. config => {
  22. const token = getToken()
  23. if (token) {
  24. config.headers.Authorization = token
  25. }
  26. return config
  27. },
  28. (error) => Promise.reject(error)
  29. )
  30. // // request interceptor
  31. // service.interceptors.request.use(
  32. // config => {
  33. // // do something before request is sent
  34. // config.headers['Content-Type'] = 'application/json'
  35. // // config.headers['Content-Type']='charset=utf-8'
  36. // config.headers = {
  37. // 'User-Agent-type': 'apifox/1.0.0 (https://www.apifox.cn)'
  38. // }
  39. // const string = getToken()
  40. // if (string) {
  41. // const obj = JSON.parse(string)
  42. // console.log(obj.token, 'obj')
  43. // config.headers['Authorization'] = obj.token
  44. // }
  45. // if (store.getters.token) {
  46. // // let each request carry token
  47. // // ['X-Token'] is a custom headers key
  48. // // please modify it according to the actual situation
  49. // const string = getToken()
  50. // const obj = eval('(' + string + ')')
  51. // config.headers['Authorization'] = obj.token
  52. // console.log(obj.token)
  53. // // config.headers['Authorization'] = getToken()
  54. // }
  55. // return config
  56. // },
  57. // error => {
  58. // // do something with request error
  59. // console.log(error) // for debug
  60. // return Promise.reject(error)
  61. // }
  62. // )
  63. // response interceptor
  64. service.interceptors.response.use(
  65. /**
  66. * If you want to get http information such as headers or status
  67. * Please return response => response
  68. */
  69. /**
  70. * Determine the request status by custom code
  71. * Here is just an example
  72. * You can also judge the status by HTTP Status Code
  73. */
  74. response => {
  75. const res = response.data
  76. // if the custom code is not 20000, it is judged as an error.
  77. console.log(response,"response")
  78. console.log(res,"res")
  79. // if (res.code !== 200) {
  80. // Message({
  81. // message: res.msg || 'Error',
  82. // type: 'error',
  83. // duration: 5 * 1000
  84. // })
  85. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  86. // if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
  87. // // to re-login
  88. // MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
  89. // confirmButtonText: 'Re-Login',
  90. // cancelButtonText: 'Cancel',
  91. // type: 'warning'
  92. // }).then(() => {
  93. // store.dispatch('user/resetToken').then(() => {
  94. // location.reload()
  95. // })
  96. // })
  97. // }
  98. // return Promise.reject(new Error(res.msg || 'Error'))
  99. // } else {
  100. // return res
  101. // }
  102. if(res.type==="application/x-zip-compressed"){
  103. return response
  104. }else if(res.code !== 200){
  105. Message({
  106. message: res.msg || 'Error',
  107. type: 'error',
  108. duration: 5 * 1000
  109. })
  110. }else{
  111. return res
  112. }
  113. },
  114. error => {
  115. console.log('err' + error) // for debug
  116. Message({
  117. message: error.msg,
  118. type: 'error',
  119. duration: 5 * 1000
  120. })
  121. return Promise.reject(error)
  122. }
  123. )
  124. export default service