Bound.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { Button, Card, message, Space } from 'antd';
  2. import type { ActionType } from '@jetlinks/pro-table';
  3. import ProTable from '@jetlinks/pro-table';
  4. import { PlusOutlined } from '@ant-design/icons';
  5. import { useEffect, useRef } from 'react';
  6. import { Store } from 'jetlinks-store';
  7. import SystemConst from '@/utils/const';
  8. import { observer } from '@formily/react';
  9. import { BindModel } from '@/components/BindUser/model';
  10. import { columns, service } from '@/components/BindUser/index';
  11. import { useIntl } from '@@/plugin-locale/localeExports';
  12. import { onlyMessage } from '@/utils/util';
  13. const Bound = observer(() => {
  14. const intl = useIntl();
  15. const actionRef = useRef<ActionType>();
  16. useEffect(() => {
  17. const listener = Store.subscribe(SystemConst.BIND_USER_STATE, () =>
  18. actionRef.current?.reload(),
  19. );
  20. return () => listener.unsubscribe();
  21. });
  22. const handleUnBindResult = {
  23. next: async () => {
  24. onlyMessage(
  25. intl.formatMessage({
  26. id: 'pages.bindUser.theBoundUser.success',
  27. defaultMessage: '解绑成功',
  28. }),
  29. );
  30. },
  31. error: async () => {
  32. message.error(
  33. intl.formatMessage({
  34. id: 'pages.bindUser.theBoundUser.fail',
  35. defaultMessage: '操作失败',
  36. }),
  37. );
  38. },
  39. complete: () => {
  40. // 通知右侧组建刷新
  41. Store.set(SystemConst.BIND_USER_STATE, 'true');
  42. actionRef.current?.reload();
  43. BindModel.unBindUsers = [];
  44. },
  45. };
  46. const {
  47. dimension: { id, type },
  48. } = BindModel;
  49. const handleRoleUnBind = () => {
  50. service.unBindRole(BindModel.unBindUsers, type!, id!).subscribe(handleUnBindResult);
  51. };
  52. const handleOrgUnBind = () => {
  53. service.unBindOrg(BindModel.unBindUsers, id!).subscribe(handleUnBindResult);
  54. };
  55. const handleUnbind = async () => {
  56. const bindType = BindModel.dimension.type;
  57. switch (bindType) {
  58. case 'role':
  59. handleRoleUnBind();
  60. break;
  61. case 'org':
  62. handleOrgUnBind();
  63. break;
  64. default:
  65. message.error(
  66. intl.formatMessage({
  67. id: 'pages.bindUser.theBoundUser.typeError',
  68. defaultMessage: '解绑类型数据错误',
  69. }),
  70. );
  71. }
  72. };
  73. return (
  74. <Card
  75. title={intl.formatMessage({
  76. id: 'pages.bindUser.theBoundUser',
  77. defaultMessage: '已绑定用户',
  78. })}
  79. >
  80. <ProTable
  81. size="small"
  82. rowKey="id"
  83. rowSelection={{
  84. selectedRowKeys: BindModel.unBindUsers,
  85. onChange: (selectedRowKeys) => {
  86. BindModel.unBindUsers = selectedRowKeys as string[];
  87. },
  88. }}
  89. tableAlertRender={({ selectedRowKeys, onCleanSelected }) => (
  90. <Space size={24}>
  91. <span>
  92. {intl.formatMessage({
  93. id: 'pages.bindUser.bindTheNewUser.selected',
  94. defaultMessage: '已选',
  95. })}{' '}
  96. {selectedRowKeys.length}{' '}
  97. {intl.formatMessage({
  98. id: 'pages.bindUser.bindTheNewUser.item',
  99. defaultMessage: '项',
  100. })}
  101. <a style={{ marginLeft: 8 }} onClick={onCleanSelected}>
  102. {intl.formatMessage({
  103. id: 'pages.bindUser.bindTheNewUser.deselect',
  104. defaultMessage: '取消选择',
  105. })}
  106. </a>
  107. </span>
  108. </Space>
  109. )}
  110. tableAlertOptionRender={() => (
  111. <Space size={16}>
  112. <a onClick={handleUnbind}>
  113. {intl.formatMessage({
  114. id: 'pages.bindUser.bindTheNewUser.untieInBulk',
  115. defaultMessage: '批量解绑',
  116. })}
  117. </a>
  118. </Space>
  119. )}
  120. actionRef={actionRef}
  121. columns={columns}
  122. pagination={{
  123. pageSize: 10,
  124. }}
  125. columnEmptyText={''}
  126. request={async (params) => service.query(params)}
  127. defaultParams={{
  128. [`id$in-dimension$${BindModel.dimension.type}`]: BindModel.dimension.id,
  129. }}
  130. toolBarRender={() => [
  131. <Button
  132. onClick={() => {
  133. BindModel.bind = true;
  134. }}
  135. key="button"
  136. icon={<PlusOutlined />}
  137. type="primary"
  138. >
  139. {intl.formatMessage({
  140. id: 'pages.system.role.option.bindUser',
  141. defaultMessage: '绑定用户',
  142. })}
  143. </Button>,
  144. ]}
  145. />
  146. </Card>
  147. );
  148. });
  149. export default Bound;