hooks.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import {deleteUser, resetUserPassword} from '@apis';
  2. import {LDColumnsType, LDTableButton, modifyDataColumns} from '@components';
  3. import {useTableDeleteEvent, useTableModalEvent} from '@hooks';
  4. import {UserListData} from '@models';
  5. import {useMutation} from '@tanstack/react-query';
  6. import {TABLE_CELL_WIDTH} from '@utils';
  7. import {App} from 'antd';
  8. const tableColumns: LDColumnsType<UserListData>[] = [
  9. {title: '用户名称', dataIndex: 'userName', width: TABLE_CELL_WIDTH.normal},
  10. {title: '真实姓名', dataIndex: 'realName', width: TABLE_CELL_WIDTH.normal},
  11. {title: '角色名称', dataIndex: 'role', width: TABLE_CELL_WIDTH.normal},
  12. {title: '邮箱', dataIndex: 'email', width: TABLE_CELL_WIDTH.middle},
  13. {title: '手机号', dataIndex: 'phone', width: TABLE_CELL_WIDTH.normal},
  14. ...modifyDataColumns,
  15. ];
  16. export function useResetPassword() {
  17. const {message, modal} = App.useApp();
  18. const {isLoading, mutate} = useMutation({
  19. mutationFn: resetUserPassword,
  20. onSuccess({msg}) {
  21. msg === '200' && message.success('密码重置成功');
  22. },
  23. });
  24. function onClick(name: string, id: string) {
  25. return function() {
  26. modal.confirm({
  27. title: '重置密码',
  28. content: `你确定要将${name}的密码重置为123456吗?`,
  29. onOk() {
  30. mutate(id);
  31. },
  32. });
  33. };
  34. }
  35. return [isLoading, onClick] as const;
  36. }
  37. export function useColumns(refetch: () => void) {
  38. const [deleteId, onDelete] = useTableDeleteEvent(
  39. deleteUser,
  40. refetch,
  41. {label: '用户信息'},
  42. );
  43. const [{editId, visible}, {onAdd, onEdit, onClose}] = useTableModalEvent();
  44. const [isResting, onResetPassword] = useResetPassword();
  45. const [
  46. {editId: authId, visible: authVisible},
  47. {onEdit: onAuthEdit, onClose: onAuthColose},
  48. ] = useTableModalEvent();
  49. const columns: LDColumnsType<UserListData>[] = [
  50. ...tableColumns,
  51. {
  52. title: '操作',
  53. dataIndex: 'id',
  54. fixed: true,
  55. width: TABLE_CELL_WIDTH.great,
  56. render({id, realName}) {
  57. const loading = String(id) === deleteId;
  58. return (
  59. <>
  60. <LDTableButton
  61. disabled={loading}
  62. onClick={onEdit(String(id))}
  63. >
  64. 修改
  65. </LDTableButton>
  66. <LDTableButton
  67. disabled={loading}
  68. loading={isResting}
  69. onClick={onResetPassword(realName, String(id))}
  70. >
  71. 重置密码
  72. </LDTableButton>
  73. <LDTableButton
  74. disabled={loading}
  75. onClick={onAuthEdit(String(id))}
  76. >
  77. 菜单权限
  78. </LDTableButton>
  79. <LDTableButton
  80. loading={loading}
  81. danger
  82. onClick={onDelete(String(id), realName)}
  83. >
  84. 删除
  85. </LDTableButton>
  86. </>
  87. );
  88. },
  89. },
  90. ];
  91. return [
  92. {editId, visible, columns, authId, authVisible},
  93. {onAdd, onClose, onAuthColose},
  94. ] as const;
  95. }