feat: add action scopes for permissions

This commit is contained in:
chenos 2021-01-16 17:26:21 +08:00
parent 5f5bb0a258
commit 3a3f6fa6ca
3 changed files with 49 additions and 9 deletions

View File

@ -13,7 +13,7 @@ import ViewFactory from '@/components/views'
function transform({value, multiple, labelField, valueField = 'id'}) {
let selectedKeys = [];
let selectedValue = [];
const values = Array.isArray(value) ? value : [];
const values = Array.isArray(value) ? value : [value].filter(Boolean);
selectedKeys = values.map(item => item[valueField]);
selectedValue = values.map(item => {
return {
@ -21,6 +21,7 @@ function transform({value, multiple, labelField, valueField = 'id'}) {
label: item[labelField],
}
});
console.log({selectedKeys, selectedValue, values, labelField, valueField})
if (!multiple) {
return [selectedKeys.shift(), selectedValue.shift()];
}
@ -28,13 +29,13 @@ function transform({value, multiple, labelField, valueField = 'id'}) {
}
export function Scope(props) {
const { target, multiple, associatedName, labelField, valueField = 'id', value, onChange } = props;
const { resourceTarget, target, multiple, associatedName, associatedKey, labelField, valueField = 'id', value, onChange } = props;
const [selectedKeys, selectedValue] = transform({value, multiple, labelField, valueField });
const [visible, setVisible] = useState(false);
const [selectedRowKeys, setSelectedRowKeys] = useState(multiple ? selectedKeys : [selectedKeys]);
const [selectedRows, setSelectedRows] = useState(selectedValue);
const [options, setOptions] = useState(selectedValue);
// console.log('valuevaluevaluevaluevaluevalue', value);
console.log('valuevaluevaluevaluevaluevalue', selectedValue, value);
return (
<>
<Select
@ -44,6 +45,7 @@ export function Scope(props) {
labelInValue
allowClear={true}
value={options}
placeholder={'默认为全部数据'}
notFoundContent={''}
onChange={(data) => {
setOptions(data);
@ -95,7 +97,10 @@ export function Scope(props) {
>
<ViewFactory
multiple={multiple}
associatedName={associatedName}
associatedKey={associatedKey}
resourceName={target}
resourceTarget={resourceTarget}
isFieldComponent={true}
selectedRowKeys={selectedRowKeys}
onSelected={(values) => {

View File

@ -1,12 +1,13 @@
import { connect } from '@formily/react-schema-renderer'
import React, { useEffect, useState } from 'react';
import { Input as AntdInput, Table, Checkbox, Select } from 'antd'
import { Input as AntdInput, Table, Checkbox, Select, Tag } from 'antd'
import { acceptEnum, mapStyledProps, mapTextComponent } from '../shared'
import api from '@/api-client';
import { useRequest } from 'umi';
import { useDynamicList } from 'ahooks';
import findIndex from 'lodash/findIndex';
import get from 'lodash/get';
import set from 'lodash/set';
import { Scope } from './Scope';
export const Permissions = {} as {Actions: any, Fields: any, Tabs: any};
@ -30,7 +31,10 @@ Permissions.Actions = connect({
},
{
title: '类型',
dataIndex: ['onlyNew'],
dataIndex: ['type'],
render: (type) => {
return type === 'create' ? <Tag color={'green'}></Tag> : <Tag color={'blue'}></Tag>;
}
},
{
title: '允许操作',
@ -59,9 +63,40 @@ Permissions.Actions = connect({
{
title: '可操作的数据范围',
dataIndex: ['scope'],
render: (value, record) => <Scope
target={'actions_scopes'} multiple={false} labelField={'title'} valueField={'id'}
render: (scope, record) => {
if (['filter', 'create'].indexOf(record.type) !== -1) {
return null;
}
const values = [...value||[]];
const index = findIndex(values, (item: any) => item && item.name === `${resourceKey}:${record.name}`);
return (
<Scope
resourceTarget={'actions_scopes'}
associatedName={'collections'}
associatedKey={resourceKey}
target={'scopes'}
multiple={false}
labelField={'title'}
valueField={'id'}
value={get(values, [index, 'scope'])}
onChange={(data) => {
const values = [...value||[]];
const index = findIndex(values, (item: any) => item && item.name === `${resourceKey}:${record.name}`);
if (index === -1) {
values.push({
name: `${resourceKey}:${record.name}`,
scope: data,
});
} else {
set(values, [index, 'scope'], data);
}
console.log('valvalvalvalval', {values})
onChange(values);
console.log('valvalvalvalval', data);
}}
/>
)
}
},
]} loading={loading}/>
})

View File

@ -12,7 +12,7 @@ export default {
name: 'title',
title: '名称',
component: {
type: 'filter',
type: 'string',
showInTable: true,
showInForm: true,
},