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
|
import React , { useEffect, useReducer, useRef, useState }from 'react'
import { Table, Space,Modal ,message,Input,Form ,Button} from 'antd';
import { getRoleList, insertRole, updateRole ,deleteRole} from '../../../api/role';
const { confirm } = Modal
const InnerTable = (props) => {
const [roleList,setRoleList] = useState([])
//pageSize -> 请求 5页
const [pageInfo,setPageInfo] = useState({
pageSize:5,
current:1,
total:200,
})
//点击分页
const RequestPage = ({current,pageSize}) => {
setPageInfo({
current,
pageSize
})
}
// useEffect(()=> {
// refreshPage()
// },pageInfo)
useEffect(()=>{
let mounted = true
getRoleList({
roleName:'',
page:pageInfo.current,
size:pageInfo.pageSize
}).then(({json,r ,code})=> {
if(mounted) {
setPageInfo({
pageSize:r.size,
current:r.curPage,
total:r.totalCount,
})
setRoleList(r.data)
}
})
return ()=> {
mounted = false;
}
},[pageInfo.current,pageInfo.pageSize])
// 字段定义
const handleDel = ({
roleId
}) => {
confirm({
title:'确认删除?',
onOk() {
deleteRole({roleId})
.then( ({code}) => {
if(code != 200 ) {
message.error("系统自带的用户,无法删除")
}else {
message.info('删除成功')
refreshPage();
}
})
},
onCancel() {
}
})
}
const formRef = useRef(null)
//更新完成后重新请求
const [updateModal, setModalState] = useReducer((state,data)=> {
switch (data.type) {
case "show": {
// formRef.current?.setFieldsValue({
// ...data.record
// })
return {
...state,
showModal:true
}
}
case "hide": {
return {
...state,
showModal:false
}
}
case 'update': {
// console.log(data.record,state)
formRef.current?.setFieldsValue({
...data.record
})
return {
...state,
...data.record,
showModal:true,
title:'更新角色',
}
}
case 'insert': {
formRef.current?.setFieldsValue({
roleId:'',
roleName:'',
roleDescription:'',
})
return {
showModal:true,
title:'新增角色',
}
}
}
return {...state}
},{
showModal:false,
roleName:'-',
roleDescription:'-',
roleId:null,
title:'更新角色',
})
const refreshPage = ()=> {
getRoleList({
roleName:'',
page:pageInfo.current,
size:pageInfo.pageSize
}).then(({json,r ,code})=> {
setPageInfo({
pageSize:r.size,
current:r.curPage,
total:r.totalCount,
})
setRoleList(r.data)
})
}
const updateRequest = () => {
//如果有主键
if(updateModal.roleId) {
updateRole({
...updateModal
}).then( ({json,r,code}) => {
if(code == 200) {
message.info('更新成功');
}else {
message.error('数据更新异常')
}
refreshPage()
}).catch((err) =>{
message.error("网络问题,请重试")
})
}else {
//插入数据
insertRole({
...updateModal
}).then( ({code}) => {
if (code ==200) {
message.info('添加数据成功')
}else {
message.error('添加失败');
}
refreshPage()
})
}
}
const columns = [
{
title: '角色名',
key: 'roleId',
dataIndex:'roleName',
render: text => <a>{text}</a>,
},
{
title: '描述',
key: 'roleDescription',
dataIndex:'roleDescription',
render: text => <a>{text}</a>,
},
{
title: '操作',
key: 'action',
render: (text, record) => (
<Space size="middle">
<a onClick={()=> {
console.log(record)
setModalState({type:"update",record})
}}>编辑</a>
<a onClick={ () => handleDel(record)}>删除</a>
</Space>
),
},
];
return (
<>
<Modal
title={updateModal.title}
visible={updateModal.showModal}
onOk={e => {setModalState({type:"hide"});updateRequest() } }
onCancel={e => { setModalState({type:'hide'})} }
>
{/* <div>
<Input placeholder="用户角色" value={currentRole.roleName} />
<Input placeholder="角色描述" value={currentRole.roleDescription} />
</div> */}
<div>
<Form ref={formRef}
initialValues={updateModal}
onValuesChange={record =>{setModalState({type:'update',record})}}
>
<Form.Item
label="用户角色"
name="roleName"
rules={[{ required: false, message: '角色' }]}
>
<Input />
</Form.Item>
<Form.Item
label="角色描述"
name="roleDescription"
rules={[{ required: false, message: '角色' }]}
>
<Input />
</Form.Item>
</Form>
</div>
</Modal>
<br/>
<Button type="primary" style={{float:'right'}}
onClick={e=>setModalState({type:'insert',record:{}})}
>
添加角色
</Button>
<br/>
<br />
<Table columns={columns} dataSource={roleList} {...props}
rowKey="roleId"
pagination={
{
...pageInfo,
pageSizeOptions:[5,10,15],
defaultPageSize:5
}
}
onChange={RequestPage}
/>
</>
)
}
const RoleManager = (props) => {
return (
<div>
<InnerTable {...props} />
</div>
)
}
export default RoleManager
|