|
@@ -0,0 +1,77 @@
|
|
|
+import { AxiosInstance } from "axios";
|
|
|
+import Axios from 'axios'
|
|
|
+
|
|
|
+const data: {[key in string]: any} = {}
|
|
|
+Axios.get('/package/data.json', {headers: { Accept: "application/json"}}).then(res => {
|
|
|
+ Object.assign(data, res.data)
|
|
|
+})
|
|
|
+
|
|
|
+// 流接口
|
|
|
+const files = {
|
|
|
+ '/fusion-xj/caseExtractDetail/downDocx': '/package/resource/caseExtractDetail.doc',
|
|
|
+ '/fusion-xj/caseInquest/downDocx': '/package/resource/caseInquest.doc',
|
|
|
+} as any
|
|
|
+
|
|
|
+export const setOfflineAxios = (axios: AxiosInstance) => {
|
|
|
+ // 添加请求拦截器
|
|
|
+ axios.interceptors.request.use(
|
|
|
+ async function (config) {
|
|
|
+ if (config.url! in data) {
|
|
|
+ throw {
|
|
|
+ isFakeResponse: true,
|
|
|
+ config,
|
|
|
+ response: {
|
|
|
+ data: data[config.url!],
|
|
|
+ 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.includes(response.config.url!)) {
|
|
|
+ console.error(response.config.url, '正在添加到离线包中!')
|
|
|
+ data[response.config.url!] = response.data
|
|
|
+ }
|
|
|
+ // 对响应数据做点什么
|
|
|
+ return response;
|
|
|
+ },
|
|
|
+ err => {
|
|
|
+ console.log(err)
|
|
|
+ if (err.isFakeResponse) {
|
|
|
+ return Promise.resolve(err.response);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+(window as any).proxyData = () => {
|
|
|
+ console.log(JSON.stringify(data))
|
|
|
+};
|