chore: 修改渲染器传送拦截器的方式,将采用合并的形式合并 axios 拦截器

This commit is contained in:
wangxuefeng
2025-03-21 13:43:17 +08:00
parent 828fe45a43
commit 4c59bfeb61
6 changed files with 59 additions and 43 deletions

View File

@@ -1,18 +1,20 @@
<script setup lang="ts">
import type { AxiosInterceptorManager, AxiosResponse } from 'axios';
import { getCurrentInstance, onMounted, ref, watch } from 'vue';
import { useQuery } from '@tanstack/vue-query';
import { jsonp, request } from '@vtj/utils';
import { createProvider } from '@vtj/web';
import { ElLoading, ElMessage } from 'element-plus';
import { isFn } from 'licia-es';
import { LowCodeService } from './service';
// 定义 wujie props 的类型
interface WujieProps {
interceptors?: {
request?: (config: any) => any;
response?: (response: any) => any;
request: AxiosInterceptorManager<InternalAxiosRequestConfig>;
response: AxiosInterceptorManager<AxiosResponse>;
};
// 必填参数
fileId: string;
@@ -23,6 +25,17 @@ interface WujieProps {
[key: string]: any;
}
declare global {
interface Window {
$wujie?: {
bus: {
$emit: (event: string, data?: any) => void;
};
props: WujieProps;
};
}
}
// 定义必要的初始化参数
interface InitParams {
// 必填参数
@@ -40,7 +53,7 @@ const isLoading = ref(false);
const provider = ref(null);
const loadingInstance = ref(null);
const errorMessage = ref('');
const initParams = ref<InitParams | null>(null);
const initParams = ref<InitParams | null | WujieProps>(null);
// 判断是否为wujie子应用
const isWujieSubApp = window.$wujie !== undefined;
@@ -57,13 +70,11 @@ const getParamsFromUrl = (): Partial<InitParams> => {
// 按优先级获取初始化参数
const getInitParams = (): InitParams | null => {
// 1. 优先从wujie props获取
if (isWujieSubApp && window.$wujie?.props) {
const props = window.$wujie.props;
// 确保必填参数存在
const props: WujieProps = window.$wujie.props;
if (props.fileId && props.projectId) {
console.log('使用wujie props初始化渲染器');
return props as InitParams;
console.log('使用无界初始化渲染器');
return props as WujieProps;
}
}
@@ -87,15 +98,32 @@ const isValidParams = (params: InitParams | null): params is InitParams => {
// 初始化请求配置
const initRequestConfig = () => {
// 处理请求拦截器
if (initParams.value?.interceptors?.request) {
request.useRequest(initParams.value.interceptors.request);
}
if (!isWujieSubApp) return console.log('不是无界渲染模式,不进行拦截器合并');
const props = window.$wujie.props;
const mergeRequestInterceptors = () => {
const requestHandlers = props.interceptors?.request?.handlers || [];
requestHandlers.forEach((handler) => {
if (isFn(handler?.fulfilled)) {
request.useRequest(
handler.fulfilled,
handler.rejected,
handler.options,
);
}
});
};
// 处理响应拦截器
if (initParams.value?.interceptors?.response) {
request.useResponse(initParams.value.interceptors.response);
}
const mergeResponseInterceptors = () => {
const responseHandlers = props.interceptors?.response?.handlers || [];
responseHandlers.forEach((handler) => {
if (isFn(handler?.fulfilled)) {
request.useResponse(handler.fulfilled, handler.rejected);
}
});
};
mergeRequestInterceptors();
mergeResponseInterceptors();
};
// 显示加载中