123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import { params as envParams } from "@/env";
- import { paramsToStr, strToParams } from "@/utils/params";
- import { AxiosInstance } from "axios";
- import Axios from 'axios'
- export const setOfflineAxios = (axios: AxiosInstance) => {
- const data: {[key in string]: any} = {}
- Axios.get('./package/data.json', {headers: { Accept: "application/json"}}).then(res => {
- Object.assign(data, res.data)
- ;(window as any).offlineData = data
- })
- // 流接口
- const files = {
- '/fusion-bc/caseExtractDetail/downDocx': './package/resource/caseExtractDetail.doc',
- '/fusion-bc/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)]
- 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))
- };
- }
|