index.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.action}
  136. key="manage"
  137. disabled={record.state?.value !== 'enabled'}
  138. onClick={() => {
  139. const url = getMenuPathByCode(MENUS_CODE[`system/DataSource/Management`]);
  140. history.push(`${url}?id=${record.id}`);
  141. }}
  142. tooltip={{
  143. title: record.state?.value !== 'enabled' ? '请先启用数据源' : '管理',
  144. }}
  145. >
  146. <DatabaseOutlined />
  147. </PermissionButton>,
  148. <PermissionButton
  149. style={{ padding: 0 }}
  150. isPermission={userPermission.action}
  151. type="link"
  152. key="changeState"
  153. popConfirm={{
  154. title: intl.formatMessage({
  155. id: `pages.data.option.${
  156. record.state?.value === 'enabled' ? 'disabled' : 'enabled'
  157. }.tips`,
  158. defaultMessage: `确认${record.state?.value === 'enabled' ? '禁用' : '启用'}?`,
  159. }),
  160. onConfirm: async () => {
  161. const resp = await service.changeStatus(
  162. record.id,
  163. record.state?.value === 'enabled' ? 'disable' : 'enable',
  164. );
  165. if (resp.status === 200) {
  166. onlyMessage(
  167. intl.formatMessage({
  168. id: 'pages.data.option.success',
  169. defaultMessage: '操作成功!',
  170. }),
  171. );
  172. actionRef.current?.reload();
  173. }
  174. },
  175. }}
  176. tooltip={{
  177. title: intl.formatMessage({
  178. id: `pages.data.option.${record.state?.value === 'enabled' ? 'disabled' : 'enabled'}`,
  179. defaultMessage: record.state?.value === 'enabled' ? '禁用' : '启用',
  180. }),
  181. }}
  182. >
  183. {record.state?.value === 'enabled' ? <StopOutlined /> : <PlayCircleOutlined />}
  184. </PermissionButton>,
  185. <PermissionButton
  186. type="link"
  187. key="delete"
  188. style={{ padding: 0 }}
  189. isPermission={userPermission.delete}
  190. disabled={record.state?.value === 'enabled'}
  191. tooltip={{ title: record.state?.value === 'disabled' ? '删除' : '请先禁用,再删除。' }}
  192. >
  193. <Popconfirm
  194. onConfirm={async () => {
  195. const resp: any = await service.remove(record.id);
  196. if (resp.status === 200) {
  197. onlyMessage(
  198. intl.formatMessage({
  199. id: 'pages.data.option.success',
  200. defaultMessage: '操作成功!',
  201. }),
  202. );
  203. actionRef.current?.reload();
  204. }
  205. }}
  206. title="确认删除?"
  207. >
  208. <DeleteOutlined />
  209. </Popconfirm>
  210. </PermissionButton>,
  211. ],
  212. },
  213. ];
  214. const [param, setParam] = useState({});
  215. return (
  216. <PageContainer>
  217. <SearchComponent<DataSourceItem>
  218. field={columns}
  219. target="datasource"
  220. onSearch={(data) => {
  221. // 重置分页数据
  222. actionRef.current?.reset?.();
  223. setParam(data);
  224. }}
  225. />
  226. <ProTable<DataSourceItem>
  227. actionRef={actionRef}
  228. params={param}
  229. columns={columns}
  230. search={false}
  231. rowKey="id"
  232. columnEmptyText={''}
  233. scroll={{ x: 1366 }}
  234. tableClassName={'datasource'}
  235. tableStyle={{ minHeight }}
  236. headerTitle={
  237. <PermissionButton
  238. onClick={() => {
  239. setCurrent({});
  240. setVisible(true);
  241. }}
  242. isPermission={userPermission.add}
  243. key="add"
  244. icon={<PlusOutlined />}
  245. type="primary"
  246. >
  247. {intl.formatMessage({
  248. id: 'pages.data.option.add',
  249. defaultMessage: '新增',
  250. })}
  251. </PermissionButton>
  252. }
  253. request={async (params) =>
  254. service.query({ ...params, sorts: [{ name: 'createTime', order: 'desc' }] })
  255. }
  256. />
  257. {visible && (
  258. <Save
  259. close={() => {
  260. setVisible(false);
  261. }}
  262. data={current}
  263. reload={() => {
  264. setVisible(false);
  265. actionRef.current?.reload();
  266. }}
  267. />
  268. )}
  269. </PageContainer>
  270. );
  271. });
  272. export default DataSource;