request.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import store from '@/store'
  2. import config from '@/config'
  3. import { getToken } from '@/utils/auth'
  4. import errorCode from '@/utils/errorCode'
  5. import { toast, showConfirm, tansParams } from '@/utils/common'
  6. let timeout = 30000
  7. const baseUrl = config.baseUrl
  8. const request = async (config) => {
  9. // 是否需要设置 token
  10. const isToken = (config.headers || {}).isToken === false
  11. config.header = config.header || {}
  12. if (getToken() && !isToken) {
  13. config.header['Authorization'] = 'Bearer ' + getToken()
  14. }
  15. // get请求映射params参数
  16. if (config.params) {
  17. let url = config.url + '?' + tansParams(config.params)
  18. url = url.slice(0, -1)
  19. config.url = url
  20. }
  21. return new Promise(async (resolve, reject) => {
  22. try {
  23. const response = await uni.request({
  24. method: config.method || 'GET',
  25. timeout: config.timeout || timeout,
  26. url: config.baseUrl || baseUrl + config.url,
  27. data: config.data,
  28. header: config.header,
  29. dataType: 'json'
  30. })
  31. const [error, res] = response
  32. if (error) {
  33. toast('后端接口连接异常')
  34. reject('后端接口连接异常')
  35. return
  36. }
  37. const code = res.data.code || 200
  38. const msg = errorCode[code] || res.data.msg || errorCode['default']
  39. if (code === 401) {
  40. // 自动登录
  41. const loginForm = {
  42. username: uni.getStorageSync('u1'),
  43. password: uni.getStorageSync('p1'),
  44. code: "",
  45. uuid: ''
  46. }
  47. try {
  48. await store.dispatch('Login', loginForm).then(async () => {
  49. // 保存原始请求配置以备重试
  50. // const originalConfig = {
  51. // method: config.method || 'GET',
  52. // timeout: config.timeout || timeout,
  53. // url: config.baseUrl || baseUrl + config.url,
  54. // data: config.data,
  55. // header: config.header,
  56. // dataType: 'json'
  57. // }
  58. const originalConfig = { ...config };
  59. // 重新发送请求
  60. const retryResponse = await retryRequest(originalConfig)
  61. resolve(retryResponse)
  62. });
  63. // // 保存原始请求配置以备重试
  64. // const originalConfig = {
  65. // method: config.method || 'GET',
  66. // timeout: config.timeout || timeout,
  67. // url: config.baseUrl || baseUrl + config.url,
  68. // data: config.data,
  69. // header: config.header,
  70. // dataType: 'json'
  71. // }
  72. // //const originalConfig = { ...config };
  73. // // 重新发送请求
  74. // const retryResponse = await retryRequest(originalConfig)
  75. // resolve(retryResponse)
  76. } catch (loginError) {
  77. showConfirm('自动重登失效,您可以继续留在该页面,或者手动登录?').then(res => {
  78. if (res.confirm) {
  79. store.dispatch('LogOut').then(res => {
  80. uni.reLaunch({
  81. url: '/pages/login'
  82. })
  83. })
  84. }
  85. })
  86. reject(loginError)
  87. }
  88. } else if (code === 500) {
  89. toast(msg)
  90. reject('500')
  91. } else if (code !== 200) {
  92. toast(msg)
  93. reject(code)
  94. } else {
  95. resolve(res.data)
  96. }
  97. } catch (error) {
  98. let { message } = error
  99. if (message === 'Network Error') {
  100. message = '后端接口连接异常'
  101. } else if (message.includes('timeout')) {
  102. message = '系统接口请求超时'
  103. } else if (message.includes('Request failed with status code')) {
  104. message = '系统接口' + message.substr(message.length - 3) + '异常'
  105. }
  106. toast(message)
  107. reject(error)
  108. }
  109. })
  110. }
  111. // 用于重新发送请求的方法
  112. // const retryRequest = async (originalConfig) => {
  113. // const response = await request(originalConfig)
  114. // return response
  115. // }
  116. const retryRequest = async (originalConfig) => {
  117. // 重新获取最新的 token
  118. const token = getToken()
  119. // 明确设置请求方法和参数,并更新 header 中的 token
  120. const response = await uni.request({
  121. method: originalConfig.method || 'GET',
  122. timeout: originalConfig.timeout || timeout,
  123. url: originalConfig.baseUrl || baseUrl + originalConfig.url,
  124. data: originalConfig.data,
  125. header: {
  126. ...originalConfig.header,
  127. Authorization: token ? 'Bearer ' + token : undefined
  128. },
  129. dataType: 'json'
  130. })
  131. const [error, res] = response
  132. if (error) {
  133. throw new Error('重试请求失败')
  134. }
  135. console.log(res.data)
  136. return res.data
  137. }
  138. export default request