import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Button, Col, Form, Input, message, Modal, Row, Tree } from 'antd';
import Panel from '../../../components/Panel';
import Grid from '../../../components/Sword/Grid';
import {
ROLE_LIST,
ROLE_GRANT_TREE,
ROLE_TREE_KEYS,
ROLE_SET_TREE_KEYS,
ROLE_GRANT,
} from '../../../actions/role';
import { MENU_REFRESH_DATA } from '../../../actions/menu';
import { tenantMode } from '../../../defaultSettings';
const FormItem = Form.Item;
const { TreeNode } = Tree;
@connect(({ role, loading }) => ({
role,
loading: loading.models.role,
}))
@Form.create()
class Role extends PureComponent {
state = {
visible: false,
confirmLoading: false,
selectedRows: [],
};
componentWillMount() {
const { dispatch } = this.props;
dispatch(ROLE_GRANT_TREE());
}
onSelectRow = rows => {
this.setState({
selectedRows: rows,
});
};
getSelectKeys = () => {
const { selectedRows } = this.state;
return selectedRows.map(row => row.id);
};
// ============ 查询 ===============
handleSearch = params => {
const { dispatch } = this.props;
dispatch(ROLE_LIST(params));
};
// ========== 权限配置 =============
handleGrant = () => {
const {
role: { roleCheckedTreeKeys },
} = this.props;
if (roleCheckedTreeKeys.length === 0) {
message.warn('权限未变更无需操作');
return false;
}
const keys = this.getSelectKeys();
this.setState({
confirmLoading: true,
});
const { dispatch } = this.props;
dispatch(
ROLE_GRANT({ roleIds: keys[0], menuIds: roleCheckedTreeKeys }, () => {
this.setState({
visible: false,
confirmLoading: false,
});
message.success('配置成功');
dispatch(MENU_REFRESH_DATA());
})
);
return true;
};
showModal = () => {
const keys = this.getSelectKeys();
if (keys.length === 0) {
message.warn('请先选择一条数据!');
return;
}
if (keys.length > 1) {
message.warn('只能选择一条数据!');
return;
}
const { dispatch } = this.props;
dispatch(ROLE_TREE_KEYS({ roleIds: keys[0] }));
this.setState({
visible: true,
});
};
handleCancel = () => {
const { dispatch } = this.props;
dispatch(ROLE_SET_TREE_KEYS({ roleCheckedTreeKeys: [] }));
this.setState({
visible: false,
});
};
onCheck = checkedTreeKeys => {
const { dispatch } = this.props;
dispatch(ROLE_SET_TREE_KEYS({ roleCheckedTreeKeys: checkedTreeKeys }));
};
// ============ 查询表单 ===============
renderSearchForm = onReset => {
const { form } = this.props;
const { getFieldDecorator } = form;
return (
{getFieldDecorator('roleName')()}
{getFieldDecorator('tenantCode')()}
{getFieldDecorator('roleAlias')()}
);
};
renderLeftButton = () => (
);
renderTreeNodes = data =>
data.map(item => {
if (item.children) {
return (
{this.renderTreeNodes(item.children)}
);
}
return ;
});
render() {
const code = 'role';
const { visible, confirmLoading } = this.state;
const {
form,
loading,
role: { data, grantTree, roleCheckedTreeKeys },
} = this.props;
const columns = [
{
title: '租户编号',
dataIndex: 'tenantCode',
},
{
title: '角色名称',
dataIndex: 'roleName',
},
{
title: '角色别名',
dataIndex: 'roleAlias',
},
{
title: '排序',
dataIndex: 'sort',
},
];
if (!tenantMode) {
columns.splice(0, 1);
}
return (
{this.renderTreeNodes(grantTree)}
);
}
}
export default Role;