request.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import ElementUI from 'element-ui'
  2. import axios from 'axios'
  3. import { Message } from 'element-ui'
  4. import store from '@/store'
  5. import { getToken } from '@/utils/auth'
  6. // import { Loading } from 'element-ui'
  7. import load from '@/utils/loading'
  8. import router from '@/router/index'
  9. import Vue from 'vue'
  10. const showMessage = Symbol('showMessage')
  11. class DonMessage {
  12. success (options, single = true) {
  13. this[showMessage]('success', options, single)
  14. }
  15. warning (options, single = true) {
  16. this[showMessage]('warning', options, single)
  17. }
  18. info (options, single = true) {
  19. this[showMessage]('info', options, single)
  20. }
  21. error (options, single = true) {
  22. this[showMessage]('error', options, single)
  23. }
  24. [showMessage] (type, options, single) {
  25. if (single) {
  26. // 判断是否已存在Message
  27. if (document.getElementsByClassName('el-message').length === 0) {
  28. Message[type](options)
  29. }
  30. } else {
  31. Message[type](options)
  32. }
  33. }
  34. }
  35. Vue.use(ElementUI)
  36. // 命名根据需要,DonMessage只是在文章中使用
  37. Vue.prototype.$message = new DonMessage()
  38. let vue = new Vue()
  39. // let loading //定义loading变量
  40. // function startLoading () { //使用Element loading-start 方法
  41. // loading = Loading.service({
  42. // lock: true,
  43. // fullscreen: true,
  44. // text: '加载中……',
  45. // background: 'rgba(0, 0, 0, 0.7)'
  46. // })
  47. // }
  48. // function endLoading () {
  49. // //使用Element loading-close 方法
  50. // loading.close();
  51. // }
  52. //那么 showFullScreenLoading() tryHideFullScreenLoading() 要干的事儿就是将同一时刻的请求合并。
  53. //声明一个变量 needLoadingRequestCount,每次调用showFullScreenLoading方法 needLoadingRequestCount + 1。
  54. //调用tryHideFullScreenLoading()方法,needLoadingRequestCount - 1。needLoadingRequestCount为 0 时,结束 loading。
  55. let needLoadingRequestCount = 0
  56. function showFullScreenLoading () {
  57. if (needLoadingRequestCount === 0) {
  58. load.startLoading()
  59. }
  60. needLoadingRequestCount++
  61. }
  62. function tryHideFullScreenLoading () {
  63. if (needLoadingRequestCount <= 0) return
  64. needLoadingRequestCount--
  65. if (needLoadingRequestCount === 0) {
  66. load.endLoading();
  67. }
  68. }
  69. // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  70. // create an axios instance
  71. const service = axios.create({
  72. baseURL: '', // url = base url + request url
  73. // withCredentials: true, // send cookies when cross-domain requests
  74. timeout: 180000 // request timeout
  75. })
  76. // { fullscreen: true, text: '努力加载中', spinner: 'el-icon-loading' }
  77. // request interceptor
  78. service.interceptors.request.use(
  79. config => {
  80. // do something before request is sent
  81. showFullScreenLoading()
  82. if (store.getters.token) {
  83. // let each request carry token
  84. // ['X-Token'] is a custom headers key
  85. // please modify it according to the actual situation
  86. config.headers['Authorization'] = getToken()
  87. // config.headers['content-type'] = "application/x-www-form-urlencoded"
  88. }
  89. return config
  90. },
  91. error => {
  92. // do something with request error
  93. tryHideFullScreenLoading()
  94. return Promise.reject(error)
  95. }
  96. )
  97. // response interceptor
  98. service.interceptors.response.use(
  99. res => {
  100. //res.code !== 200
  101. if (res.data) {
  102. let data = JSON.parse(JSON.stringify(res.data))
  103. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  104. if (data.code == 401 || data.code == 403) {
  105. // Message({
  106. // message: `登录过期,请重新登录!`,
  107. // type: 'error',
  108. // duration: 5 * 1000
  109. // })
  110. vue.$message.error(`登录过期,请重新登录!`)
  111. setTimeout(() => {
  112. tryHideFullScreenLoading()
  113. store.dispatch('user/resetToken').then(() => {
  114. location.reload()
  115. })
  116. }, 1000);
  117. return;
  118. }
  119. if (data.code == 404) {
  120. router.push('/404')
  121. }
  122. if (data.code != 200) {
  123. // Message({
  124. // message: data.msg || `请求失败code码为${ data.code }`,
  125. // type: 'error',
  126. // duration: 5 * 1000
  127. // })
  128. let str = data.msg || `请求失败code码为${data.code}`
  129. vue.$message.error(str)
  130. tryHideFullScreenLoading()
  131. return Promise.reject(new Error(data.msg || 'Error'))
  132. } else {
  133. tryHideFullScreenLoading()
  134. return data
  135. }
  136. } else {
  137. tryHideFullScreenLoading()
  138. return Promise.reject()
  139. }
  140. },
  141. error => {
  142. if(error.message == 'Network Error') {
  143. vue.$message.error('网络异常,请检查网络连接')
  144. } else {
  145. vue.$message.error(error.message)
  146. }
  147. tryHideFullScreenLoading()
  148. return Promise.reject(error)
  149. }
  150. )
  151. export default service