http.js 3.9 KB

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