offline.ts 2.4 KB

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