- 优化节点查找
This commit is contained in:
parent
87fa8ba5fa
commit
d126b19393
@ -7,7 +7,6 @@ import {
|
|||||||
queryRole,
|
queryRole,
|
||||||
queryRoleResourceTree,
|
queryRoleResourceTree,
|
||||||
roleAssignResource,
|
roleAssignResource,
|
||||||
resourceTree,
|
|
||||||
} from '../../services/admin';
|
} from '../../services/admin';
|
||||||
|
|
||||||
function buildTreeNode(nodes, titleKey, nodeKey) {
|
function buildTreeNode(nodes, titleKey, nodeKey) {
|
||||||
@ -22,10 +21,57 @@ function buildTreeNode(nodes, titleKey, nodeKey) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getKeys(treeData) {
|
function findNodes(id, nodes) {
|
||||||
return treeData.map(item => {
|
const res = [];
|
||||||
return item.key;
|
for (let i = 0; i < nodes.length; i += 1) {
|
||||||
});
|
const node = nodes[i];
|
||||||
|
if (node.key === id) {
|
||||||
|
res.push(node.key);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
const childNodes = findNodes(id, node.children);
|
||||||
|
if (childNodes.length) {
|
||||||
|
res.push(node.key);
|
||||||
|
for (let j = 0; j < childNodes.length; j += 1) {
|
||||||
|
res.push(childNodes[j]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
function findAllNodes(resourceIds, nodes) {
|
||||||
|
const findNodesArray = [];
|
||||||
|
for (let i = 0; i < resourceIds.length; i += 1) {
|
||||||
|
const findNodesData = findNodes(resourceIds[i], nodes);
|
||||||
|
if (findNodesData) {
|
||||||
|
for (let j = 0; j < findNodesData.length; j += 1) {
|
||||||
|
const jD = findNodesData[j];
|
||||||
|
if (findNodesArray.indexOf(jD) === -1) {
|
||||||
|
findNodesArray.push(jD);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return findNodesArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
function findCheckedKeys(nodes) {
|
||||||
|
let res = [];
|
||||||
|
for (let i = 0; i < nodes.length; i += 1) {
|
||||||
|
const node = nodes[i];
|
||||||
|
if (node.children) {
|
||||||
|
const findChildrenNodes = findCheckedKeys(node.children);
|
||||||
|
if (findChildrenNodes) {
|
||||||
|
res = res.concat(findChildrenNodes);
|
||||||
|
}
|
||||||
|
} else if (node.assigned === true) {
|
||||||
|
res.push(node.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@ -38,7 +84,6 @@ export default {
|
|||||||
pageSize: 10,
|
pageSize: 10,
|
||||||
|
|
||||||
roleTreeData: [],
|
roleTreeData: [],
|
||||||
resourceTreeData: [],
|
|
||||||
checkedKeys: [],
|
checkedKeys: [],
|
||||||
assignModalLoading: true,
|
assignModalLoading: true,
|
||||||
},
|
},
|
||||||
@ -99,31 +144,16 @@ export default {
|
|||||||
payload: true,
|
payload: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const roleResourceResponse = yield call(queryRoleResourceTree, payload);
|
const response = yield call(queryRoleResourceTree, payload);
|
||||||
const resourceTreeResponse = yield call(resourceTree);
|
const roleResourceTree = response.data;
|
||||||
|
const roleTreeData = buildTreeNode(roleResourceTree, 'displayName', 'id');
|
||||||
const roleTreeData = buildTreeNode(roleResourceResponse.data, 'displayName', 'id');
|
const checkedKeys = findCheckedKeys(roleResourceTree);
|
||||||
const resourceTreeData = buildTreeNode(resourceTreeResponse.data, 'displayName', 'id');
|
|
||||||
const roleTreeIdKeys = getKeys(roleTreeData);
|
|
||||||
const resourceTreeIdKeys = getKeys(resourceTreeData);
|
|
||||||
|
|
||||||
const checkedKeys = roleTreeIdKeys.filter(roleKey => {
|
|
||||||
let res = false;
|
|
||||||
resourceTreeIdKeys.map(key => {
|
|
||||||
if (key === roleKey) {
|
|
||||||
res = true;
|
|
||||||
}
|
|
||||||
return key;
|
|
||||||
});
|
|
||||||
return res;
|
|
||||||
});
|
|
||||||
|
|
||||||
yield put({
|
yield put({
|
||||||
type: 'querySuccess',
|
type: 'querySuccess',
|
||||||
payload: {
|
payload: {
|
||||||
checkedKeys,
|
checkedKeys,
|
||||||
roleTreeData,
|
roleTreeData,
|
||||||
resourceTreeData,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -133,10 +163,12 @@ export default {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
*roleAssignResource({ payload }, { call }) {
|
*roleAssignResource({ payload }, { call }) {
|
||||||
const { id, resourceIds } = payload;
|
const { id, resourceIds, roleTreeData } = payload;
|
||||||
|
const assignNodes = findAllNodes(resourceIds, roleTreeData);
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
id,
|
id,
|
||||||
resourceIds: arrayToStringParams(resourceIds),
|
resourceIds: arrayToStringParams(assignNodes),
|
||||||
};
|
};
|
||||||
const response = yield call(roleAssignResource, params);
|
const response = yield call(roleAssignResource, params);
|
||||||
if (response.code === 0) {
|
if (response.code === 0) {
|
||||||
|
@ -100,6 +100,7 @@ const AssignModal = Form.create()(props => {
|
|||||||
<Tree
|
<Tree
|
||||||
defaultExpandAll={true}
|
defaultExpandAll={true}
|
||||||
checkable={true}
|
checkable={true}
|
||||||
|
multiple={true}
|
||||||
checkedKeys={checkedKeys}
|
checkedKeys={checkedKeys}
|
||||||
onCheck={handleCheckBoxClick}
|
onCheck={handleCheckBoxClick}
|
||||||
>
|
>
|
||||||
@ -212,6 +213,7 @@ class RoleList extends PureComponent {
|
|||||||
payload: {
|
payload: {
|
||||||
id: roleAssignRecord.id,
|
id: roleAssignRecord.id,
|
||||||
resourceIds: data.checkedKeys,
|
resourceIds: data.checkedKeys,
|
||||||
|
roleTreeData: data.roleTreeData,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
this.handleAssignModalVisibleClose(false);
|
this.handleAssignModalVisibleClose(false);
|
||||||
@ -282,7 +284,7 @@ class RoleList extends PureComponent {
|
|||||||
render() {
|
render() {
|
||||||
const { list, data } = this.props;
|
const { list, data } = this.props;
|
||||||
|
|
||||||
const { pageNo, pageSize, count, resourceTreeData, checkedKeys, assignModalLoading } = data;
|
const { pageNo, pageSize, count, roleTreeData, checkedKeys, assignModalLoading } = data;
|
||||||
const { modalVisible, modalType, initValues, roleAssignVisible } = this.state;
|
const { modalVisible, modalType, initValues, roleAssignVisible } = this.state;
|
||||||
|
|
||||||
const parentMethods = {
|
const parentMethods = {
|
||||||
@ -350,7 +352,7 @@ class RoleList extends PureComponent {
|
|||||||
<CreateForm {...parentMethods} modalVisible={modalVisible} />
|
<CreateForm {...parentMethods} modalVisible={modalVisible} />
|
||||||
<AssignModal
|
<AssignModal
|
||||||
loading={assignModalLoading}
|
loading={assignModalLoading}
|
||||||
treeData={resourceTreeData}
|
treeData={roleTreeData}
|
||||||
checkedKeys={checkedKeys}
|
checkedKeys={checkedKeys}
|
||||||
handleOk={this.handleAssignOK}
|
handleOk={this.handleAssignOK}
|
||||||
modalVisible={roleAssignVisible}
|
modalVisible={roleAssignVisible}
|
||||||
|
Loading…
Reference in New Issue
Block a user