index.tsx
10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import QxFormRender from '@/packages/qx-form-generator/src/form-render';
import { PermissionTree } from '@/pages/system/role/components/index';
import {
getApps,
getRolePerm,
saveAppPerm,
saveViewPerm,
} from '@/pages/system/role/service';
import '@/pages/system/role/style.less';
import { useForm } from '@qx/form-render';
import { Button, Checkbox, Col, Row } from 'antd';
import { message } from 'antd/es';
import React, { useEffect, useState } from 'react';
const CheckboxGroup = Checkbox.Group;
// 权限设置的schema
const SETTING_SCHEMA = {
type: 'object',
properties: {
allowApplication: {
title: '授权应用',
type: 'string',
placeholder: '请选择',
widget: 'select',
default: 'all',
enum: ['all', 'part'],
enumNames: ['所有应用', '指定应用'],
displayType: 'row',
labelWidth: '80px',
width: '25%',
},
authCode: {
title: '授权权限',
type: 'string',
placeholder: '请选择',
widget: 'select',
default: 'LIST_ALL_EDIT_ALL',
enum: [
'LIST_ALL_EDIT_ALL',
'LIST_ALL_EDIT_MANAGE',
'LIST_ALL_EDIT_OWNER',
'LIST_MANAGE_EDIT_MANAGE',
'LIST_JOIN_EDIT_OWNER',
'LIST_ALL_ONLY',
],
enumNames: [
'可查看及管理所有的记录',
'可查看所有记录,但只能管理自己管辖范围内的记录',
'可查看所有记录,但只能管理自己拥有的记录',
'只能查看和管理自己管辖范围内的所有记录',
'只能查看自己加入的和管理自己拥有的记录',
'只能查看所有记录',
],
displayType: 'row',
labelWidth: '80px',
width: '100%',
hidden: '{{formData.allowApplication==="part"}}',
},
appIds: {
type: 'array',
width: '100%',
widget: 'selectApplication',
hidden: '{{formData.allowApplication==="all"}}',
},
selectPage: {
title: '选择',
type: 'string',
default: 'all',
enum: ['all', 'part'],
enumNames: ['所有的页面', '指定的页面'],
widget: 'radio',
displayType: 'row',
className: 'radio-item',
labelWidth: '80px',
width: '100%',
hidden: '{{formData.allowApplication==="all"}}',
},
authCode1: {
title: '授权权限',
type: 'string',
placeholder: '请选择',
widget: 'select',
default: 'LIST_ALL_EDIT_ALL',
enum: [
'LIST_ALL_EDIT_ALL',
'LIST_ALL_EDIT_MANAGE',
'LIST_ALL_EDIT_OWNER',
'LIST_MANAGE_EDIT_MANAGE',
'LIST_JOIN_EDIT_OWNER',
'LIST_ALL_ONLY',
],
enumNames: [
'可查看及管理所有的记录',
'可查看所有记录,但只能管理自己管辖范围内的记录',
'可查看所有记录,但只能管理自己拥有的记录',
'只能查看和管理自己管辖范围内的所有记录',
'只能查看自己加入的和管理自己拥有的记录',
'只能查看所有记录',
],
displayType: 'row',
labelWidth: '80px',
width: '100%',
// hidden: '{{formData.selectPage==="part"}}'
hidden:
'{{rootValue.allowApplication === "all" || formData.selectPage === "part"}}',
},
roleViewList: {
type: 'array',
width: '100%',
widget: 'permissionTree',
hidden:
'{{rootValue.allowApplication === "all" || formData.selectPage === "all"}}',
},
},
labelWidth: 120,
displayType: 'row',
};
// 选择应用组件
const SelectApplication = (props: any) => {
const { value, onChange, roleId } = props;
const defaultCheckedList: any = [];
const [appOptions, setAppOptions] = useState([]);
const [checkedList, setCheckedList] = useState(defaultCheckedList);
// const [indeterminate, setIndeterminate] = useState(false);
const [checkAll, setCheckAll] = useState(false);
useEffect(() => {
setCheckAll(checkedList?.length >= appOptions?.length);
}, [checkedList, appOptions]);
const onCheckAllChange = (e: any) => {
// @ts-ignore
setCheckedList(e.target.checked ? appOptions.map((item) => item.id) : []);
// setIndeterminate(false);
setCheckAll(e.target.checked);
// @ts-ignore
onChange(e.target.checked ? appOptions.map((item) => item.id) : []);
};
const onCheckitem = (list: any) => {
setCheckedList(list);
// setIndeterminate(!!list.length && list.length < appOptions.length);
setCheckAll(list.length === appOptions.length);
onChange(list);
};
useEffect(() => {
if (value && value.length > 0) {
setCheckedList(value);
} else {
setCheckedList([]);
}
}, [value]);
useEffect(() => {
if (!!roleId) {
// status: 'ENABLED'
getApps({ pageNum: 1, pageSize: 1000 }).then((res) => {
setAppOptions(res?.list || []);
});
}
}, [roleId]);
return (
<div className={'check-wrap'}>
<Checkbox
className={'check-all'}
indeterminate={!checkAll && checkedList?.length}
onChange={onCheckAllChange}
checked={checkAll}
>
全选
</Checkbox>
<div></div>
{/* <CheckboxGroup options={plainOptions} value={checkedList} onChange={onCheckitem} /> */}
<CheckboxGroup
style={{ width: '100%' }}
value={checkedList}
onChange={onCheckitem}
>
<Row>
{appOptions.map((item: any) => {
return (
<Col key={item.id} style={{ width: '14%', marginTop: '20px' }}>
<Checkbox value={item.id}>{item.appName}</Checkbox>
</Col>
);
})}
</Row>
</CheckboxGroup>
</div>
);
};
const SystemRole: React.FC = (props: any) => {
const form = useForm(); // 权限限制的form
const [mount, setMount] = useState(false);
const roleId = props.roleId;
// const roleId = 'qZQ3uYFh4Iw6I8jt1eM'
// 获取对应角色id的权限设置
const _getRolePerm = (_roleId: string) => {
getRolePerm(_roleId).then((res) => {
if (!!res) {
if (res.appIds[0] === 'ALL') {
form.setValues({ allowApplication: 'all', authCode: res.authCode });
} else {
if (!!res.authCode) {
form.setValues({
allowApplication: 'part',
appIds: res.appIds,
authCode1: res.authCode,
});
} else {
form.setValues({
allowApplication: 'part',
appIds: res.appIds,
selectPage: 'part',
roleViewList: res.roleViewList,
});
}
}
}
});
};
useEffect(() => {
if (!!roleId) {
form.resetFields();
_getRolePerm(roleId);
}
}, [roleId]);
// 提交表单
const onFinish = async (formData: any, errors: any) => {
if (errors.length) {
return;
}
console.log(formData, 224);
const {
allowApplication,
authCode,
appIds,
selectPage,
authCode1,
roleViewList,
} = formData;
if (allowApplication === 'all') {
saveAppPerm({ roleId, appIds: ['ALL'], authCode }).then(() => {
message.success('保存成功!');
_getRolePerm(roleId);
});
}
if (allowApplication === 'part' && selectPage === 'all') {
saveAppPerm({ roleId, appIds: appIds || [], authCode: authCode1 }).then(
() => {
message.success('保存成功!');
_getRolePerm(roleId);
},
);
}
if (allowApplication === 'part' && selectPage === 'part') {
let role: any = [];
if (appIds?.length) {
appIds.forEach((el: any) => {
if (roleViewList?.length) {
roleViewList.forEach((bl: any) => {
if (bl.appId === el) {
role.push(bl);
}
});
}
});
} else {
role = [];
}
saveViewPerm({ roleId, appIds: appIds || [], roleViewList: role }).then(
() => {
message.success('保存成功!');
_getRolePerm(roleId);
},
);
}
};
const onMount = () => {
setMount(true);
};
const reset = () => {
const values = form.getValues();
if (!!roleId && mount) {
if (values.selectPage === 'part') {
form.setSchemaByPath('roleViewList', {
props: {
key: roleId,
roleId,
appIds: values.appIds,
roleViewList: values.roleViewList,
selectPage: values.selectPage,
},
});
}
if (values.allowApplication === 'part') {
form.setSchemaByPath('appIds', {
props: { roleId },
});
}
if (values.allowApplication === 'all') {
form.setSchemaByPath('appIds', {
props: { roleId },
});
}
}
};
useEffect(() => {
reset();
}, [roleId, mount]);
return (
<div className="qx-view" style={{ minHeight: 'calc(100% - 88px)' }}>
<section
className="qx-view_content"
style={{ paddingLeft: '5px', backgroundColor: '#fff' }}
>
{roleId?.includes('default_user') && (
<div style={{ padding: '10px 0 0 23px', color: '#999' }}>
默认所有成员均拥有此角色
</div>
)}
<div style={{ padding: '10px 0 0 15px' }}>
<QxFormRender
form={form}
schema={SETTING_SCHEMA}
onFinish={onFinish}
onMount={onMount}
widgets={{
selectApplication: SelectApplication,
permissionTree: PermissionTree,
}}
watch={{
'#': (val) => {
if (!val || Object.keys(val).length === 0) {
return;
}
// reset();
},
appIds: (val) => {
const values = form.getValues();
form.setSchemaByPath('roleViewList', {
props: {
key: roleId,
roleId,
appIds: val,
roleViewList: values.roleViewList,
selectPage: values.selectPage,
},
});
},
selectPage: (val) => {
if (val !== 'part') {
return;
}
const values = form.getValues();
form.setSchemaByPath('roleViewList', {
props: {
key: roleId,
roleId,
appIds: values.appIds,
roleViewList: values.roleViewList,
selectPage: val,
},
});
},
}}
/>
<div style={{ margin: '18px 0' }}>
<Button type="primary" onClick={form.submit}>
保存
</Button>
</div>
</div>
</section>
</div>
);
};
export default SystemRole;