responseInterceptors.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import throttle from "@/uni_modules/uv-ui-tools/libs/function/throttle";
  2. import mxConst from "@/common/mxConst";
  3. import {fnPlaceholder} from "@/utils/uni-helper";
  4. import {useUserStore} from "@/hooks/useUserStore";
  5. function isBlobRequest(response) {
  6. return response?.config?.responseType?.toLowerCase() === 'arraybuffer'
  7. }
  8. /**
  9. * 响应拦截
  10. * @param {Object} http
  11. */
  12. export const responseInterceptors = (vm) => {
  13. // blob 预处理,文件流强制转换输出类型
  14. uni.$uv.http.interceptors.response.use((response) => {
  15. if (isBlobRequest(response)) {
  16. response.config.custom = response.config.custom || {}
  17. response.config.custom.raw = true
  18. }
  19. return response
  20. })
  21. // 一般的请求处理结果
  22. uni.$uv.http.interceptors.response.use(async (response) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
  23. const data = response.data
  24. // 自定义参数
  25. const custom = response.config?.custom
  26. const code = data.code || response.statusCode
  27. if (code !== 200) { // 服务端返回的状态码不等于200,则reject()
  28. const {clearToken} = useUserStore()
  29. const msg = data.msg || data.message || mxConst.serverErrors[data.code] || '服务器错误,请稍后重试'
  30. switch (code) {
  31. case 401:
  32. // 无提示跳转
  33. clearToken()
  34. throttle(() => uni.$uv.route(mxConst.routes.login))
  35. break
  36. case 402:
  37. case 405:
  38. // 有提示跳转
  39. throttle(() => {
  40. Promise.all([
  41. new Promise((resolve) => {
  42. uni.showModal({
  43. content: msg, // 登陆提示或者是 VIP 提示
  44. showCancel: false,
  45. success: _ => {
  46. if (data.code == 402) {
  47. clearToken()
  48. uni.$uv.route(mxConst.routes.login)
  49. }
  50. else if (data.code == 405) {
  51. uni.$uv.route(mxConst.routes.activate)
  52. }
  53. else {
  54. throw new Error('Unhandled code for modal action: ' + data.code)
  55. }
  56. resolve()
  57. }
  58. })
  59. })
  60. ])
  61. })
  62. break
  63. default:
  64. // 如果没有显式定义custom的toast参数为false的话,默认对报错进行toast弹出提示
  65. if (custom?.toast !== false) {
  66. uni.$uv.toast(msg)
  67. }
  68. break
  69. }
  70. // 如果需要catch返回,则进行reject
  71. if (custom?.catch) {
  72. // 后续能显式收到错误
  73. return Promise.reject(data)
  74. } else {
  75. // 打断继续执行,但不会显式收到错误
  76. return new Promise(fnPlaceholder)
  77. }
  78. }
  79. // 成功时,定了几种不同的返回类型
  80. if (custom?.raw === true) return response
  81. if (custom?.root === true) return data // 默认配置
  82. return data.data || {}
  83. }, (response) => { /* 对响应错误做点什么 (statusCode !== 200)*/
  84. return Promise.reject(response)
  85. })
  86. // blob 文件流返回数据处理
  87. uni.$uv.http.interceptors.response.use((response) => {
  88. if (isBlobRequest(response)) {
  89. if (!(response.data instanceof Blob) && (response.data instanceof ArrayBuffer)) {
  90. response.data = new Blob([response.data])
  91. }
  92. }
  93. return response
  94. })
  95. }