feat: add action scopes for permissions
This commit is contained in:
parent
5f5bb0a258
commit
3a3f6fa6ca
@ -13,7 +13,7 @@ import ViewFactory from '@/components/views'
|
|||||||
function transform({value, multiple, labelField, valueField = 'id'}) {
|
function transform({value, multiple, labelField, valueField = 'id'}) {
|
||||||
let selectedKeys = [];
|
let selectedKeys = [];
|
||||||
let selectedValue = [];
|
let selectedValue = [];
|
||||||
const values = Array.isArray(value) ? value : [];
|
const values = Array.isArray(value) ? value : [value].filter(Boolean);
|
||||||
selectedKeys = values.map(item => item[valueField]);
|
selectedKeys = values.map(item => item[valueField]);
|
||||||
selectedValue = values.map(item => {
|
selectedValue = values.map(item => {
|
||||||
return {
|
return {
|
||||||
@ -21,6 +21,7 @@ function transform({value, multiple, labelField, valueField = 'id'}) {
|
|||||||
label: item[labelField],
|
label: item[labelField],
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
console.log({selectedKeys, selectedValue, values, labelField, valueField})
|
||||||
if (!multiple) {
|
if (!multiple) {
|
||||||
return [selectedKeys.shift(), selectedValue.shift()];
|
return [selectedKeys.shift(), selectedValue.shift()];
|
||||||
}
|
}
|
||||||
@ -28,13 +29,13 @@ function transform({value, multiple, labelField, valueField = 'id'}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function Scope(props) {
|
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 [selectedKeys, selectedValue] = transform({value, multiple, labelField, valueField });
|
||||||
const [visible, setVisible] = useState(false);
|
const [visible, setVisible] = useState(false);
|
||||||
const [selectedRowKeys, setSelectedRowKeys] = useState(multiple ? selectedKeys : [selectedKeys]);
|
const [selectedRowKeys, setSelectedRowKeys] = useState(multiple ? selectedKeys : [selectedKeys]);
|
||||||
const [selectedRows, setSelectedRows] = useState(selectedValue);
|
const [selectedRows, setSelectedRows] = useState(selectedValue);
|
||||||
const [options, setOptions] = useState(selectedValue);
|
const [options, setOptions] = useState(selectedValue);
|
||||||
// console.log('valuevaluevaluevaluevaluevalue', value);
|
console.log('valuevaluevaluevaluevaluevalue', selectedValue, value);
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Select
|
<Select
|
||||||
@ -44,6 +45,7 @@ export function Scope(props) {
|
|||||||
labelInValue
|
labelInValue
|
||||||
allowClear={true}
|
allowClear={true}
|
||||||
value={options}
|
value={options}
|
||||||
|
placeholder={'默认为全部数据'}
|
||||||
notFoundContent={''}
|
notFoundContent={''}
|
||||||
onChange={(data) => {
|
onChange={(data) => {
|
||||||
setOptions(data);
|
setOptions(data);
|
||||||
@ -95,7 +97,10 @@ export function Scope(props) {
|
|||||||
>
|
>
|
||||||
<ViewFactory
|
<ViewFactory
|
||||||
multiple={multiple}
|
multiple={multiple}
|
||||||
|
associatedName={associatedName}
|
||||||
|
associatedKey={associatedKey}
|
||||||
resourceName={target}
|
resourceName={target}
|
||||||
|
resourceTarget={resourceTarget}
|
||||||
isFieldComponent={true}
|
isFieldComponent={true}
|
||||||
selectedRowKeys={selectedRowKeys}
|
selectedRowKeys={selectedRowKeys}
|
||||||
onSelected={(values) => {
|
onSelected={(values) => {
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
import { connect } from '@formily/react-schema-renderer'
|
import { connect } from '@formily/react-schema-renderer'
|
||||||
import React, { useEffect, useState } from 'react';
|
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 { acceptEnum, mapStyledProps, mapTextComponent } from '../shared'
|
||||||
import api from '@/api-client';
|
import api from '@/api-client';
|
||||||
import { useRequest } from 'umi';
|
import { useRequest } from 'umi';
|
||||||
import { useDynamicList } from 'ahooks';
|
import { useDynamicList } from 'ahooks';
|
||||||
import findIndex from 'lodash/findIndex';
|
import findIndex from 'lodash/findIndex';
|
||||||
import get from 'lodash/get';
|
import get from 'lodash/get';
|
||||||
|
import set from 'lodash/set';
|
||||||
import { Scope } from './Scope';
|
import { Scope } from './Scope';
|
||||||
|
|
||||||
export const Permissions = {} as {Actions: any, Fields: any, Tabs: any};
|
export const Permissions = {} as {Actions: any, Fields: any, Tabs: any};
|
||||||
@ -30,7 +31,10 @@ Permissions.Actions = connect({
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: '类型',
|
title: '类型',
|
||||||
dataIndex: ['onlyNew'],
|
dataIndex: ['type'],
|
||||||
|
render: (type) => {
|
||||||
|
return type === 'create' ? <Tag color={'green'}>对新数据操作</Tag> : <Tag color={'blue'}>对已有数据操作</Tag>;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: '允许操作',
|
title: '允许操作',
|
||||||
@ -59,9 +63,40 @@ Permissions.Actions = connect({
|
|||||||
{
|
{
|
||||||
title: '可操作的数据范围',
|
title: '可操作的数据范围',
|
||||||
dataIndex: ['scope'],
|
dataIndex: ['scope'],
|
||||||
render: (value, record) => <Scope
|
render: (scope, record) => {
|
||||||
target={'actions_scopes'} multiple={false} labelField={'title'} valueField={'id'}
|
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}/>
|
]} loading={loading}/>
|
||||||
})
|
})
|
||||||
|
@ -12,7 +12,7 @@ export default {
|
|||||||
name: 'title',
|
name: 'title',
|
||||||
title: '名称',
|
title: '名称',
|
||||||
component: {
|
component: {
|
||||||
type: 'filter',
|
type: 'string',
|
||||||
showInTable: true,
|
showInTable: true,
|
||||||
showInForm: true,
|
showInForm: true,
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user