request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import axios from '@/utils/request/axios'
  2. import qs from 'qs'
  3. import { storage } from '@/utils'
  4. import { ubmsConfig } from './config'
  5. import { PendingManager } from './pendingManager'
  6. const pendingManager = new PendingManager()
  7. /**
  8. * 创建 axios 实例
  9. */
  10. const instance = axios.create({
  11. baseURL: ubmsConfig.baseURL,
  12. timeout: 5000,
  13. })
  14. /**
  15. * 请求拦截器
  16. */
  17. instance.interceptors.request.use((request) => {
  18. const accessToken = storage.get(`${ubmsConfig.storagePrefixKey}accessToken`, '')
  19. request.headers.deviceid = 'unknown'
  20. request.headers.Authorization = `Bearer ${accessToken}`
  21. return request
  22. })
  23. /**
  24. * 响应拦截器
  25. */
  26. instance.interceptors.response.use(
  27. (response) => {
  28. if (Number(response.status) === 200) {
  29. const result = response.data
  30. result.msg = result.message // message
  31. if (Number(result.code) === 0) {
  32. return result
  33. } else {
  34. uni.showToast({
  35. icon: 'none',
  36. title: `${result.msg || result.code || 'Result Error'}`,
  37. duration: 3 * 1000,
  38. })
  39. return Promise.reject(result)
  40. }
  41. } else {
  42. httpErrorHandle(response)
  43. return Promise.reject(response)
  44. }
  45. },
  46. async (error) => {
  47. if (error.response.status === 410) {
  48. if (!pendingManager.isRefreshing) {
  49. pendingManager.isRefreshing = true
  50. await refreshAccessToken()
  51. pendingManager.isRefreshing = false
  52. pendingManager.removeAllPending()
  53. return instance(error.config)
  54. }
  55. return new Promise((resolve) => {
  56. pendingManager.addPending(() => {
  57. resolve(instance(error.config))
  58. })
  59. })
  60. } else {
  61. httpErrorHandle(error)
  62. }
  63. return Promise.reject(error)
  64. },
  65. )
  66. /**
  67. * HTTP 错误
  68. */
  69. export function httpErrorHandle(error) {
  70. // console.log('[httpErrorHandle]', error)
  71. let msg = ''
  72. if (error.response && error.response.data) {
  73. const { code, message: msg } = error.response.data
  74. uni.showToast({
  75. icon: 'none',
  76. title: msg || code,
  77. duration: 3 * 1000,
  78. })
  79. return
  80. }
  81. if (msg) {
  82. uni.showToast({
  83. icon: 'none',
  84. title: '当前网络不可用,请检查你的网络设置',
  85. duration: 3 * 1000,
  86. })
  87. }
  88. }
  89. /**
  90. * 刷新AccessToken,通过登录接口
  91. */
  92. async function refreshAccessToken() {
  93. try {
  94. const params = {
  95. username: ubmsConfig.username,
  96. password: ubmsConfig.password,
  97. }
  98. const response = await axios({
  99. url: `${ubmsConfig.baseURL}/ubms-foreign/authorize/login`,
  100. method: 'post',
  101. data: qs.stringify(params),
  102. headers: {
  103. deviceid: 'unknown',
  104. Authorization: ubmsConfig.authorization,
  105. },
  106. })
  107. const data = response.data
  108. const { access_token } = data
  109. storage.set(`${ubmsConfig.storagePrefixKey}accessToken`, access_token)
  110. return data
  111. } catch (error) {
  112. console.error('Failed to refresh access token:', error)
  113. throw error
  114. }
  115. }
  116. export default instance