index.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import Service from '@/pages/system/DataSource/service';
  2. import { PageContainer } from '@ant-design/pro-layout';
  3. import SearchComponent from '@/components/SearchComponent';
  4. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  5. import ProTable from '@jetlinks/pro-table';
  6. import { Badge, Popconfirm } from 'antd';
  7. import {
  8. DatabaseOutlined,
  9. DeleteOutlined,
  10. EditOutlined,
  11. PlayCircleOutlined,
  12. PlusOutlined,
  13. StopOutlined,
  14. } from '@ant-design/icons';
  15. import { useIntl } from '@@/plugin-locale/localeExports';
  16. import { useEffect, useRef, useState } from 'react';
  17. import { observer } from '@formily/react';
  18. import { PermissionButton } from '@/components';
  19. import usePermissions from '@/hooks/permission';
  20. import Save from './Save';
  21. import { Store } from 'jetlinks-store';
  22. import { getMenuPathByCode, MENUS_CODE } from '@/utils/menu';
  23. import { useHistory } from 'umi';
  24. import { useDomFullHeight } from '@/hooks';
  25. import { onlyMessage } from '@/utils/util';
  26. export const service = new Service('datasource/config');
  27. const DataSource = observer(() => {
  28. const intl = useIntl();
  29. const actionRef = useRef<ActionType>();
  30. const history = useHistory<Record<string, string>>();
  31. const { permission: userPermission } = usePermissions('system/DataSource');
  32. const [visible, setVisible] = useState<boolean>(false);
  33. const [current, setCurrent] = useState<Partial<DataSourceItem>>({});
  34. const { minHeight } = useDomFullHeight(`.datasource`, 24);
  35. useEffect(() => {
  36. service.getType().then((res) => {
  37. if (res.status === 200) {
  38. const list = res?.result.map((pItem: any) => ({ label: pItem.name, value: pItem.id }));
  39. Store.set('datasource-type', list);
  40. }
  41. });
  42. }, []);
  43. const columns: ProColumns<DataSourceItem>[] = [
  44. {
  45. title: '名称',
  46. dataIndex: 'name',
  47. ellipsis: true,
  48. fixed: 'left',
  49. width: 250,
  50. },
  51. {
  52. title: '类型',
  53. dataIndex: 'typeId',
  54. ellipsis: true,
  55. valueType: 'select',
  56. request: async () => {
  57. const res = await service.getType();
  58. if (res.status === 200) {
  59. const list = res.result.map((pItem: any) => ({ label: pItem.name, value: pItem.id }));
  60. return list;
  61. }
  62. return [];
  63. },
  64. render: (_, row) =>
  65. (Store.get('datasource-type') || []).find((item: any) => row.typeId === item.value)
  66. ?.label || row.typeId,
  67. filterMultiple: true,
  68. },
  69. {
  70. dataIndex: 'description',
  71. title: '说明',
  72. ellipsis: true,
  73. },
  74. {
  75. title: intl.formatMessage({
  76. id: 'pages.searchTable.titleStatus',
  77. defaultMessage: '状态',
  78. }),
  79. dataIndex: 'state',
  80. valueType: 'select',
  81. width: 120,
  82. valueEnum: {
  83. enabled: {
  84. text: intl.formatMessage({
  85. id: 'pages.searchTable.titleStatus.normal',
  86. defaultMessage: '正常',
  87. }),
  88. status: 'enabled',
  89. },
  90. disabled: {
  91. text: intl.formatMessage({
  92. id: 'pages.searchTable.titleStatus.disable',
  93. defaultMessage: '已禁用',
  94. }),
  95. status: 'disabled',
  96. },
  97. },
  98. render: (_, record) => (
  99. <Badge
  100. status={record.state?.value === 'enabled' ? 'success' : 'error'}
  101. text={record.state?.text}
  102. />
  103. ),
  104. },
  105. {
  106. title: intl.formatMessage({
  107. id: 'pages.data.option',
  108. defaultMessage: '操作',
  109. }),
  110. valueType: 'option',
  111. width: 200,
  112. fixed: 'right',
  113. render: (_, record) => [
  114. <PermissionButton
  115. style={{ padding: 0 }}
  116. type="link"
  117. isPermission={userPermission.update}
  118. key="editable"
  119. onClick={() => {
  120. setCurrent(record);
  121. setVisible(true);
  122. }}
  123. tooltip={{
  124. title: intl.formatMessage({
  125. id: 'pages.data.option.edit',
  126. defaultMessage: '编辑',
  127. }),
  128. }}
  129. >
  130. <EditOutlined />
  131. </PermissionButton>,
  132. <PermissionButton
  133. style={{ padding: 0 }}
  134. type="link"
  135. isPermission={userPermission.manage}
  136. key="manage"
  137. disabled={record.state?.value !== 'enabled' || record?.typeId === 'rabbitmq'}
  138. onClick={() => {
  139. const url = getMenuPathByCode(MENUS_CODE[`system/DataSource/Management`]);
  140. history.push(`${url}?id=${record.id}`);
  141. }}
  142. tooltip={{
  143. title:
  144. record?.typeId === 'rabbitmq'
  145. ? '暂不支持管理功能'
  146. : record.state?.value !== 'enabled'
  147. ? '请先启用数据源'
  148. : '管理',
  149. }}
  150. >
  151. <DatabaseOutlined />
  152. </PermissionButton>,
  153. <PermissionButton
  154. style={{ padding: 0 }}
  155. isPermission={userPermission.action}
  156. type="link"
  157. key="changeState"
  158. popConfirm={{
  159. title: intl.formatMessage({
  160. id: `pages.data.option.${
  161. record.state?.value === 'enabled' ? 'disabled' : 'enabled'
  162. }.tips`,
  163. defaultMessage: `确认${record.state?.value === 'enabled' ? '禁用' : '启用'}?`,
  164. }),
  165. onConfirm: async () => {
  166. const resp = await service.changeStatus(
  167. record.id,
  168. record.state?.value === 'enabled' ? 'disable' : 'enable',
  169. );
  170. if (resp.status === 200) {
  171. onlyMessage(
  172. intl.formatMessage({
  173. id: 'pages.data.option.success',
  174. defaultMessage: '操作成功!',
  175. }),
  176. );
  177. actionRef.current?.reload();
  178. }
  179. },
  180. }}
  181. tooltip={{
  182. title: intl.formatMessage({
  183. id: `pages.data.option.${record.state?.value === 'enabled' ? 'disabled' : 'enabled'}`,
  184. defaultMessage: record.state?.value === 'enabled' ? '禁用' : '启用',
  185. }),
  186. }}
  187. >
  188. {record.state?.value === 'enabled' ? <StopOutlined /> : <PlayCircleOutlined />}
  189. </PermissionButton>,
  190. <PermissionButton
  191. type="link"
  192. key="delete"
  193. style={{ padding: 0 }}
  194. isPermission={userPermission.delete}
  195. disabled={record.state?.value === 'enabled'}
  196. tooltip={{ title: record.state?.value === 'disabled' ? '删除' : '请先禁用,再删除。' }}
  197. >
  198. <Popconfirm
  199. onConfirm={async () => {
  200. const resp: any = await service.remove(record.id);
  201. if (resp.status === 200) {
  202. onlyMessage(
  203. intl.formatMessage({
  204. id: 'pages.data.option.success',
  205. defaultMessage: '操作成功!',
  206. }),
  207. );
  208. actionRef.current?.reload();
  209. }
  210. }}
  211. title="确认删除?"
  212. >
  213. <DeleteOutlined />
  214. </Popconfirm>
  215. </PermissionButton>,
  216. ],
  217. },
  218. ];
  219. const [param, setParam] = useState({});
  220. return (
  221. <PageContainer>
  222. <SearchComponent<DataSourceItem>
  223. field={columns}
  224. target="datasource"
  225. onSearch={(data) => {
  226. // 重置分页数据
  227. actionRef.current?.reset?.();
  228. setParam(data);
  229. }}
  230. />
  231. <ProTable<DataSourceItem>
  232. actionRef={actionRef}
  233. params={param}
  234. columns={columns}
  235. search={false}
  236. rowKey="id"
  237. columnEmptyText={''}
  238. scroll={{ x: 1366 }}
  239. tableClassName={'datasource'}
  240. tableStyle={{ minHeight }}
  241. headerTitle={
  242. <PermissionButton
  243. onClick={() => {
  244. setCurrent({});
  245. setVisible(true);
  246. }}
  247. isPermission={userPermission.add}
  248. key="add"
  249. icon={<PlusOutlined />}
  250. type="primary"
  251. >
  252. {intl.formatMessage({
  253. id: 'pages.data.option.add',
  254. defaultMessage: '新增',
  255. })}
  256. </PermissionButton>
  257. }
  258. request={async (params) =>
  259. service.query({ ...params, sorts: [{ name: 'createTime', order: 'desc' }] })
  260. }
  261. />
  262. {visible && (
  263. <Save
  264. close={() => {
  265. setVisible(false);
  266. }}
  267. data={current}
  268. reload={() => {
  269. setVisible(false);
  270. actionRef.current?.reload();
  271. }}
  272. />
  273. )}
  274. </PageContainer>
  275. );
  276. });
  277. export default DataSource;