Unbound.tsx 4.4 KB

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