request.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 qs from 'querystring'
  8. // import { Loading } from 'element-ui'
  9. import { showFullScreenLoading, tryHideFullScreenLoading } from './request-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. // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  58. // create an axios instance
  59. const service = axios.create({
  60. baseURL: '', // url = base url + request url
  61. // withCredentials: true, // send cookies when cross-domain requests
  62. timeout: 180000, // request timeout
  63. })
  64. // { fullscreen: true, text: '努力加载中', spinner: 'el-icon-loading' }
  65. // request interceptor
  66. service.interceptors.request.use(
  67. config => {
  68. // do something before request is sent
  69. showFullScreenLoading()
  70. if (store.getters.token) {
  71. // let each request carry token
  72. // ['X-Token'] is a custom headers key
  73. // please modify it according to the actual situation
  74. config.headers['Authorization'] = getToken()
  75. // config.headers['content-type'] = "application/x-www-form-urlencoded"
  76. }
  77. const tenantId = sessionStorage.getItem('tenantId') || null
  78. if(tenantId && tenantId != 'undefined') {
  79. config.headers['tenantId'] = tenantId
  80. }
  81. config.params = cleanDeep(config.params)
  82. // params: cleanDeep(options.params),
  83. // (config)
  84. return config
  85. },
  86. error => {
  87. // do something with request error
  88. tryHideFullScreenLoading()
  89. return Promise.reject(error)
  90. }
  91. )
  92. // response interceptor
  93. service.interceptors.response.use(
  94. res => {
  95. //res.code !== 200
  96. if (res.data) {
  97. let data = JSON.parse(JSON.stringify(res.data))
  98. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  99. if (data.code == 401 || data.code == 403) {
  100. // Message({
  101. // message: `登录过期,请重新登录!`,
  102. // type: 'error',
  103. // duration: 5 * 1000
  104. // })
  105. vue.$message.error(`登录过期,请重新登录!`)
  106. setTimeout(() => {
  107. tryHideFullScreenLoading()
  108. store.dispatch('user/resetToken').then(() => {
  109. location.reload()
  110. })
  111. }, 1000);
  112. return;
  113. }
  114. if (data.code == 404) {
  115. router.push('/404')
  116. }
  117. if (data.code < 200&&data.code != 100||data.code >= 300&&data.code != 100) {
  118. // Message({
  119. // message: data.msg || `请求失败code码为${ data.code }`,
  120. // type: 'error',
  121. // duration: 5 * 1000
  122. // })
  123. let str = data.msg || `请求失败code码为${data.code}`
  124. vue.$message.error(str)
  125. tryHideFullScreenLoading()
  126. return Promise.reject(new Error(data.msg || 'Error'))
  127. } else {
  128. tryHideFullScreenLoading()
  129. return data
  130. }
  131. } else {
  132. tryHideFullScreenLoading()
  133. return Promise.reject()
  134. }
  135. },
  136. error => {
  137. if (error.message == 'Network Error') {
  138. vue.$message.error('网络异常,请检查网络连接')
  139. } else {
  140. vue.$message.error(error.message)
  141. }
  142. tryHideFullScreenLoading()
  143. return Promise.reject(error)
  144. }
  145. )
  146. export default service