|
|
@@ -2,6 +2,9 @@ import { Tree } from 'antd';
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
|
|
import { service } from '@/pages/system/Platforms';
|
|
|
import { ApiModel } from '@/pages/system/Platforms/Api/base';
|
|
|
+import { forkJoin, from, defer } from 'rxjs';
|
|
|
+import { map } from 'rxjs/operators';
|
|
|
+import { LoadingOutlined } from '@ant-design/icons';
|
|
|
|
|
|
type LeftTreeType = {
|
|
|
onSelect: (data: any) => void;
|
|
|
@@ -25,73 +28,32 @@ interface DataNode {
|
|
|
|
|
|
export default (props: LeftTreeType) => {
|
|
|
const [treeData, setTreeData] = useState<DataNode[]>([]);
|
|
|
-
|
|
|
- const getLevelOne = async () => {
|
|
|
- const resp = await service.getApiFirstLevel();
|
|
|
- if (resp.urls && resp.urls.length) {
|
|
|
- if (props.showHome) {
|
|
|
- const home = [
|
|
|
- {
|
|
|
- id: 'home',
|
|
|
- name: '首页',
|
|
|
- isLeaf: true,
|
|
|
- },
|
|
|
- ];
|
|
|
- ApiModel.data = undefined;
|
|
|
- setTreeData(home.concat(resp.urls.map((item: any) => ({ ...item, id: item.url }))));
|
|
|
- } else {
|
|
|
- setTreeData(resp.urls.map((item: any) => ({ ...item, id: item.url })));
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
+ const [loading, setLoading] = useState(false);
|
|
|
|
|
|
const updateTreeData = useCallback(
|
|
|
- (list: DataNode[], key: React.Key, children: DataNode[]): DataNode[] => {
|
|
|
- return list.map((node) => {
|
|
|
- if (node.id === key) {
|
|
|
- const newChildren = node.children ? [...node.children, ...children] : children;
|
|
|
- // // 屏蔽掉没有子节点的接口
|
|
|
- // const noOperations = newChildren.filter((item: any) => {
|
|
|
- // return item.extraData &&
|
|
|
- // item.extraData.some((extraItem: any) =>
|
|
|
- // props.operations?.includes(extraItem.operationId),
|
|
|
- // )
|
|
|
- // })
|
|
|
-
|
|
|
- let filterChildren = newChildren;
|
|
|
- // api详情时,过滤掉没有授权的接口
|
|
|
- if (props.type === 'empowerment') {
|
|
|
- filterChildren = newChildren.filter(
|
|
|
- (item: any) =>
|
|
|
- item.extraData &&
|
|
|
- item.extraData.some((extraItem: any) =>
|
|
|
- props.operations?.includes(extraItem.operationId),
|
|
|
- ),
|
|
|
- );
|
|
|
- } else if (props.type === 'authorize') {
|
|
|
- filterChildren = newChildren.filter(
|
|
|
- (item: any) =>
|
|
|
- item.extraData &&
|
|
|
- item.extraData.some((extraItem: any) =>
|
|
|
- props.grantKeys?.includes(extraItem.operationId),
|
|
|
- ),
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- return {
|
|
|
- ...node,
|
|
|
- children: filterChildren,
|
|
|
- };
|
|
|
- }
|
|
|
+ (list: DataNode[], parentItem: any): DataNode[] => {
|
|
|
+ let newArray: any[] = list;
|
|
|
+ console.log(list, props.grantKeys);
|
|
|
+ if (props.type === 'empowerment') {
|
|
|
+ newArray = list.filter(
|
|
|
+ (item: any) =>
|
|
|
+ item.extraData &&
|
|
|
+ item.extraData.some((extraItem: any) =>
|
|
|
+ props.operations?.includes(extraItem.operationId),
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ } else if (props.type === 'authorize') {
|
|
|
+ newArray = list.filter(
|
|
|
+ (item: any) =>
|
|
|
+ item.extraData &&
|
|
|
+ item.extraData.some((extraItem: any) =>
|
|
|
+ props.grantKeys?.includes(extraItem.operationId),
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ }
|
|
|
+ parentItem.children = newArray;
|
|
|
|
|
|
- if (node.children) {
|
|
|
- return {
|
|
|
- ...node,
|
|
|
- children: updateTreeData(node.children, key, children),
|
|
|
- };
|
|
|
- }
|
|
|
- return node;
|
|
|
- });
|
|
|
+ return parentItem;
|
|
|
},
|
|
|
[props.isShowGranted, props.grantKeys, props.type, props.operations],
|
|
|
);
|
|
|
@@ -120,60 +82,129 @@ export default (props: LeftTreeType) => {
|
|
|
return newArr;
|
|
|
};
|
|
|
|
|
|
- const getChildren = (key: string, name: string): Promise<any> => {
|
|
|
- return new Promise(async (resolve) => {
|
|
|
- const resp = await service.getApiNextLevel(name);
|
|
|
- if (resp) {
|
|
|
- ApiModel.components = { ...ApiModel.components, ...resp.components.schemas };
|
|
|
- const handleData = handleTreeData(resp);
|
|
|
- setTreeData((origin) => {
|
|
|
- const data = updateTreeData(origin, key, handleData);
|
|
|
+ const getChildrenData = async (data: any[], extraData?: any) => {
|
|
|
+ const ofArray: any[] = [];
|
|
|
+ data.forEach((item: any) => {
|
|
|
+ ofArray.push(
|
|
|
+ defer(() =>
|
|
|
+ from(service.getApiNextLevel(item.name)).pipe(
|
|
|
+ map((resp: any) => {
|
|
|
+ if (resp && resp.components) {
|
|
|
+ return handleTreeData(resp);
|
|
|
+ }
|
|
|
+ return undefined;
|
|
|
+ }),
|
|
|
+ map((resp: any) => resp && updateTreeData(resp, item)),
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ });
|
|
|
|
|
|
- return data;
|
|
|
- });
|
|
|
- resolve(resp.result);
|
|
|
- }
|
|
|
+ forkJoin(ofArray).subscribe((res) => {
|
|
|
+ console.log(res);
|
|
|
+ setLoading(false);
|
|
|
+ setTreeData(extraData ? [extraData, ...res] : res);
|
|
|
});
|
|
|
};
|
|
|
|
|
|
- const onLoadData = (node: any): Promise<void> => {
|
|
|
- return new Promise(async (resolve) => {
|
|
|
- if (node.children) {
|
|
|
- resolve();
|
|
|
- return;
|
|
|
+ const getLevelOne = async () => {
|
|
|
+ setLoading(true);
|
|
|
+ const resp = await service.getApiFirstLevel();
|
|
|
+ if (resp.urls && resp.urls.length) {
|
|
|
+ if (props.showHome) {
|
|
|
+ const home = {
|
|
|
+ id: 'home',
|
|
|
+ name: '首页',
|
|
|
+ isLeaf: true,
|
|
|
+ };
|
|
|
+ ApiModel.data = undefined;
|
|
|
+ getChildrenData(
|
|
|
+ resp.urls.map((item: any) => ({ ...item, id: item.url })),
|
|
|
+ home,
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ // setTreeData(resp.urls.map((item: any) => ({ ...item, id: item.url })));
|
|
|
+ getChildrenData(resp.urls.map((item: any) => ({ ...item, id: item.url })));
|
|
|
}
|
|
|
- await getChildren(node.key, node.name);
|
|
|
- resolve();
|
|
|
- });
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
+ // const getChildren = (key: string, name: string): Promise<any> => {
|
|
|
+ // return new Promise(async (resolve) => {
|
|
|
+ // const resp = await service.getApiNextLevel(name);
|
|
|
+ // if (resp && resp.components) {
|
|
|
+ // ApiModel.components = { ...ApiModel.components, ...resp.components.schemas };
|
|
|
+ // const handleData = handleTreeData(resp);
|
|
|
+ // setTreeData((origin) => {
|
|
|
+ // const data = updateTreeData(origin, key, handleData);
|
|
|
+ //
|
|
|
+ // return data;
|
|
|
+ // });
|
|
|
+ // resolve(resp.result);
|
|
|
+ // } else {
|
|
|
+ // resolve([])
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ // };
|
|
|
+
|
|
|
+ // const onLoadData = (node: any): Promise<void> => {
|
|
|
+ // return new Promise(async (resolve) => {
|
|
|
+ // if (node.children) {
|
|
|
+ // resolve();
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+ // await getChildren(node.key, node.name);
|
|
|
+ // resolve();
|
|
|
+ // });
|
|
|
+ // };
|
|
|
+
|
|
|
useEffect(() => {
|
|
|
- getLevelOne();
|
|
|
- }, []);
|
|
|
+ if (props.type === 'all') {
|
|
|
+ getLevelOne();
|
|
|
+ } else if (props.type === 'empowerment' && props.grantKeys) {
|
|
|
+ getLevelOne();
|
|
|
+ } else if (props.type === 'authorize' && props.operations) {
|
|
|
+ getLevelOne();
|
|
|
+ }
|
|
|
+ }, [props.operations, props.grantKeys, props.type]);
|
|
|
|
|
|
+ console.log(treeData);
|
|
|
return (
|
|
|
- <Tree
|
|
|
- showIcon
|
|
|
- showLine={{ showLeafIcon: false }}
|
|
|
- height={700}
|
|
|
- style={{ minWidth: 145 }}
|
|
|
- fieldNames={{
|
|
|
- title: 'name',
|
|
|
- key: 'id',
|
|
|
- }}
|
|
|
- onSelect={(_, { node }: any) => {
|
|
|
- if (node.isLeaf && props.onSelect) {
|
|
|
- props.onSelect(node.extraData);
|
|
|
- }
|
|
|
+ <div
|
|
|
+ style={{
|
|
|
+ width: '100%',
|
|
|
+ height: '100%',
|
|
|
}}
|
|
|
- // onExpand={(_,{node}:any)=>{
|
|
|
- // if (node.isLeaf && props.onSelect) {
|
|
|
- // props.onSelect(node.extraData);
|
|
|
- // }
|
|
|
- // }}
|
|
|
- defaultSelectedKeys={['home']}
|
|
|
- loadData={onLoadData}
|
|
|
- treeData={treeData}
|
|
|
- />
|
|
|
+ >
|
|
|
+ {!loading ? (
|
|
|
+ <Tree
|
|
|
+ showIcon
|
|
|
+ showLine={{ showLeafIcon: false }}
|
|
|
+ height={700}
|
|
|
+ style={{ minWidth: 145 }}
|
|
|
+ fieldNames={{
|
|
|
+ title: 'name',
|
|
|
+ key: 'id',
|
|
|
+ }}
|
|
|
+ onSelect={(_, { node }: any) => {
|
|
|
+ if (node.isLeaf && props.onSelect) {
|
|
|
+ props.onSelect(node.extraData);
|
|
|
+ }
|
|
|
+ }}
|
|
|
+ // onExpand={(_,{node}:any)=>{
|
|
|
+ // if (node.isLeaf && props.onSelect) {
|
|
|
+ // props.onSelect(node.extraData);
|
|
|
+ // }
|
|
|
+ // }}
|
|
|
+ defaultSelectedKeys={['home']}
|
|
|
+ // loadData={onLoadData}
|
|
|
+ treeData={treeData}
|
|
|
+ />
|
|
|
+ ) : (
|
|
|
+ <div style={{ paddingTop: 32, display: 'flex', justifyContent: 'center' }}>
|
|
|
+ <LoadingOutlined style={{ fontSize: 32 }} />
|
|
|
+ </div>
|
|
|
+ )}
|
|
|
+ </div>
|
|
|
);
|
|
|
};
|