index.tsx 8.5 KB

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