http.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import axios from 'axios'
  2. import Qs from 'qs'
  3. import { MessageBox } from 'element-ui'
  4. import $store from '@/store'
  5. let host = ''
  6. if (window.location.port === '8080') {
  7. host = '//test.mee.chat/'
  8. }
  9. let ax = axios.create({
  10. baseURL: host,
  11. headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  12. transformRequest: [function (data) {
  13. data = Qs.stringify(data)
  14. return data
  15. }],
  16. transformResponse: [],
  17. withCredentials: true
  18. })
  19. ax.host = host
  20. ax.interceptors.request.use(
  21. async config => {
  22. // 添加登录态
  23. if (config.needLogin) {
  24. if (!localStorage.getItem('user_id') || !localStorage.getItem('token')) {
  25. try {
  26. await $store.dispatch('doGameLogin')
  27. return handleConfig(config)
  28. } catch (error) {
  29. }
  30. } else {
  31. return handleConfig(config)
  32. }
  33. } else {
  34. return handleConfig(config)
  35. }
  36. },
  37. error => {
  38. return Promise.reject(error)
  39. }
  40. )
  41. // 拦截器处理错误
  42. ax.interceptors.response.use(
  43. response => {
  44. if (response.data.result === 1 || response.data.code === -1001) { // result为1时请求成功 code == -1001针对红包已抢完的情况
  45. return Promise.resolve(response)
  46. } else {
  47. if (response.data.code === -5) { // code为-5是登录态失效
  48. $store.dispatch('resetGameLogin')
  49. } else {
  50. // 请求出错提示错误
  51. MessageBox.confirm(response.data.msg, 'Error', {
  52. center: true,
  53. showCancelButton: false,
  54. showConfirmButton: false,
  55. callback () {}
  56. })
  57. return Promise.reject(new Error(response.data.msg))
  58. }
  59. }
  60. },
  61. error => {
  62. return Promise.reject(error)
  63. }
  64. )
  65. async function handleConfig (config) {
  66. try {
  67. let authData = {
  68. user_id: localStorage.getItem('user_id') || '',
  69. token: localStorage.getItem('token') || ''
  70. }
  71. if (config.method === 'post') {
  72. if (config.data) {
  73. config.data = Object.assign(authData, config.data)
  74. } else {
  75. config.data = authData
  76. }
  77. } else if (config.method === 'get') {
  78. if (config.params) {
  79. config.params = Object.assign(authData, config.params)
  80. } else {
  81. config.params = authData
  82. }
  83. }
  84. return config
  85. } catch {
  86. return config
  87. }
  88. }
  89. // let _request = ax.request
  90. ax.request2 = async function (config) {
  91. var objResult = null
  92. if (config.cache) {
  93. return new Promise(async (resolve, reject) => {
  94. var data = null
  95. config = await handleConfig(config)
  96. if (config.method === 'post') {
  97. data = config.data
  98. } else if (config.method === 'get') {
  99. data = config.params
  100. }
  101. var cacheKey = config.method + ':' + config.url + '?' + JSON.stringify(data)
  102. var cacheStr = localStorage.getItem(cacheKey)
  103. if (cacheStr) {
  104. objResult = JSON.parse(cacheStr)
  105. config.callback(objResult)
  106. }
  107. objResult = await ax.request(config)
  108. delete objResult['config'] // 这个config没必要缓存
  109. delete objResult['request'] // 这个request没必要缓存
  110. var str2 = JSON.stringify(objResult)
  111. if (cacheStr != str2) {
  112. if (objResult.data.result) {
  113. localStorage.setItem(cacheKey, str2)
  114. }
  115. config.callback(objResult)
  116. }
  117. })
  118. } else {
  119. objResult = await ax.request(config)
  120. config.callback(objResult)
  121. }
  122. }
  123. export default ax