86133 | aaa3f5d | 2025-04-20 21:33:29 +0800 | [diff] [blame] | 1 | |
| 2 | import React, { useState, useRef, useEffect } from 'react'; |
| 3 | import { useIntl, FormattedMessage, useAccess } from '@umijs/max'; |
| 4 | import type { FormInstance } from 'antd'; |
| 5 | import { Button, message, Modal } from 'antd'; |
| 6 | import { ActionType, FooterToolbar, PageContainer, ProColumns, ProTable } from '@ant-design/pro-components'; |
| 7 | import { PlusOutlined, DeleteOutlined, ExclamationCircleOutlined } from '@ant-design/icons'; |
| 8 | import { getPostList, removePost, addPost, updatePost, exportPost } from '@/services/system/post'; |
| 9 | import UpdateForm from './edit'; |
| 10 | import { getDictValueEnum } from '@/services/system/dict'; |
| 11 | import DictTag from '@/components/DictTag'; |
| 12 | |
| 13 | /** |
| 14 | * 添加节点 |
| 15 | * |
| 16 | * @param fields |
| 17 | */ |
| 18 | const handleAdd = async (fields: API.System.Post) => { |
| 19 | const hide = message.loading('正在添加'); |
| 20 | try { |
| 21 | const resp = await addPost({ ...fields }); |
| 22 | hide(); |
| 23 | if (resp.code === 200) { |
| 24 | message.success('添加成功'); |
| 25 | } else { |
| 26 | message.error(resp.msg); |
| 27 | } |
| 28 | return true; |
| 29 | } catch (error) { |
| 30 | hide(); |
| 31 | message.error('添加失败请重试!'); |
| 32 | return false; |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * 更新节点 |
| 38 | * |
| 39 | * @param fields |
| 40 | */ |
| 41 | const handleUpdate = async (fields: API.System.Post) => { |
| 42 | const hide = message.loading('正在更新'); |
| 43 | try { |
| 44 | const resp = await updatePost(fields); |
| 45 | hide(); |
| 46 | if (resp.code === 200) { |
| 47 | message.success('更新成功'); |
| 48 | } else { |
| 49 | message.error(resp.msg); |
| 50 | } |
| 51 | return true; |
| 52 | } catch (error) { |
| 53 | hide(); |
| 54 | message.error('配置失败请重试!'); |
| 55 | return false; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * 删除节点 |
| 61 | * |
| 62 | * @param selectedRows |
| 63 | */ |
| 64 | const handleRemove = async (selectedRows: API.System.Post[]) => { |
| 65 | const hide = message.loading('正在删除'); |
| 66 | if (!selectedRows) return true; |
| 67 | try { |
| 68 | const resp = await removePost(selectedRows.map((row) => row.postId).join(',')); |
| 69 | hide(); |
| 70 | if (resp.code === 200) { |
| 71 | message.success('删除成功,即将刷新'); |
| 72 | } else { |
| 73 | message.error(resp.msg); |
| 74 | } |
| 75 | return true; |
| 76 | } catch (error) { |
| 77 | hide(); |
| 78 | message.error('删除失败,请重试'); |
| 79 | return false; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | const handleRemoveOne = async (selectedRow: API.System.Post) => { |
| 84 | const hide = message.loading('正在删除'); |
| 85 | if (!selectedRow) return true; |
| 86 | try { |
| 87 | const params = [selectedRow.postId]; |
| 88 | const resp = await removePost(params.join(',')); |
| 89 | hide(); |
| 90 | if (resp.code === 200) { |
| 91 | message.success('删除成功,即将刷新'); |
| 92 | } else { |
| 93 | message.error(resp.msg); |
| 94 | } |
| 95 | return true; |
| 96 | } catch (error) { |
| 97 | hide(); |
| 98 | message.error('删除失败,请重试'); |
| 99 | return false; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | /** |
| 104 | * 导出数据 |
| 105 | * |
| 106 | * |
| 107 | */ |
| 108 | const handleExport = async () => { |
| 109 | const hide = message.loading('正在导出'); |
| 110 | try { |
| 111 | await exportPost(); |
| 112 | hide(); |
| 113 | message.success('导出成功'); |
| 114 | return true; |
| 115 | } catch (error) { |
| 116 | hide(); |
| 117 | message.error('导出失败,请重试'); |
| 118 | return false; |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | |
| 123 | const PostTableList: React.FC = () => { |
| 124 | const formTableRef = useRef<FormInstance>(); |
| 125 | |
| 126 | const [modalVisible, setModalVisible] = useState<boolean>(false); |
| 127 | |
| 128 | const actionRef = useRef<ActionType>(); |
| 129 | const [currentRow, setCurrentRow] = useState<API.System.Post>(); |
| 130 | const [selectedRows, setSelectedRows] = useState<API.System.Post[]>([]); |
| 131 | |
| 132 | const [statusOptions, setStatusOptions] = useState<any>([]); |
| 133 | |
| 134 | const access = useAccess(); |
| 135 | |
| 136 | /** 国际化配置 */ |
| 137 | const intl = useIntl(); |
| 138 | |
| 139 | useEffect(() => { |
| 140 | getDictValueEnum('sys_normal_disable').then((data) => { |
| 141 | setStatusOptions(data); |
| 142 | }); |
| 143 | }, []); |
| 144 | |
| 145 | const columns: ProColumns<API.System.Post>[] = [ |
| 146 | { |
| 147 | title: <FormattedMessage id="system.post.post_id" defaultMessage="岗位编号" />, |
| 148 | dataIndex: 'postId', |
| 149 | valueType: 'text', |
| 150 | }, |
| 151 | { |
| 152 | title: <FormattedMessage id="system.post.post_code" defaultMessage="岗位编码" />, |
| 153 | dataIndex: 'postCode', |
| 154 | valueType: 'text', |
| 155 | }, |
| 156 | { |
| 157 | title: <FormattedMessage id="system.post.post_name" defaultMessage="岗位名称" />, |
| 158 | dataIndex: 'postName', |
| 159 | valueType: 'text', |
| 160 | }, |
| 161 | { |
| 162 | title: <FormattedMessage id="system.post.post_sort" defaultMessage="显示顺序" />, |
| 163 | dataIndex: 'postSort', |
| 164 | valueType: 'text', |
| 165 | hideInSearch: true, |
| 166 | }, |
| 167 | { |
| 168 | title: <FormattedMessage id="system.post.status" defaultMessage="状态" />, |
| 169 | dataIndex: 'status', |
| 170 | valueType: 'select', |
| 171 | valueEnum: statusOptions, |
| 172 | render: (_, record) => { |
| 173 | return (<DictTag enums={statusOptions} value={record.status} />); |
| 174 | }, |
| 175 | }, |
| 176 | { |
| 177 | title: <FormattedMessage id="pages.searchTable.titleOption" defaultMessage="操作" />, |
| 178 | dataIndex: 'option', |
| 179 | width: '220px', |
| 180 | valueType: 'option', |
| 181 | render: (_, record) => [ |
| 182 | <Button |
| 183 | type="link" |
| 184 | size="small" |
| 185 | key="edit" |
| 186 | hidden={!access.hasPerms('system:post:edit')} |
| 187 | onClick={() => { |
| 188 | setModalVisible(true); |
| 189 | setCurrentRow(record); |
| 190 | }} |
| 191 | > |
| 192 | 编辑 |
| 193 | </Button>, |
| 194 | <Button |
| 195 | type="link" |
| 196 | size="small" |
| 197 | danger |
| 198 | key="batchRemove" |
| 199 | hidden={!access.hasPerms('system:post:remove')} |
| 200 | onClick={async () => { |
| 201 | Modal.confirm({ |
| 202 | title: '删除', |
| 203 | content: '确定删除该项吗?', |
| 204 | okText: '确认', |
| 205 | cancelText: '取消', |
| 206 | onOk: async () => { |
| 207 | const success = await handleRemoveOne(record); |
| 208 | if (success) { |
| 209 | if (actionRef.current) { |
| 210 | actionRef.current.reload(); |
| 211 | } |
| 212 | } |
| 213 | }, |
| 214 | }); |
| 215 | }} |
| 216 | > |
| 217 | 删除 |
| 218 | </Button>, |
| 219 | ], |
| 220 | }, |
| 221 | ]; |
| 222 | |
| 223 | return ( |
| 224 | <PageContainer> |
| 225 | <div style={{ width: '100%', float: 'right' }}> |
| 226 | <ProTable<API.System.Post> |
| 227 | headerTitle={intl.formatMessage({ |
| 228 | id: 'pages.searchTable.title', |
| 229 | defaultMessage: '信息', |
| 230 | })} |
| 231 | actionRef={actionRef} |
| 232 | formRef={formTableRef} |
| 233 | rowKey="postId" |
| 234 | key="postList" |
| 235 | search={{ |
| 236 | labelWidth: 120, |
| 237 | }} |
| 238 | toolBarRender={() => [ |
| 239 | <Button |
| 240 | type="primary" |
| 241 | key="add" |
| 242 | hidden={!access.hasPerms('system:post:add')} |
| 243 | onClick={async () => { |
| 244 | setCurrentRow(undefined); |
| 245 | setModalVisible(true); |
| 246 | }} |
| 247 | > |
| 248 | <PlusOutlined /> <FormattedMessage id="pages.searchTable.new" defaultMessage="新建" /> |
| 249 | </Button>, |
| 250 | <Button |
| 251 | type="primary" |
| 252 | key="remove" |
| 253 | danger |
| 254 | hidden={selectedRows?.length === 0 || !access.hasPerms('system:post:remove')} |
| 255 | onClick={async () => { |
| 256 | Modal.confirm({ |
| 257 | title: '是否确认删除所选数据项?', |
| 258 | icon: <ExclamationCircleOutlined />, |
| 259 | content: '请谨慎操作', |
| 260 | async onOk() { |
| 261 | const success = await handleRemove(selectedRows); |
| 262 | if (success) { |
| 263 | setSelectedRows([]); |
| 264 | actionRef.current?.reloadAndRest?.(); |
| 265 | } |
| 266 | }, |
| 267 | onCancel() {}, |
| 268 | }); |
| 269 | }} |
| 270 | > |
| 271 | <DeleteOutlined /> |
| 272 | <FormattedMessage id="pages.searchTable.delete" defaultMessage="删除" /> |
| 273 | </Button>, |
| 274 | <Button |
| 275 | type="primary" |
| 276 | key="export" |
| 277 | hidden={!access.hasPerms('system:post:export')} |
| 278 | onClick={async () => { |
| 279 | handleExport(); |
| 280 | }} |
| 281 | > |
| 282 | <PlusOutlined /> |
| 283 | <FormattedMessage id="pages.searchTable.export" defaultMessage="导出" /> |
| 284 | </Button>, |
| 285 | ]} |
| 286 | request={(params) => |
| 287 | getPostList({ ...params } as API.System.PostListParams).then((res) => { |
| 288 | const result = { |
| 289 | data: res.rows, |
| 290 | total: res.total, |
| 291 | success: true, |
| 292 | }; |
| 293 | return result; |
| 294 | }) |
| 295 | } |
| 296 | columns={columns} |
| 297 | rowSelection={{ |
| 298 | onChange: (_, selectedRows) => { |
| 299 | setSelectedRows(selectedRows); |
| 300 | }, |
| 301 | }} |
| 302 | /> |
| 303 | </div> |
| 304 | {selectedRows?.length > 0 && ( |
| 305 | <FooterToolbar |
| 306 | extra={ |
| 307 | <div> |
| 308 | <FormattedMessage id="pages.searchTable.chosen" defaultMessage="已选择" /> |
| 309 | <a style={{ fontWeight: 600 }}>{selectedRows.length}</a> |
| 310 | <FormattedMessage id="pages.searchTable.item" defaultMessage="项" /> |
| 311 | </div> |
| 312 | } |
| 313 | > |
| 314 | <Button |
| 315 | key="remove" |
| 316 | danger |
| 317 | hidden={!access.hasPerms('system:post:del')} |
| 318 | onClick={async () => { |
| 319 | Modal.confirm({ |
| 320 | title: '删除', |
| 321 | content: '确定删除该项吗?', |
| 322 | okText: '确认', |
| 323 | cancelText: '取消', |
| 324 | onOk: async () => { |
| 325 | const success = await handleRemove(selectedRows); |
| 326 | if (success) { |
| 327 | setSelectedRows([]); |
| 328 | actionRef.current?.reloadAndRest?.(); |
| 329 | } |
| 330 | }, |
| 331 | }); |
| 332 | }} |
| 333 | > |
| 334 | <FormattedMessage id="pages.searchTable.batchDeletion" defaultMessage="批量删除" /> |
| 335 | </Button> |
| 336 | </FooterToolbar> |
| 337 | )} |
| 338 | <UpdateForm |
| 339 | onSubmit={async (values) => { |
| 340 | let success = false; |
| 341 | if (values.postId) { |
| 342 | success = await handleUpdate({ ...values } as API.System.Post); |
| 343 | } else { |
| 344 | success = await handleAdd({ ...values } as API.System.Post); |
| 345 | } |
| 346 | if (success) { |
| 347 | setModalVisible(false); |
| 348 | setCurrentRow(undefined); |
| 349 | if (actionRef.current) { |
| 350 | actionRef.current.reload(); |
| 351 | } |
| 352 | } |
| 353 | }} |
| 354 | onCancel={() => { |
| 355 | setModalVisible(false); |
| 356 | setCurrentRow(undefined); |
| 357 | }} |
| 358 | open={modalVisible} |
| 359 | values={currentRow || {}} |
| 360 | statusOptions={statusOptions} |
| 361 | /> |
| 362 | </PageContainer> |
| 363 | ); |
| 364 | }; |
| 365 | |
| 366 | export default PostTableList; |