request.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import Vue from 'vue'
  2. import axios from 'axios'
  3. import store from '../store'
  4. import _ from 'lodash'
  5. import { Toast, Notify } from 'vant'
  6. import { getCookieValue } from '@/utils'
  7. const host = window.location.host
  8. const domain = host.substring(0, host.indexOf('.')) || host.substring(0, host.indexOf(':'))
  9. const request = axios.create({
  10. withCredentials: true,
  11. baseURL: process.env.API_DOMAIN,
  12. timeout: 50000,
  13. headers: {
  14. 'Content-Type': 'application/json;charset=UTF-8'
  15. }
  16. })
  17. let loading = null
  18. // 当前正在请求的数量
  19. let needLoadingRequestCount = 0
  20. // 显示loading
  21. function showLoading () {
  22. if (needLoadingRequestCount === 0 && !loading) {
  23. loading = Toast.loading({
  24. message: '拼命获取中',
  25. forbidClick: true,
  26. duration: 0
  27. })
  28. }
  29. needLoadingRequestCount++
  30. }
  31. // 隐藏loading
  32. function hideLoading () {
  33. needLoadingRequestCount--
  34. needLoadingRequestCount = Math.max(needLoadingRequestCount, 0) // 做个保护
  35. if (needLoadingRequestCount === 0) {
  36. // 关闭loading
  37. toHideLoading()
  38. }
  39. }
  40. // 防抖:将 300ms 间隔内的关闭 loading 便合并为一次。防止连续请求时, loading闪烁的问题。
  41. const toHideLoading = _.debounce(() => {
  42. loading.clear()
  43. loading = null
  44. }, 300)
  45. // Add a request interceptor
  46. request.interceptors.request.use(request => {
  47. const inviteId = store.getters['common/inviteId']
  48. const requestUrl = request.url
  49. // 因为微信开发者工具重复授权,本地开发时写死
  50. if (/^(0|192|10|localhost)/.test(domain)) {
  51. request.headers.wechatToken = '7820fddb72c61b89c7d31c3450269da1'
  52. } else {
  53. request.headers.wechatToken = getCookieValue('fanbutingwechatToken')
  54. }
  55. request.url = requestUrl + (requestUrl.indexOf('?') > -1 ? '&' : '?') + 'nextUrl=' + encodeURIComponent(location.href) + (inviteId ? '&invite_id=' + inviteId : '')
  56. // 判断当前请求是否设置了不显示Loading
  57. if (request.showLoading) {
  58. showLoading()
  59. }
  60. return request
  61. }, error => {
  62. // 判断当前请求是否设置了不显示Loading
  63. if (loading) {
  64. hideLoading()
  65. }
  66. Notify({
  67. type: 'warning',
  68. message: '请求超时'
  69. })
  70. // Do something with request error
  71. return Promise.reject(error)
  72. })
  73. // Add a response interceptor
  74. request.interceptors.response.use(response => {
  75. // Do something with response data
  76. const { code, url } = response.data
  77. if (loading) {
  78. hideLoading()
  79. }
  80. if (code === 401 && url) {
  81. Vue.cookie.delete('fanbutingwechatToken')
  82. location.replace(url)
  83. }
  84. return response.data
  85. }, (error) => {
  86. if (loading) {
  87. hideLoading()
  88. }
  89. Notify({
  90. type: 'danger',
  91. message: '数据获取失败!'
  92. })
  93. // Do something with response error
  94. return Promise.reject(error)
  95. })
  96. export default request