index.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import { Badge, Button, Col, Input, message, Modal, Popconfirm, Row, Tooltip, Tree } from 'antd';
  2. import { observer } from '@formily/react';
  3. import { service, state } from '..';
  4. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  5. import ProTable from '@jetlinks/pro-table';
  6. import { useEffect, useRef, useState } from 'react';
  7. import { history, useLocation } from 'umi';
  8. import { DisconnectOutlined, EditOutlined } from '@ant-design/icons';
  9. import BindUser from '../BindUser';
  10. const SyncUser = observer(() => {
  11. const [dept, setDept] = useState<string>();
  12. const location = useLocation<{ id: string }>();
  13. const id = (location as any).query?.id;
  14. const [visible, setVisible] = useState<boolean>(false);
  15. const [current, setCurrent] = useState<any>({});
  16. const [list, setList] = useState<any[]>([]);
  17. const idMap = {
  18. dingTalk: '钉钉',
  19. weixin: '微信',
  20. };
  21. const actionRef = useRef<ActionType>();
  22. const columns: ProColumns<any>[] = [
  23. {
  24. dataIndex: 'thirdPartyUserName',
  25. title: `${idMap[id]}用户名`,
  26. },
  27. {
  28. dataIndex: 'userId',
  29. title: `用户`,
  30. render: (text: any, record: any) => (
  31. <span>{record?.userId ? `${record?.userName}(${record?.username})` : '--'}</span>
  32. ),
  33. },
  34. {
  35. dataIndex: 'status',
  36. title: '绑定状态',
  37. render: (text: any, record: any) => (
  38. <Badge
  39. status={record?.status === 1 ? 'success' : 'error'}
  40. text={record?.status === 1 ? '已绑定' : '未绑定'}
  41. />
  42. ),
  43. },
  44. {
  45. dataIndex: 'action',
  46. title: '操作',
  47. render: (text: any, record: any) => [
  48. <Tooltip title={'绑定用户'} key="bind">
  49. <Button
  50. type="link"
  51. onClick={() => {
  52. setCurrent(record);
  53. setVisible(true);
  54. }}
  55. >
  56. <EditOutlined />
  57. </Button>
  58. </Tooltip>,
  59. <Tooltip title={'解绑用户'} key="unbind">
  60. {record?.status === 1 && (
  61. <Button type="link">
  62. <Popconfirm
  63. title={'确认解绑'}
  64. onConfirm={async () => {
  65. if (record?.bindingId) {
  66. const resp = await service.syncUser.unBindUser(record.bindingId, {
  67. bindingId: record.bindingId,
  68. });
  69. if (resp.status === 200) {
  70. message.success('操作成功!');
  71. actionRef.current?.reload();
  72. }
  73. }
  74. }}
  75. >
  76. <DisconnectOutlined />
  77. </Popconfirm>
  78. </Button>
  79. )}
  80. </Tooltip>,
  81. ],
  82. },
  83. ];
  84. const [treeData, setTreeData] = useState([]);
  85. /**
  86. * 获取部门列表
  87. */
  88. const getDepartment = async () => {
  89. if (state.current?.id) {
  90. if (id === 'dingTalk') {
  91. service.syncUser.dingTalkDept(state.current?.id).then((resp) => {
  92. if (resp.status === 200) {
  93. setTreeData(resp.result);
  94. setDept(resp.result[0].id);
  95. // console.log(resp.result[0].id, 'id');
  96. }
  97. });
  98. } else if (id === 'weixin') {
  99. service.syncUser.wechatDept(state.current?.id).then((resp) => {
  100. if (resp.status === 200) {
  101. setTreeData(resp.result);
  102. setDept(resp.result[0].id);
  103. // console.log(resp.result[0].id, 'id~~');
  104. }
  105. });
  106. }
  107. }
  108. };
  109. useEffect(() => {
  110. if (!state.current?.id) {
  111. history.goBack();
  112. }
  113. getDepartment();
  114. }, [id]);
  115. return (
  116. <Modal
  117. title="同步用户"
  118. bodyStyle={{ height: '600px', overflowY: 'auto' }}
  119. visible={true}
  120. onCancel={() => (state.syncUser = false)}
  121. width="80vw"
  122. >
  123. <Row>
  124. <Col span={4}>
  125. <div style={{ borderRight: 'lightgray 1px solid', padding: '2px', height: '600px' }}>
  126. <Input.Search style={{ marginBottom: 8 }} placeholder="请输入部门名称" />
  127. <Tree
  128. fieldNames={{
  129. title: 'name',
  130. key: 'id',
  131. }}
  132. selectedKeys={[dept || '']}
  133. onSelect={(key) => {
  134. setDept(key[0] as string);
  135. }}
  136. treeData={treeData}
  137. />
  138. </div>
  139. </Col>
  140. <Col span={20}>
  141. {dept && (
  142. <ProTable
  143. rowKey="thirdPartyUserId"
  144. actionRef={actionRef}
  145. search={false}
  146. columns={columns}
  147. params={{ dept: dept }}
  148. request={(params) =>
  149. service
  150. .queryZipSyncUser(
  151. {
  152. dingTalk: 'dingtalk',
  153. weixin: 'wechat',
  154. }[id],
  155. id,
  156. state.current?.provider || '',
  157. state.current?.id || '',
  158. params.dept || '',
  159. )
  160. .then((resp: any) => {
  161. setList(resp);
  162. return {
  163. code: '',
  164. result: {
  165. data: resp || [],
  166. pageIndex: 0,
  167. pageSize: 0,
  168. total: 0,
  169. },
  170. status: 200,
  171. };
  172. })
  173. }
  174. headerTitle={
  175. <Popconfirm
  176. title="确认保存"
  177. onConfirm={async () => {
  178. const arr = list
  179. .filter((item) => item.status === 0)
  180. .map((i) => {
  181. return {
  182. userId: i.userId,
  183. providerName: i.userName,
  184. thirdPartyUserId: i.thirdPartyUserId,
  185. };
  186. });
  187. const resp = await service.syncUser.bindUser(
  188. id,
  189. state.current?.provider || '',
  190. state.current?.id || '',
  191. [...arr],
  192. );
  193. if (resp.status === 200) {
  194. message.success('操作成功!');
  195. actionRef.current?.reload();
  196. }
  197. }}
  198. >
  199. <Button type="primary">保存</Button>
  200. </Popconfirm>
  201. }
  202. />
  203. )}
  204. </Col>
  205. </Row>
  206. {visible && (
  207. <BindUser
  208. id={id}
  209. close={() => {
  210. setCurrent({});
  211. setVisible(false);
  212. }}
  213. data={current}
  214. reload={() => {
  215. setCurrent({});
  216. setVisible(false);
  217. actionRef.current?.reload();
  218. }}
  219. />
  220. )}
  221. </Modal>
  222. );
  223. });
  224. export default SyncUser;