index.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { Button, Col, Input, Modal, Row, Tree } from 'antd';
  2. import { observer } from '@formily/react';
  3. import { service, state } from '..';
  4. import ProTable, { ActionType, ProColumns } from '@jetlinks/pro-table';
  5. import { useEffect, useRef, useState } from 'react';
  6. import { history, useLocation } from 'umi';
  7. import { PermissionButton } from '@/components';
  8. import { DisconnectOutlined, EditOutlined } from '@ant-design/icons';
  9. const SyncUser = observer(() => {
  10. const [dept, setDept] = useState<string>();
  11. const location = useLocation<{ id: string }>();
  12. const id = (location as any).query?.id;
  13. const idMap = {
  14. dingTalk: '钉钉',
  15. weixin: '微信',
  16. };
  17. const columns: ProColumns<any>[] = [
  18. {
  19. dataIndex: 'id',
  20. title: `${idMap[id]}ID`,
  21. },
  22. {
  23. dataIndex: 'name',
  24. title: `${idMap[id]}用户名`,
  25. },
  26. {
  27. dataIndex: 'action',
  28. title: '绑定状态',
  29. render: () => [
  30. <PermissionButton
  31. tooltip={{
  32. title: '绑定用户',
  33. }}
  34. >
  35. <EditOutlined />
  36. </PermissionButton>,
  37. <PermissionButton
  38. tooltip={{
  39. title: '解绑用户',
  40. }}
  41. >
  42. <DisconnectOutlined />
  43. </PermissionButton>,
  44. ],
  45. },
  46. ];
  47. const actionRef = useRef<ActionType>();
  48. const [treeData, setTreeData] = useState([]);
  49. /**
  50. * 获取部门列表
  51. */
  52. const getDepartment = async () => {
  53. if (state.current?.id) {
  54. if (id === 'dingTalk') {
  55. service.syncUser.dingTalkDept(state.current?.id).then((resp) => {
  56. if (resp.status === 200) {
  57. setTreeData(resp.result);
  58. setDept(resp.result[0].id);
  59. console.log(resp.result[0].id, 'id');
  60. }
  61. });
  62. } else if (id === 'weixin') {
  63. service.syncUser.wechatDept(state.current?.id).then((resp) => {
  64. if (resp.status === 200) {
  65. setTreeData(resp.result);
  66. setDept(resp.result[0].id);
  67. console.log(resp.result[0].id, 'id~~');
  68. }
  69. });
  70. }
  71. }
  72. };
  73. useEffect(() => {
  74. if (!state.current?.id) {
  75. history.goBack();
  76. }
  77. getDepartment();
  78. }, [id]);
  79. // const updateTreeData = (list: any[], key: React.Key, children: any[]): any[] => {
  80. // return list.map((node) => {
  81. // if (node.id === key) {
  82. // return {
  83. // ...node,
  84. // children: node.children ? [...node.children, ...children] : children,
  85. // };
  86. // }
  87. //
  88. // if (node.children) {
  89. // return {
  90. // ...node,
  91. // children: updateTreeData(node.children, key, children),
  92. // };
  93. // }
  94. // return node;
  95. // });
  96. // };
  97. // const getParentKey = (key: any, tree: string | any[]): any => {
  98. // let parentKey;
  99. // for (let i = 0; i < tree.length; i++) {
  100. // const node = tree[i];
  101. // if (node.children) {
  102. // if (node.children.some((item: { key: any; }) => item.key === key)) {
  103. // parentKey = node.key;
  104. // } else if (getParentKey(key, node.children)) {
  105. // parentKey = getParentKey(key, node.children);
  106. // }
  107. // }
  108. // }
  109. // return parentKey;
  110. // };
  111. return (
  112. <Modal
  113. title="同步用户"
  114. bodyStyle={{ height: '600px', overflowY: 'auto' }}
  115. visible={true}
  116. onCancel={() => (state.syncUser = false)}
  117. width="80vw"
  118. >
  119. <Row>
  120. <Col span={4}>
  121. <div style={{ borderRight: 'lightgray 1px solid', padding: '2px', height: '600px' }}>
  122. <Input.Search style={{ marginBottom: 8 }} placeholder="请输入部门名称" />
  123. <Tree
  124. fieldNames={{
  125. title: 'name',
  126. key: 'id',
  127. }}
  128. onSelect={(key) => {
  129. setDept(key[0] as string);
  130. }}
  131. treeData={treeData}
  132. // loadData={onLoadData}
  133. />
  134. </div>
  135. </Col>
  136. <Col span={20}>
  137. {dept && (
  138. <ProTable
  139. rowKey="id"
  140. actionRef={actionRef}
  141. search={false}
  142. columns={columns}
  143. params={{ dept: dept }}
  144. request={async (params) =>
  145. service.syncUser
  146. .getDeptUser(
  147. {
  148. dingTalk: 'dingtalk',
  149. weixin: 'wechat',
  150. }[id],
  151. state.current?.id || '',
  152. params.dept || '',
  153. )
  154. .then((resp) => {
  155. return {
  156. code: resp.message,
  157. result: {
  158. data: resp.result || [],
  159. pageIndex: 0,
  160. pageSize: 0,
  161. total: 0,
  162. },
  163. status: resp.status,
  164. };
  165. })
  166. }
  167. headerTitle={<Button>保存</Button>}
  168. />
  169. )}
  170. </Col>
  171. </Row>
  172. </Modal>
  173. );
  174. });
  175. export default SyncUser;