Commit 613ea9d4 authored by superman's avatar superman

2

parent 712bedf8
This diff is collapsed.
This diff is collapsed.
......@@ -51,11 +51,8 @@ export default class EditItem extends Component {
constructor() {
super(...arguments);
this.state = {
isEdit: false,
permissions: {},
expandedKeys:[],
autoExpandParent: true,
checkedKeys:[]
resourcesCategoryMap:{},
checkedKeys: []
}
}
......@@ -66,12 +63,10 @@ export default class EditItem extends Component {
};
componentWillReceiveProps(nextProps) {
const state = {isEdit: nextProps.isEdit};
if (nextProps.item && nextProps.item.permissions && nextProps.resources) {
state.permissions = nextProps.item.permissions;
console.log('state.permissions', state.permissions);
this.analysisCheckedKeys(nextProps);
console.log('analysisCheckedKeys');
}
this.setState(state);
};
fetchItem(id) {
......@@ -93,7 +88,14 @@ export default class EditItem extends Component {
let data = {};
data = this.props.form.getFieldsValue();
data.id = this.props.params.id;
data.permissions = JSON.stringify(this.state.permissions);
const permissions = {};
this.state.checkedKeys.forEach(key=>{
const [ka, cn, mask] = key.split('-');
if(ka === 'a' && mask){
permissions[cn] =( permissions[cn]|| 0) + parseInt(mask, 10);
}
});
data.permissions = JSON.stringify(permissions);
if (isNaN(data.status)) {
ENABLE_STATUS_LIST.forEach(item=> {
if (item.text === data.status) {
......@@ -108,28 +110,55 @@ export default class EditItem extends Component {
});
}
handleExpand(expandedKeys){
console.log('onExpand', arguments);
this.setState({
expandedKeys,
autoExpandParent: false
analysisCheckedKeys(props) {
const {item:{permissions}, resources, isEdit} = props;
//对权限资源分组
const resourcesCategoryMap = {};
const checkedKeys = [];
resources && resources.forEach(res=> {
if (res.actionMask && res.status == 9) {
const category = res.category || '其他';
const map = resourcesCategoryMap[category] || [];
//角色拥有权限
const permisMask = permissions && permissions[res.controllerName] || 0;
const permisAction = permisMask && resourceActions(permisMask) || [];
const permisActionMap = {};
if (permisAction.length) {
permisAction.forEach(pac=> {
permisActionMap[pac.mask] = pac;
});
}
if (isEdit) {
res.action = resourceActions(res.actionMask);
res.action.forEach(ac=> {
if (permisActionMap[ac.mask]) {
ac.checked = true;
checkedKeys.push(['a', res.controllerName, ac.mask].join('-'));
}
});
} else {
res.action = permisAction;
}
if (isEdit || permisAction.length) {
map.push(res);
resourcesCategoryMap[category] = map;
}
}
});
}
handleCheck(checkedKeys){
this.setState({
resourcesCategoryMap,
checkedKeys
});
}
render() {
const {item, resources, loading, form:{getFieldProps}, location:{query}} = this.props;
const {isEdit} = this.state;
const {item, resources, loading, form:{getFieldProps}, location:{query}, isEdit} = this.props;
const operation = (
<div style={{textAlign:'right'}}>
......@@ -166,53 +195,6 @@ export default class EditItem extends Component {
/>
);
//对权限资源分组
const resourcesCategoryMap = {};
//角色权限
const {permissions} = this.state;
const checkedKeys = [];
resources.forEach(res=> {
if (res.actionMask && res.status == 9) {
const category = res.category || '其他';
const map = resourcesCategoryMap[category] || [];
//角色拥有权限
const permisMask = permissions[res.controllerName];
const permisAction = permisMask && resourceActions(permisMask) || [];
const permisActionMap = {};
if (permisAction.length) {
permisAction.forEach(pac=> {
permisActionMap[pac.mask] = pac;
});
}
if (isEdit) {
res.action = resourceActions(res.actionMask);
res.action.forEach(ac=> {
if (permisActionMap[ac.mask]) {
ac.checked = true;
checkedKeys.push(['a', res.controllerName, ac.mask].join('-'));
}
});
} else {
res.action = permisAction;
}
if (isEdit || permisAction.length) {
map.push(res);
resourcesCategoryMap[category] = map;
}
}
});
console.log(permissions, '选中的:', checkedKeys);
return (
<Layout header={header}>
<Spin spinning={loading}>
......@@ -289,31 +271,13 @@ export default class EditItem extends Component {
<Tree checkable={isEdit}
defaultExpandAll={true}
checkedKeys={this.state.checkedKeys}
onClick={
(cks)=>{
const cs = {};
cks.forEach(key=>{
const [ac, cn, mask] = key.split('-');
if(ac === 'a' && typeof mask !== 'undefined'){
let _mask = parseInt(mask, 10);
cs[cn] = (cs[cn] || 0) + _mask;
}
});
console.log(cs);
this.setState({
checkedKeys: cks,
permissions: cs,
})
}
}
>
onCheck={(checkedKeys)=>{this.setState({checkedKeys});}}>
{
Object.keys(resourcesCategoryMap).map((key, index)=>
Object.keys(this.state.resourcesCategoryMap).map((key, index)=>
<Tree.TreeNode className="resource-category" key={'c-'+key}
title={key}>
{
resourcesCategoryMap[key].map(res=>
this.state.resourcesCategoryMap[key].map(res=>
<Tree.TreeNode className="resource-item"
key={
res.action.length===1 ?
......
......@@ -116,8 +116,8 @@ export default class List extends Component {
width: 180,
}, {
title: '权限值',
dataIndex: 'actionName',
key: 'actionName',
dataIndex: 'actionMask',
key: 'actionMask',
width: 80,
className: 'tac'
}, {
......@@ -140,16 +140,6 @@ export default class List extends Component {
render: (status)=> (
<span>{enableStatusToString(status)}</span>
)
}, {
title: '操作',
key: 'operation',
width: 120,
className: 'tac',
render: (text, item)=>(
<span onClick={e=>{e.stopPropagation(); e.preventDefault();}}>
</span>
)
}
];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment