httpForm.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import axios from 'axios'
  2. import { MessageBox } from 'element-ui'
  3. import $store from '@/store'
  4. let host = ''
  5. if (window.location.port === '8080') {
  6. host = '//test.mee.chat/'
  7. }
  8. let ax = axios.create({
  9. baseURL: host,
  10. headers: { 'Content-Type': 'multipart/form-data' },
  11. withCredentials: true
  12. })
  13. ax.host = host
  14. // 拦截器处理错误
  15. ax.interceptors.response.use(
  16. response => {
  17. if (response.data.result === 1 || response.data.code === -1001) {
  18. // result为1时请求成功 code == -1001针对红包已抢完的情况
  19. return Promise.resolve(response)
  20. } else {
  21. if (response.data.code === -5) {
  22. // code为-5是登录态失效
  23. $store.dispatch('resetGameLogin')
  24. } else {
  25. // 请求出错提示错误
  26. MessageBox.confirm(response.data.msg, 'Error', {
  27. center: true,
  28. showCancelButton: false,
  29. showConfirmButton: false,
  30. callback () {}
  31. })
  32. return Promise.reject(new Error(response.data.msg))
  33. }
  34. }
  35. },
  36. error => {
  37. return Promise.reject(error)
  38. }
  39. )
  40. export default ax