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