index.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import {Button, Divider, Input, message, Modal, Radio} from 'antd';
  2. import React, {useRef, useState} from 'react';
  3. import {PageHeaderWrapper} from '@ant-design/pro-layout';
  4. import ProTable, {ActionType, ProColumns} from '@ant-design/pro-table';
  5. import {SorterResult} from 'antd/es/table/interface';
  6. import {ExclamationCircleOutlined, PlusOutlined} from "@ant-design/icons/lib";
  7. import {TableListItem} from './data.d';
  8. import service from './service';
  9. /**
  10. * 删除节点
  11. * @param fields
  12. */
  13. const handleRemove = async (fields: TableListItem) => {
  14. const hide = message.loading('正在删除');
  15. try {
  16. const res = await service.delete(fields);
  17. if (res.success) {
  18. message.success('删除成功,即将刷新');
  19. } else {
  20. message.error('删除失败,请重试');
  21. }
  22. hide();
  23. return res.success;
  24. } catch (error) {
  25. hide();
  26. message.error('删除失败,请重试');
  27. return false;
  28. }
  29. };
  30. /**
  31. * 添加节点
  32. * @param fields
  33. */
  34. const handleAdd = async (fields: TableListItem) => {
  35. const hide = message.loading('正在添加');
  36. try {
  37. const res = await service.register({...fields});
  38. hide();
  39. if (res.success) {
  40. message.success('添加成功');
  41. return true;
  42. }
  43. message.error('添加失败请重试!');
  44. return false;
  45. } catch (error) {
  46. hide();
  47. message.error('添加失败请重试!');
  48. return false;
  49. }
  50. };
  51. /**
  52. * 更新节点
  53. * @param fields
  54. */
  55. const handleUpdate = async (fields: TableListItem) => {
  56. const hide = message.loading('正在配置');
  57. try {
  58. const res = await service.update(fields);
  59. hide();
  60. if (res.success) {
  61. message.success('配置成功');
  62. return true;
  63. }
  64. message.error('配置失败请重试!');
  65. return false;
  66. } catch (error) {
  67. hide();
  68. message.error('配置失败请重试!');
  69. return false;
  70. }
  71. };
  72. const TableList: React.FC<{}> = () => {
  73. const [sorter, setSorter] = useState<string>('');
  74. const [modelVisible, setModalVisible] = useState<boolean>(false);
  75. const [formValues, setFormValues] = useState({});
  76. const actionRef = useRef<ActionType>();
  77. // 重置表单
  78. const [resetVisible, setResetVisible] = useState<boolean>(false);
  79. const [resetLoading, setResetLoading] = useState<boolean>(false);
  80. const [newPassword, setNewPassword] = useState<string>('');
  81. const {confirm} = Modal;
  82. const deleteMemory = async (record: any) => {
  83. await handleRemove(record);
  84. if (actionRef.current) {
  85. actionRef.current.reload();
  86. }
  87. };
  88. const columns: ProColumns<TableListItem>[] = [
  89. {
  90. title: '用户名',
  91. dataIndex: 'userName',
  92. width: 100,
  93. hideInSearch: false,
  94. },
  95. {
  96. title: '性别',
  97. dataIndex: 'gender',
  98. width: 50,
  99. hideInSearch: true,
  100. renderFormItem: () => {
  101. return (
  102. <Radio.Group>
  103. <Radio value="0">女</Radio>
  104. <Radio value='1'>男</Radio>
  105. </Radio.Group>
  106. );
  107. },
  108. valueEnum: {
  109. '0': {text: '女'},
  110. '1': {text: '男'}
  111. },
  112. },
  113. {
  114. title: '年龄',
  115. dataIndex: 'age',
  116. width: 50,
  117. hideInSearch: true,
  118. },
  119. {
  120. title: '手机号',
  121. dataIndex: 'phone',
  122. width: 80,
  123. hideInSearch: true,
  124. },
  125. {
  126. title: '状态',
  127. dataIndex: 'status',
  128. width: 80,
  129. },
  130. {
  131. title: '角色',
  132. dataIndex: 'roleId',
  133. width: 80,
  134. renderFormItem: () => {
  135. return (
  136. <Radio.Group>
  137. <Radio value={1}>管理员</Radio>
  138. <Radio value={2}>普通用户</Radio>
  139. </Radio.Group>
  140. );
  141. },
  142. valueEnum: {
  143. 1: {text: '管理员'},
  144. 2: {text: '普通用户'}
  145. },
  146. },
  147. {
  148. title: '操作',
  149. dataIndex: 'option',
  150. width: 150,
  151. valueType: 'option',
  152. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  153. render: (_, record, index, action) => (
  154. <>
  155. <a
  156. onClick={() => {
  157. setFormValues(record);
  158. setModalVisible(true);
  159. }}
  160. >
  161. 修改
  162. </a>
  163. <Divider type="vertical"/>
  164. <a
  165. onClick={async () => {
  166. confirm({
  167. title: '确定删除吗?',
  168. icon: <ExclamationCircleOutlined/>,
  169. content: '删除后无法恢复',
  170. okText: '确定',
  171. okType: 'danger',
  172. cancelText: '取消',
  173. onOk() {
  174. deleteMemory(record);
  175. },
  176. onCancel() {
  177. console.log('Cancel');
  178. },
  179. });
  180. }}
  181. >
  182. 删除
  183. </a>
  184. <Divider type="vertical"/>
  185. <a
  186. onClick={() => {
  187. setFormValues(record);
  188. setResetVisible(true);
  189. }}
  190. >
  191. 重置密码
  192. </a>
  193. </>
  194. ),
  195. },
  196. ];
  197. return (
  198. <PageHeaderWrapper title={false}>
  199. <ProTable<TableListItem>
  200. // search={false}
  201. headerTitle="查询表格"
  202. actionRef={actionRef}
  203. rowKey="id"
  204. onChange={(_, _filter, _sorter) => {
  205. const sorterResult = _sorter as SorterResult<TableListItem>;
  206. if (sorterResult.field) {
  207. setSorter(`${sorterResult.field}_${sorterResult.order}`);
  208. }
  209. }}
  210. pagination={{
  211. defaultPageSize: 10,
  212. }}
  213. params={{
  214. sorter,
  215. }}
  216. request={async (params = {}) => {
  217. const res = await service.queryList(params);
  218. return {
  219. data: res.data.list,
  220. page: params.current,
  221. success: true,
  222. total: res.data.total,
  223. };
  224. }}
  225. columns={columns}
  226. rowSelection={{}}
  227. />
  228. <Modal
  229. title="重置密码"
  230. visible={resetVisible}
  231. onOk={ async ()=>{
  232. setResetLoading(true);
  233. let res = await service.forceUpdatePassword({user_Id: formValues['id'], newPassword: newPassword});
  234. if (res.success) {
  235. message.success("修改成功");
  236. setResetVisible(false);
  237. } else {
  238. message.error("修改失败");
  239. }
  240. setResetLoading(false);
  241. }}
  242. confirmLoading={resetLoading}
  243. onCancel={()=>{setResetVisible(false)}}
  244. >
  245. <Input placeholder="新密码" value={newPassword} onChange={(e)=>setNewPassword(e.target.value)} />
  246. </Modal>
  247. <Modal
  248. destroyOnClose
  249. title={'id' in formValues ? '编辑' : '新建'}
  250. visible={modelVisible}
  251. onCancel={() => setModalVisible(false)}
  252. footer={null}
  253. >
  254. <ProTable<TableListItem, TableListItem>
  255. onSubmit={async (value) => {
  256. let success = false;
  257. if ('id' in formValues) {
  258. value['id'] = formValues['id']
  259. success = await handleUpdate(value);
  260. } else {
  261. success = await handleAdd(value);
  262. }
  263. if (success) {
  264. setModalVisible(false);
  265. if (actionRef.current) {
  266. actionRef.current.reload();
  267. }
  268. }
  269. }}
  270. rowKey="id"
  271. type="form"
  272. columns={columns}
  273. rowSelection={{}}
  274. form={{initialValues: formValues}}
  275. />
  276. </Modal>
  277. </PageHeaderWrapper>
  278. );
  279. };
  280. export default TableList;