Info.js
11.1 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
/**
* Author: llw
* Date: 2020.7.20
* Description: [应用详情]
*/
import React, { useEffect } from 'react';
import { connect, Link } from 'umi';
import { Modal, Card, Breadcrumb, Form, Input, Button, Tree, message, Spin } from 'antd';
import { IconFontConfig } from '@/common';
import { EditOutlined, DeleteOutlined, ExclamationCircleOutlined } from '@ant-design/icons';
import ModalSetAuth from './ModalSetAuth';
import { getAuthData, getAllMenuParams, getPermissionList, getDelAuthData, getDragData } from '@/utils/utils';
const { TreeNode } = Tree;
const layout = {labelCol: { span: 3 }, wrapperCol: { span: 12 }};
const ApplicationInfo = props => {
const [form] = Form.useForm();
let {
loading,
isRepeat,
dispatch,
dataModal,
oldInfo = {},
dataAuth = [],
dataMenuList = [],
dataExpandedKeys = [],
location: { query: { id } },
} = props;
/* 获取应用详情 */
useEffect(() => {
if (id) {
dispatch({type: 'Application/getApplicationInfo', payload: {id}}).then(res => {
const { name, url, code, apiDomain } = res.data || {};
form.setFieldsValue({
name, url, code, apiDomain
});
});
}
return (() => {
dispatch({type: 'Application/changeState', payload: {
dataMenuList: [],
dataAuth: [],
dataExpandedKeys: [],
dataApplicationInfo: {}
}})
})
}, []);
/* 添加权限 */
const handleAddTemp = () => {
dispatch({type: 'Application/changeState', payload: {
dataModal: {
modalType: 'AUTH_SET_MODAL',
modalShow: true,
modalData: {isEdit: false}
}
}})
};
/* 关闭弹框 */
const handleCancelModal = () => {
dispatch({type: 'Application/changeState', payload: {oldInfo: {}}});
dispatch({type: 'Application/cancelModal'});
};
/* 保存权限配置 */
const handleAuthSetOk = (values) => {
const { isEdit } = values;
const { allCode = [], allMenuName = [] } = getAllMenuParams(dataAuth);
if (oldInfo.name !== values.name && allMenuName.includes(values.name)) {
message.warning("菜单名称不能重复~")
return false;
}
if (oldInfo.code !== values.code && allCode.includes(values.code)) {
message.warning("code不能重复~")
return false;
}
/* 上述条件不成立时才会走下面生成tree */
let params = {
subPermissionList: [],
...values,
isEdit: undefined,
key: values.code,
oldCode: (oldInfo.parentCode && oldInfo.parentCode !== values.parentCode) ? oldInfo.parentCode : undefined
};
let dataSetAuth =
!isEdit
?
getAuthData(dataAuth, params)
:
getDelAuthData(dataAuth, params, 'EDIT');
const { data, dataMenu, dataExpandedKeys } = dataSetAuth;
handleCancelModal();
dispatch({type: 'Application/changeState', payload: {
dataAuth: JSON.parse(JSON.stringify(data)),
dataMenuList: JSON.parse(JSON.stringify(dataMenu)),
dataExpandedKeys: JSON.parse(JSON.stringify(dataExpandedKeys))
}});
};
// TreeNode的DOM
const renderTreeNode = (data) => {
return data.map((item) => {
if (item.subPermissionList && item.subPermissionList.length) {
return (
<TreeNode
title={
<>
{item.name}
<EditOutlined title="编辑" className="ml-10" onClick={() => handleTreeNode(item, "INFO")} />
<DeleteOutlined title="删除" className="ml-10" onClick={() => handleTreeNode(item, "DEL")} />
</>
}
key={item.code}
icon={<IconFontConfig type={(item.type === 3 || item.type === "BTN") ? "icon-btn" : "icon-menu"} style={{fontSize: '14px'}} />}
>
{renderTreeNode(item.subPermissionList)}
</TreeNode>
)
}
return (
<TreeNode
title={
<>
{item.name}
<EditOutlined title="编辑" className="ml-10" onClick={() => handleTreeNode(item, "INFO")} />
<DeleteOutlined title="删除" className="ml-10" onClick={() => handleTreeNode(item, "DEL")} />
</>
}
key={item.code}
icon={<IconFontConfig type={(item.type === 3 || item.type === "BTN") ? "icon-btn" : "icon-menu"} style={{fontSize: '14px'}} />}
/>
)
})
};
// 点击TreeNode
const handleTreeNode = (item, type) => {
if (type === "INFO") {
dispatch({type: 'Application/changeState', payload: {
oldInfo: {...item},
dataModal: {
modalType: 'AUTH_SET_MODAL',
modalShow: true,
modalData: {...item, isEdit: true}
}
}})
} else if (type === "DEL") {
const { data, dataMenu, dataExpandedKeys } = getDelAuthData(dataAuth, item, 'DEL');
Modal.confirm({
title: '确定删除吗?',
icon: <ExclamationCircleOutlined />,
okText: '确定',
cancelText: '取消',
onOk() {
dispatch({type: 'Application/changeState', payload: {
dataAuth: JSON.parse(JSON.stringify(data)),
dataMenuList: JSON.parse(JSON.stringify(dataMenu)),
dataExpandedKeys: JSON.parse(JSON.stringify(dataExpandedKeys))
}});
}
});
}
};
// 点击Tree
const handleOnExpand = (expandedKeys) => {
dispatch({type: 'Application/changeState', payload: {
dataExpandedKeys: expandedKeys
}});
};
// 保存应用
const handleSaveApplication = () => {
let permissionList = getPermissionList(dataAuth);
form.validateFields().then(values => {
dispatch({type: 'Application/updateApplication', payload: {
...values,
id,
permissionList
}})
})
};
/* 拖拽排序 */
const onDrop = info => {
const dropKey = info.node.props.eventKey;
const dragKey = info.dragNode.props.eventKey;
const dropPos = info.node.props.pos.split('-');
const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
const loop = (data, key, callback) => {
for (let i = 0; i < data.length; i++) {
if (data[i].key === key) {
return callback(data[i], i, data);
}
if (data[i].subPermissionList) {
loop(data[i].subPermissionList, key, callback);
}
}
};
let dataStateAuth = JSON.parse(JSON.stringify(dataAuth));
const data = [...dataStateAuth];
// Find dragObject
let dragObj;
loop(data, dragKey, (item, index, arr) => {
arr.splice(index, 1);
dragObj = item;
});
if (!info.dropToGap) {
// Drop on the content
loop(data, dropKey, item => {
item.subPermissionList = item.subPermissionList || [];
// where to insert 示例添加到尾部,可以是随意位置
item.subPermissionList.push(dragObj);
});
} else if (
(info.node.props.subPermissionList || []).length > 0 &&
info.node.props.expanded &&
dropPosition === 1
) {
loop(data, dropKey, item => {
item.subPermissionList = item.subPermissionList || [];
// where to insert 示例添加到头部,可以是随意位置
item.subPermissionList.unshift(dragObj);
});
} else {
let ar;
let i;
loop(data, dropKey, (item, index, arr) => {
ar = arr;
i = index;
});
if (dropPosition === -1) {
ar.splice(i, 0, dragObj);
} else {
ar.splice(i + 1, 0, dragObj);
}
}
let { dataDragAuth, dataMenu, dataExpandedKeys } = getDragData(data);
dispatch({type: 'Application/changeState', payload: {
dataAuth: JSON.parse(JSON.stringify(dataDragAuth.length > 0 ? dataDragAuth : dataAuth)),
dataMenuList: JSON.parse(JSON.stringify(dataMenu)),
dataExpandedKeys: JSON.parse(JSON.stringify(dataExpandedKeys))
}});
};
/* 清空所有权限 */
const handleRemoveAuth = () => {
Modal.confirm({
title: '确定清空该应用权限的相关数据吗?',
icon: <ExclamationCircleOutlined />,
okText: '确定',
cancelText: '取消',
onOk() {
dispatch({type: 'Application/changeState', payload: {
dataAuth: [],
dataMenuList: [],
dataExpandedKeys: []
}});
}
});
};
return (
<div className="box-info">
<Breadcrumb>
<Breadcrumb.Item><Link to="/appliction">应用列表</Link></Breadcrumb.Item>
<Breadcrumb.Item>{!id ? '新建应用' : '编辑应用'}</Breadcrumb.Item>
</Breadcrumb>
<Spin spinning={id ? loading : false}>
<Card bordered={false} className="mt-15">
<Form
{...layout}
form={form}
name="applicationInfo"
>
<Form.Item
label="应用名称"
name="name"
rules={[{ required: true, message: '请输入应用名称' }]}
>
<Input placeholder="请输入应用名称" />
</Form.Item>
<Form.Item
label="应用标识码"
name="code"
>
<Input placeholder="请输入应用标识码" />
</Form.Item>
<Form.Item
label="应用Api域名地址"
name="apiDomain"
>
<Input placeholder="请输入应用api域名地址" />
</Form.Item>
<Form.Item
label="应用链接地址"
name="url"
>
<Input placeholder="请输入应用链接地址" />
</Form.Item>
<Form.Item label="权限服务模板">
<Button onClick={handleAddTemp}>添加</Button>
</Form.Item>
<Form.Item label="权限预览" className="view-tree-auth">
{
dataAuth.length > 0 ?
(
<Tree
showLine
showIcon
draggable
selectedKeys={[]}
className="info-tree"
onExpand={handleOnExpand}
expandedKeys={dataExpandedKeys}
onDrop={onDrop}
>
{renderTreeNode(dataAuth)}
</Tree>
)
:
<span>暂无数据</span>
}
</Form.Item>
<Form.Item className="footer-btn" wrapperCol={{span: 24}}>
<Button disabled={!dataAuth.length} onClick={handleRemoveAuth} className="mr-10">清空权限</Button>
<Button loading={isRepeat} onClick={handleSaveApplication} type="primary" className="ml-10">保存应用</Button>
</Form.Item>
</Form>
</Card>
{/* 设置权限抽屉 */}
<ModalSetAuth
dataModal={dataModal}
dataMenuList={dataMenuList}
handleAuthSetOk={handleAuthSetOk}
handleCancelModal={handleCancelModal}
/>
</Spin>
</div>
)
};
export default connect(({ Application, loading }) => ({
...Application,
isRepeat: loading.effects["Application/updateApplication"],
loading: loading.effects["Application/getApplicationInfo"]
}))(ApplicationInfo);