request.js 4.6 KB

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