request.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. import {Loading} from 'element-ui'
  11. const loading={
  12. loadingInstance:null,
  13. // 打开加载
  14. open(){
  15. if(this.loadingInstance===null){
  16. this.loadingInstance=Loading.service({
  17. text:'加载中...', //加载图标下的文字
  18. spinner:'el-icon-loading', //加载图标
  19. // background:'rgba(0,0,0,0.8)', //背景色
  20. customClass:'loading' //自定义样式的类名
  21. })
  22. }
  23. },
  24. // 关闭加载
  25. close(){
  26. if(this.loadingInstance!==null){
  27. this.loadingInstance.close()
  28. }
  29. this.loadingInstance=null
  30. }
  31. }
  32. // create an axios instance
  33. const service = axios.create({
  34. baseURL: ' https://ht.9026.com', // url = base url + request url
  35. // withCredentials: true, // send cookies when cross-domain requests
  36. timeout: 10 * 10000, // request timeout
  37. method: 'post',
  38. headers: {
  39. 'Content-Type': 'application/json'
  40. }
  41. })
  42. service.interceptors.request.use(
  43. config => {
  44. loading.open()
  45. const token = getToken()
  46. if (token) {
  47. config.headers.Authorization = token
  48. }
  49. return config
  50. },
  51. (error) => Promise.reject(error)
  52. )
  53. // // request interceptor
  54. // service.interceptors.request.use(
  55. // config => {
  56. // // do something before request is sent
  57. // config.headers['Content-Type'] = 'application/json'
  58. // // config.headers['Content-Type']='charset=utf-8'
  59. // config.headers = {
  60. // 'User-Agent-type': 'apifox/1.0.0 (https://www.apifox.cn)'
  61. // }
  62. // const string = getToken()
  63. // if (string) {
  64. // const obj = JSON.parse(string)
  65. // console.log(obj.token, 'obj')
  66. // config.headers['Authorization'] = obj.token
  67. // }
  68. // if (store.getters.token) {
  69. // // let each request carry token
  70. // // ['X-Token'] is a custom headers key
  71. // // please modify it according to the actual situation
  72. // const string = getToken()
  73. // const obj = eval('(' + string + ')')
  74. // config.headers['Authorization'] = obj.token
  75. // console.log(obj.token)
  76. // // config.headers['Authorization'] = getToken()
  77. // }
  78. // return config
  79. // },
  80. // error => {
  81. // // do something with request error
  82. // console.log(error) // for debug
  83. // return Promise.reject(error)
  84. // }
  85. // )
  86. // response interceptor
  87. service.interceptors.response.use(
  88. /**
  89. * If you want to get http information such as headers or status
  90. * Please return response => response
  91. */
  92. /**
  93. * Determine the request status by custom code
  94. * Here is just an example
  95. * You can also judge the status by HTTP Status Code
  96. */
  97. response => {
  98. loading.close()
  99. const res = response.data
  100. // if the custom code is not 20000, it is judged as an error.
  101. // if (res.code !== 200) {
  102. // Message({
  103. // message: res.msg || 'Error',
  104. // type: 'error',
  105. // duration: 5 * 1000
  106. // })
  107. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  108. // if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
  109. // // to re-login
  110. // MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
  111. // confirmButtonText: 'Re-Login',
  112. // cancelButtonText: 'Cancel',
  113. // type: 'warning'
  114. // }).then(() => {
  115. // store.dispatch('user/resetToken').then(() => {
  116. // location.reload()
  117. // })
  118. // })
  119. // }
  120. // return Promise.reject(new Error(res.msg || 'Error'))
  121. // } else {
  122. // return res
  123. // }
  124. if(res.type==="application/x-zip-compressed" ||
  125. res.type==="application/vnd.ms-excel"||res.type === 'application/json'){
  126. return response
  127. }else if(res.code != 200){
  128. Message({
  129. message: res.msg || 'Error',
  130. type: 'error',
  131. duration: 5 * 1000
  132. })
  133. }else{
  134. return res
  135. }
  136. },
  137. error => {
  138. console.log('err' + error) // for debug
  139. Message({
  140. message: error.msg,
  141. type: 'error',
  142. duration: 5 * 1000
  143. })
  144. loading.close()
  145. return Promise.reject(error)
  146. }
  147. )
  148. export default service