offline.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { params as envParams } from "@/env";
  2. import { paramsToStr, strToParams } from "@/utils/params";
  3. import { AxiosInstance } from "axios";
  4. import {namespace} from '@/env'
  5. import Axios from 'axios'
  6. export const offlinePrev = import.meta.env.DEV ? 'offlineData' : './'
  7. export const setOfflineAxios = (axios: AxiosInstance) => {
  8. const data: {[key in string]: any} = {}
  9. Axios.get(`./${offlinePrev}package/data.json`, {headers: { Accept: "application/json"}}).then(res => {
  10. Object.assign(data, res.data)
  11. const prev = import.meta.env.DEV ? offlinePrev : offlinePrev.substring(0, offlinePrev.length - 1)
  12. for (const key in data) {
  13. data[prev + key] = data[key]
  14. }
  15. ;(window as any).__data = data
  16. ;(window as any).offlineData = new Proxy({}, {
  17. get(t, key) {
  18. if (key in data) {
  19. return data[key as any]
  20. } else {
  21. console.error(key)
  22. return Axios.get(key as any, {headers: { Accept: "application/json"}})
  23. .then((res) => {
  24. data[key as any] = res.data
  25. return res.data
  26. })
  27. }
  28. }
  29. })
  30. })
  31. // 流接口
  32. const files = {
  33. [`${namespace}/caseExtractDetail/downDocx`]: './package/resource/caseExtractDetail.doc',
  34. [`${namespace}/caseInquest/downDocx`]: './package/resource/caseInquest.doc',
  35. } as any
  36. // 添加请求拦截器
  37. axios.interceptors.request.use(
  38. async function (config) {
  39. const params = {...config.params}
  40. if (envParams.caseId) {
  41. params.caseId = envParams.caseId
  42. }
  43. let item = data[config.url!+ paramsToStr(params)]
  44. console.log(data, item)
  45. if (!item) {
  46. delete params.caseId
  47. item = data[config.url!+ paramsToStr(params)]
  48. }
  49. if (item) {
  50. throw {
  51. isFakeResponse: true,
  52. config,
  53. response: {
  54. data: item,
  55. status: 200,
  56. statusText: 'OK',
  57. headers: {},
  58. config: config,
  59. }
  60. }
  61. } else if (files[config.url!]) {
  62. const res = await Axios.get(files[config.url!], {responseType: 'blob'})
  63. throw {
  64. isFakeResponse: true,
  65. response: {
  66. data: res.data,
  67. status: 200,
  68. statusText: 'OK',
  69. headers: {},
  70. config: config,
  71. },
  72. }
  73. } else {
  74. console.error(config.url, '未在离线包中!')
  75. }
  76. return config
  77. },
  78. function (error) {
  79. // 对请求错误做些什么
  80. return Promise.reject(error);
  81. }
  82. );
  83. // 添加响应拦截器
  84. axios.interceptors.response.use(
  85. function (response) {
  86. if (!files[response.config.url!]) {
  87. console.error(response.config.url + paramsToStr(response.config.params), '正在添加到离线包中!')
  88. data[response.config.url+ paramsToStr(response.config.params)!] = response.data
  89. }
  90. // 对响应数据做点什么
  91. return response;
  92. },
  93. err => {
  94. if (err.isFakeResponse) {
  95. return Promise.resolve(err.response);
  96. }
  97. }
  98. );
  99. (window as any).proxyData = () => {
  100. console.log(data)
  101. console.log(JSON.stringify(data))
  102. };
  103. }