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, ReloadOutlined, DownloadOutlined } from '@ant-design/icons'; |
| 8 | import { getConfigList, removeConfig, addConfig, updateConfig, exportConfig, refreshConfigCache } from '@/services/system/config'; |
| 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.Config) => { |
| 19 | const hide = message.loading('正在添加'); |
| 20 | try { |
| 21 | const resp = await addConfig({ ...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.Config) => { |
| 42 | const hide = message.loading('正在更新'); |
| 43 | try { |
| 44 | const resp = await updateConfig(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.Config[]) => { |
| 65 | const hide = message.loading('正在删除'); |
| 66 | if (!selectedRows) return true; |
| 67 | try { |
| 68 | const resp = await removeConfig(selectedRows.map((row) => row.configId).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.Config) => { |
| 84 | const hide = message.loading('正在删除'); |
| 85 | if (!selectedRow) return true; |
| 86 | try { |
| 87 | const params = [selectedRow.configId]; |
| 88 | const resp = await removeConfig(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 exportConfig(); |
| 112 | hide(); |
| 113 | message.success('导出成功'); |
| 114 | return true; |
| 115 | } catch (error) { |
| 116 | hide(); |
| 117 | message.error('导出失败,请重试'); |
| 118 | return false; |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | const handleRefreshCache = async () => { |
| 123 | const hide = message.loading('正在刷新'); |
| 124 | try { |
| 125 | await refreshConfigCache(); |
| 126 | hide(); |
| 127 | message.success('刷新成功'); |
| 128 | return true; |
| 129 | } catch (error) { |
| 130 | hide(); |
| 131 | message.error('刷新失败,请重试'); |
| 132 | return false; |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | const ConfigTableList: React.FC = () => { |
| 137 | const formTableRef = useRef<FormInstance>(); |
| 138 | |
| 139 | const [modalVisible, setModalVisible] = useState<boolean>(false); |
| 140 | |
| 141 | const actionRef = useRef<ActionType>(); |
| 142 | const [currentRow, setCurrentRow] = useState<API.System.Config>(); |
| 143 | const [selectedRows, setSelectedRows] = useState<API.System.Config[]>([]); |
| 144 | |
| 145 | const [configTypeOptions, setConfigTypeOptions] = useState<any>([]); |
| 146 | |
| 147 | const access = useAccess(); |
| 148 | |
| 149 | /** 国际化配置 */ |
| 150 | const intl = useIntl(); |
| 151 | |
| 152 | useEffect(() => { |
| 153 | getDictValueEnum('sys_yes_no').then((data) => { |
| 154 | setConfigTypeOptions(data); |
| 155 | }); |
| 156 | }, []); |
| 157 | |
| 158 | const columns: ProColumns<API.System.Config>[] = [ |
| 159 | { |
| 160 | title: <FormattedMessage id="system.config.config_id" defaultMessage="参数主键" />, |
| 161 | dataIndex: 'configId', |
| 162 | valueType: 'text', |
| 163 | hideInSearch: true, |
| 164 | }, |
| 165 | { |
| 166 | title: <FormattedMessage id="system.config.config_name" defaultMessage="参数名称" />, |
| 167 | dataIndex: 'configName', |
| 168 | valueType: 'text', |
| 169 | }, |
| 170 | { |
| 171 | title: <FormattedMessage id="system.config.config_key" defaultMessage="参数键名" />, |
| 172 | dataIndex: 'configKey', |
| 173 | valueType: 'text', |
| 174 | }, |
| 175 | { |
| 176 | title: <FormattedMessage id="system.config.config_value" defaultMessage="参数键值" />, |
| 177 | dataIndex: 'configValue', |
| 178 | valueType: 'textarea', |
| 179 | }, |
| 180 | { |
| 181 | title: <FormattedMessage id="system.config.config_type" defaultMessage="系统内置" />, |
| 182 | dataIndex: 'configType', |
| 183 | valueType: 'select', |
| 184 | valueEnum: configTypeOptions, |
| 185 | render: (_, record) => { |
| 186 | return (<DictTag enums={configTypeOptions} value={record.configType} />); |
| 187 | }, |
| 188 | }, |
| 189 | { |
| 190 | title: <FormattedMessage id="system.config.remark" defaultMessage="备注" />, |
| 191 | dataIndex: 'remark', |
| 192 | valueType: 'textarea', |
| 193 | hideInSearch: true, |
| 194 | }, |
| 195 | { |
| 196 | title: <FormattedMessage id="pages.searchTable.titleOption" defaultMessage="操作" />, |
| 197 | dataIndex: 'option', |
| 198 | width: '120px', |
| 199 | valueType: 'option', |
| 200 | render: (_, record) => [ |
| 201 | <Button |
| 202 | type="link" |
| 203 | size="small" |
| 204 | key="edit" |
| 205 | hidden={!access.hasPerms('system:config:edit')} |
| 206 | onClick={() => { |
| 207 | setModalVisible(true); |
| 208 | setCurrentRow(record); |
| 209 | }} |
| 210 | > |
| 211 | 编辑 |
| 212 | </Button>, |
| 213 | <Button |
| 214 | type="link" |
| 215 | size="small" |
| 216 | danger |
| 217 | key="batchRemove" |
| 218 | hidden={!access.hasPerms('system:config:remove')} |
| 219 | onClick={async () => { |
| 220 | Modal.confirm({ |
| 221 | title: '删除', |
| 222 | content: '确定删除该项吗?', |
| 223 | okText: '确认', |
| 224 | cancelText: '取消', |
| 225 | onOk: async () => { |
| 226 | const success = await handleRemoveOne(record); |
| 227 | if (success) { |
| 228 | if (actionRef.current) { |
| 229 | actionRef.current.reload(); |
| 230 | } |
| 231 | } |
| 232 | }, |
| 233 | }); |
| 234 | }} |
| 235 | > |
| 236 | 删除 |
| 237 | </Button>, |
| 238 | ], |
| 239 | }, |
| 240 | ]; |
| 241 | |
| 242 | return ( |
| 243 | <PageContainer> |
| 244 | <div style={{ width: '100%', float: 'right' }}> |
| 245 | <ProTable<API.System.Config> |
| 246 | headerTitle={intl.formatMessage({ |
| 247 | id: 'pages.searchTable.title', |
| 248 | defaultMessage: '信息', |
| 249 | })} |
| 250 | actionRef={actionRef} |
| 251 | formRef={formTableRef} |
| 252 | rowKey="configId" |
| 253 | key="configList" |
| 254 | search={{ |
| 255 | labelWidth: 120, |
| 256 | }} |
| 257 | toolBarRender={() => [ |
| 258 | <Button |
| 259 | type="primary" |
| 260 | key="add" |
| 261 | hidden={!access.hasPerms('system:config:add')} |
| 262 | onClick={async () => { |
| 263 | setCurrentRow(undefined); |
| 264 | setModalVisible(true); |
| 265 | }} |
| 266 | > |
| 267 | <PlusOutlined /> <FormattedMessage id="pages.searchTable.new" defaultMessage="新建" /> |
| 268 | </Button>, |
| 269 | <Button |
| 270 | type="primary" |
| 271 | key="remove" |
| 272 | danger |
| 273 | hidden={selectedRows?.length === 0 || !access.hasPerms('system:config:remove')} |
| 274 | onClick={async () => { |
| 275 | Modal.confirm({ |
| 276 | title: '是否确认删除所选数据项?', |
| 277 | icon: <ExclamationCircleOutlined />, |
| 278 | content: '请谨慎操作', |
| 279 | async onOk() { |
| 280 | const success = await handleRemove(selectedRows); |
| 281 | if (success) { |
| 282 | setSelectedRows([]); |
| 283 | actionRef.current?.reloadAndRest?.(); |
| 284 | } |
| 285 | }, |
| 286 | onCancel() { }, |
| 287 | }); |
| 288 | }} |
| 289 | > |
| 290 | <DeleteOutlined /> |
| 291 | <FormattedMessage id="pages.searchTable.delete" defaultMessage="删除" /> |
| 292 | </Button>, |
| 293 | <Button |
| 294 | type="primary" |
| 295 | key="export" |
| 296 | hidden={!access.hasPerms('system:config:export')} |
| 297 | onClick={async () => { |
| 298 | handleExport(); |
| 299 | }} |
| 300 | > |
| 301 | <DownloadOutlined /> |
| 302 | <FormattedMessage id="pages.searchTable.export" defaultMessage="导出" /> |
| 303 | </Button>, |
| 304 | <Button |
| 305 | type="primary" |
| 306 | key="refresh" |
| 307 | danger |
| 308 | hidden={!access.hasPerms('system:config:remove')} |
| 309 | onClick={async () => { |
| 310 | handleRefreshCache(); |
| 311 | }} |
| 312 | > |
| 313 | <ReloadOutlined /> |
| 314 | <FormattedMessage id="system.config.refreshCache" defaultMessage="刷新缓存" /> |
| 315 | </Button>, |
| 316 | ]} |
| 317 | request={(params) => |
| 318 | getConfigList({ ...params } as API.System.ConfigListParams).then((res) => { |
| 319 | const result = { |
| 320 | data: res.rows, |
| 321 | total: res.total, |
| 322 | success: true, |
| 323 | }; |
| 324 | return result; |
| 325 | }) |
| 326 | } |
| 327 | columns={columns} |
| 328 | rowSelection={{ |
| 329 | onChange: (_, selectedRows) => { |
| 330 | setSelectedRows(selectedRows); |
| 331 | }, |
| 332 | }} |
| 333 | /> |
| 334 | </div> |
| 335 | {selectedRows?.length > 0 && ( |
| 336 | <FooterToolbar |
| 337 | extra={ |
| 338 | <div> |
| 339 | <FormattedMessage id="pages.searchTable.chosen" defaultMessage="已选择" /> |
| 340 | <a style={{ fontWeight: 600 }}>{selectedRows.length}</a> |
| 341 | <FormattedMessage id="pages.searchTable.item" defaultMessage="项" /> |
| 342 | </div> |
| 343 | } |
| 344 | > |
| 345 | <Button |
| 346 | key="remove" |
| 347 | danger |
| 348 | hidden={!access.hasPerms('system:config:del')} |
| 349 | onClick={async () => { |
| 350 | Modal.confirm({ |
| 351 | title: '删除', |
| 352 | content: '确定删除该项吗?', |
| 353 | okText: '确认', |
| 354 | cancelText: '取消', |
| 355 | onOk: async () => { |
| 356 | const success = await handleRemove(selectedRows); |
| 357 | if (success) { |
| 358 | setSelectedRows([]); |
| 359 | actionRef.current?.reloadAndRest?.(); |
| 360 | } |
| 361 | }, |
| 362 | }); |
| 363 | }} |
| 364 | > |
| 365 | <FormattedMessage id="pages.searchTable.batchDeletion" defaultMessage="批量删除" /> |
| 366 | </Button> |
| 367 | </FooterToolbar> |
| 368 | )} |
| 369 | <UpdateForm |
| 370 | onSubmit={async (values) => { |
| 371 | let success = false; |
| 372 | if (values.configId) { |
| 373 | success = await handleUpdate({ ...values } as API.System.Config); |
| 374 | } else { |
| 375 | success = await handleAdd({ ...values } as API.System.Config); |
| 376 | } |
| 377 | if (success) { |
| 378 | setModalVisible(false); |
| 379 | setCurrentRow(undefined); |
| 380 | if (actionRef.current) { |
| 381 | actionRef.current.reload(); |
| 382 | } |
| 383 | } |
| 384 | }} |
| 385 | onCancel={() => { |
| 386 | setModalVisible(false); |
| 387 | setCurrentRow(undefined); |
| 388 | }} |
| 389 | open={modalVisible} |
| 390 | values={currentRow || {}} |
| 391 | configTypeOptions={configTypeOptions} |
| 392 | /> |
| 393 | </PageContainer> |
| 394 | ); |
| 395 | }; |
| 396 | |
| 397 | export default ConfigTableList; |