index.tsx 8.2 KB

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