request2.js 5.0 KB

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