Jiarenxiang | 38dcb05 | 2025-03-13 16:40:09 +0800 | [diff] [blame^] | 1 | import React, { useEffect, useState } from 'react'; |
| 2 | import { Checkbox, Col, Form, Modal, Row, Tree } from 'antd'; |
| 3 | import { FormattedMessage, useIntl } from '@umijs/max'; |
| 4 | import { Key, ProForm, ProFormDigit, ProFormSelect, ProFormText } from '@ant-design/pro-components'; |
| 5 | import { DataNode } from 'antd/es/tree'; |
| 6 | import { CheckboxValueType } from 'antd/es/checkbox/Group'; |
| 7 | |
| 8 | /* * |
| 9 | * |
| 10 | * @author whiteshader@163.com |
| 11 | * @datetime 2023/02/06 |
| 12 | * |
| 13 | * */ |
| 14 | |
| 15 | export type FormValueType = any & Partial<API.System.Dept>; |
| 16 | |
| 17 | export type DataScopeFormProps = { |
| 18 | onCancel: (flag?: boolean, formVals?: FormValueType) => void; |
| 19 | onSubmit: (values: FormValueType) => Promise<void>; |
| 20 | open: boolean; |
| 21 | values: Partial<API.System.Role>; |
| 22 | deptTree: DataNode[]; |
| 23 | deptCheckedKeys: string[]; |
| 24 | }; |
| 25 | |
| 26 | const DataScopeForm: React.FC<DataScopeFormProps> = (props) => { |
| 27 | const [form] = Form.useForm(); |
| 28 | |
| 29 | const { deptTree, deptCheckedKeys } = props; |
| 30 | const [dataScopeType, setDataScopeType] = useState<string | undefined>('1'); |
| 31 | const [deptIds, setDeptIds] = useState<string[] | {checked: string[], halfChecked: string[]}>([]); |
| 32 | const [deptTreeExpandKey, setDeptTreeExpandKey] = useState<Key[]>([]); |
| 33 | const [checkStrictly, setCheckStrictly] = useState<boolean>(true); |
| 34 | |
| 35 | |
| 36 | useEffect(() => { |
| 37 | setDeptIds(deptCheckedKeys); |
| 38 | form.resetFields(); |
| 39 | form.setFieldsValue({ |
| 40 | roleId: props.values.roleId, |
| 41 | roleName: props.values.roleName, |
| 42 | roleKey: props.values.roleKey, |
| 43 | dataScope: props.values.dataScope, |
| 44 | }); |
| 45 | setDataScopeType(props.values.dataScope); |
| 46 | }, [props.values]); |
| 47 | |
| 48 | const intl = useIntl(); |
| 49 | const handleOk = () => { |
| 50 | form.submit(); |
| 51 | }; |
| 52 | const handleCancel = () => { |
| 53 | props.onCancel(); |
| 54 | }; |
| 55 | const handleFinish = async (values: Record<string, any>) => { |
| 56 | props.onSubmit({ ...values, deptIds } as FormValueType); |
| 57 | }; |
| 58 | |
| 59 | const getAllDeptNode = (node: DataNode[]) => { |
| 60 | let keys: any[] = []; |
| 61 | node.forEach(value => { |
| 62 | keys.push(value.key); |
| 63 | if(value.children) { |
| 64 | keys = keys.concat(getAllDeptNode(value.children)); |
| 65 | } |
| 66 | }); |
| 67 | return keys; |
| 68 | } |
| 69 | |
| 70 | const deptAllNodes = getAllDeptNode(deptTree); |
| 71 | |
| 72 | |
| 73 | const onDeptOptionChange = (checkedValues: CheckboxValueType[]) => { |
| 74 | if(checkedValues.includes('deptExpand')) { |
| 75 | setDeptTreeExpandKey(deptAllNodes); |
| 76 | } else { |
| 77 | setDeptTreeExpandKey([]); |
| 78 | } |
| 79 | if(checkedValues.includes('deptNodeAll')) { |
| 80 | setDeptIds(deptAllNodes); |
| 81 | } else { |
| 82 | setDeptIds([]); |
| 83 | } |
| 84 | |
| 85 | if(checkedValues.includes('deptCheckStrictly')) { |
| 86 | setCheckStrictly(false); |
| 87 | } else { |
| 88 | setCheckStrictly(true); |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | return ( |
| 93 | <Modal |
| 94 | width={640} |
| 95 | title={intl.formatMessage({ |
| 96 | id: 'system.user.auth.role', |
| 97 | defaultMessage: '分配角色', |
| 98 | })} |
| 99 | open={props.open} |
| 100 | destroyOnClose |
| 101 | forceRender |
| 102 | onOk={handleOk} |
| 103 | onCancel={handleCancel} |
| 104 | > |
| 105 | <ProForm |
| 106 | form={form} |
| 107 | grid={true} |
| 108 | layout="horizontal" |
| 109 | onFinish={handleFinish} |
| 110 | initialValues={{ |
| 111 | login_password: '', |
| 112 | confirm_password: '', |
| 113 | }} |
| 114 | > |
| 115 | |
| 116 | <ProFormDigit |
| 117 | name="roleId" |
| 118 | label={intl.formatMessage({ |
| 119 | id: 'system.role.role_id', |
| 120 | defaultMessage: '角色编号', |
| 121 | })} |
| 122 | colProps={{ md: 12, xl: 12 }} |
| 123 | placeholder="请输入角色编号" |
| 124 | disabled |
| 125 | hidden={true} |
| 126 | rules={[ |
| 127 | { |
| 128 | required: false, |
| 129 | message: <FormattedMessage id="请输入角色编号!" defaultMessage="请输入角色编号!" />, |
| 130 | }, |
| 131 | ]} |
| 132 | /> |
| 133 | <ProFormText |
| 134 | name="roleName" |
| 135 | label={intl.formatMessage({ |
| 136 | id: 'system.role.role_name', |
| 137 | defaultMessage: '角色名称', |
| 138 | })} |
| 139 | disabled |
| 140 | placeholder="请输入角色名称" |
| 141 | rules={[ |
| 142 | { |
| 143 | required: true, |
| 144 | message: <FormattedMessage id="请输入角色名称!" defaultMessage="请输入角色名称!" />, |
| 145 | }, |
| 146 | ]} |
| 147 | /> |
| 148 | <ProFormText |
| 149 | name="roleKey" |
| 150 | label={intl.formatMessage({ |
| 151 | id: 'system.role.role_key', |
| 152 | defaultMessage: '权限字符串', |
| 153 | })} |
| 154 | disabled |
| 155 | placeholder="请输入角色权限字符串" |
| 156 | rules={[ |
| 157 | { |
| 158 | required: true, |
| 159 | message: <FormattedMessage id="请输入角色权限字符串!" defaultMessage="请输入角色权限字符串!" />, |
| 160 | }, |
| 161 | ]} |
| 162 | /> |
| 163 | <ProFormSelect |
| 164 | name="dataScope" |
| 165 | label='权限范围' |
| 166 | initialValue={'1'} |
| 167 | placeholder="请输入用户性别" |
| 168 | valueEnum={{ |
| 169 | "1": "全部数据权限", |
| 170 | "2": "自定数据权限", |
| 171 | "3": "本部门数据权限", |
| 172 | "4": "本部门及以下数据权限", |
| 173 | "5": "仅本人数据权限" |
| 174 | }} |
| 175 | rules={[ |
| 176 | { |
| 177 | required: true, |
| 178 | }, |
| 179 | ]} |
| 180 | fieldProps={{ |
| 181 | onChange: (value) => { |
| 182 | setDataScopeType(value); |
| 183 | }, |
| 184 | }} |
| 185 | /> |
| 186 | <ProForm.Item |
| 187 | name="deptIds" |
| 188 | label={intl.formatMessage({ |
| 189 | id: 'system.role.auth', |
| 190 | defaultMessage: '菜单权限', |
| 191 | })} |
| 192 | required={dataScopeType === '1'} |
| 193 | hidden={dataScopeType !== '1'} |
| 194 | > |
| 195 | <Row gutter={[16, 16]}> |
| 196 | <Col md={24}> |
| 197 | <Checkbox.Group |
| 198 | options={[ |
| 199 | { label: '展开/折叠', value: 'deptExpand' }, |
| 200 | { label: '全选/全不选', value: 'deptNodeAll' }, |
| 201 | // { label: '父子联动', value: 'deptCheckStrictly' }, |
| 202 | ]} |
| 203 | onChange={onDeptOptionChange} /> |
| 204 | </Col> |
| 205 | <Col md={24}> |
| 206 | <Tree |
| 207 | checkable={true} |
| 208 | checkStrictly={checkStrictly} |
| 209 | expandedKeys={deptTreeExpandKey} |
| 210 | treeData={deptTree} |
| 211 | checkedKeys={deptIds} |
| 212 | defaultCheckedKeys={deptCheckedKeys} |
| 213 | onCheck={(checkedKeys: any, checkInfo: any) => { |
| 214 | console.log(checkedKeys, checkInfo); |
| 215 | if(checkStrictly) { |
| 216 | return setDeptIds(checkedKeys.checked); |
| 217 | } else { |
| 218 | return setDeptIds({checked: checkedKeys, halfChecked: checkInfo.halfCheckedKeys}); |
| 219 | } |
| 220 | }} |
| 221 | onExpand={(expandedKeys: Key[]) => { |
| 222 | setDeptTreeExpandKey(deptTreeExpandKey.concat(expandedKeys)); |
| 223 | }} |
| 224 | /> |
| 225 | </Col> |
| 226 | </Row> |
| 227 | </ProForm.Item> |
| 228 | </ProForm> |
| 229 | </Modal> |
| 230 | ); |
| 231 | }; |
| 232 | |
| 233 | export default DataScopeForm; |