123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import { params as envParams } from "@/env";
- import { paramsToStr, strToParams } from "@/utils/params";
- import { AxiosInstance } from "axios";
- import {namespace} from '@/env'
- import Axios from 'axios'
- export const offlinePrev = import.meta.env.DEV ? 'offlineData' : './'
- export const setOfflineAxios = (axios: AxiosInstance) => {
- const data: {[key in string]: any} = {}
- Axios.get(`./${offlinePrev}package/data.json`, {headers: { Accept: "application/json"}}).then(res => {
- Object.assign(data, res.data)
- const prev = import.meta.env.DEV ? offlinePrev : offlinePrev.substring(0, offlinePrev.length - 1)
- for (const key in data) {
- data[prev + key] = data[key]
- }
- ;(window as any).__data = data
- ;(window as any).offlineData = new Proxy({}, {
- get(t, key) {
- if (key in data) {
- return data[key as any]
- } else {
- console.error(key)
- return Axios.get(key as any, {headers: { Accept: "application/json"}})
- .then((res) => {
- data[key as any] = res.data
- return res.data
- })
- }
- }
- })
-
- })
- // 流接口
- const files = {
- [`${namespace}/caseExtractDetail/downDocx`]: './package/resource/caseExtractDetail.doc',
- [`${namespace}/caseInquest/downDocx`]: './package/resource/caseInquest.doc',
- } as any
- // 添加请求拦截器
- axios.interceptors.request.use(
- async function (config) {
- const params = {...config.params}
- if (envParams.caseId) {
- params.caseId = envParams.caseId
- }
- let item = data[config.url!+ paramsToStr(params)]
- console.log(data, item)
- if (!item) {
- delete params.caseId
- item = data[config.url!+ paramsToStr(params)]
- }
- if (item) {
- throw {
- isFakeResponse: true,
- config,
- response: {
- data: item,
- status: 200,
- statusText: 'OK',
- headers: {},
- config: config,
- }
- }
- } else if (files[config.url!]) {
- const res = await Axios.get(files[config.url!], {responseType: 'blob'})
- throw {
- isFakeResponse: true,
- response: {
- data: res.data,
- status: 200,
- statusText: 'OK',
- headers: {},
- config: config,
- },
- }
- } else {
- console.error(config.url, '未在离线包中!')
- }
- return config
- },
- function (error) {
- // 对请求错误做些什么
- return Promise.reject(error);
- }
- );
- // 添加响应拦截器
- axios.interceptors.response.use(
- function (response) {
- if (!files[response.config.url!]) {
- console.error(response.config.url + paramsToStr(response.config.params), '正在添加到离线包中!')
- data[response.config.url+ paramsToStr(response.config.params)!] = response.data
- }
- // 对响应数据做点什么
- return response;
- },
- err => {
- if (err.isFakeResponse) {
- return Promise.resolve(err.response);
- }
- }
- );
- (window as any).proxyData = () => {
- console.log(data)
- console.log(JSON.stringify(data))
- };
- }
|