index.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  3. import type { ProtocolItem } from '@/pages/link/Protocol/typings';
  4. import { Badge, message } from 'antd';
  5. import { useEffect, useRef, useState } from 'react';
  6. import {
  7. DeleteOutlined,
  8. EditOutlined,
  9. PlayCircleOutlined,
  10. PlusOutlined,
  11. StopOutlined,
  12. } from '@ant-design/icons';
  13. import Service from '@/pages/link/Protocol/service';
  14. import { useIntl, useLocation } from 'umi';
  15. import SearchComponent from '@/components/SearchComponent';
  16. import { PermissionButton, ProTableCard } from '@/components';
  17. import ProcotolCard from '@/components/ProTableCard/CardItems/protocol';
  18. import Save from './save';
  19. export const service = new Service('protocol');
  20. const Protocol = () => {
  21. const actionRef = useRef<ActionType>();
  22. const [visible, setVisible] = useState<boolean>(false);
  23. const [current, setCurrent] = useState<ProtocolItem | undefined>();
  24. const [searchParams, setSearchParams] = useState<any>({});
  25. const { permission } = PermissionButton.usePermission('link/Protocol');
  26. const intl = useIntl();
  27. const modifyState = async (id: string, type: 'deploy' | 'un-deploy') => {
  28. const resp = await service.modifyState(id, type);
  29. if (resp.status === 200) {
  30. message.success('操作成功!');
  31. } else {
  32. message.error(resp?.message || '操作失败');
  33. }
  34. actionRef.current?.reload();
  35. };
  36. const columns: ProColumns<ProtocolItem>[] = [
  37. {
  38. dataIndex: 'id',
  39. title: 'ID',
  40. sorter: true,
  41. ellipsis: true,
  42. defaultSortOrder: 'ascend',
  43. },
  44. {
  45. dataIndex: 'name',
  46. ellipsis: true,
  47. title: intl.formatMessage({
  48. id: 'pages.table.name',
  49. defaultMessage: '名称',
  50. }),
  51. },
  52. {
  53. dataIndex: 'type',
  54. title: '类型',
  55. ellipsis: true,
  56. valueType: 'select',
  57. valueEnum: {
  58. jar: {
  59. text: 'jar',
  60. status: 'jar',
  61. },
  62. local: {
  63. text: 'local',
  64. status: 'local',
  65. },
  66. },
  67. },
  68. {
  69. dataIndex: 'state',
  70. title: '状态',
  71. renderText: (text) => (
  72. <Badge color={text !== 1 ? 'red' : 'green'} text={text !== 1 ? '未发布' : '已发布'} />
  73. ),
  74. },
  75. {
  76. dataIndex: 'description',
  77. ellipsis: true,
  78. title: '说明',
  79. },
  80. {
  81. title: intl.formatMessage({
  82. id: 'pages.data.option',
  83. defaultMessage: '操作',
  84. }),
  85. valueType: 'option',
  86. render: (text, record) => [
  87. <PermissionButton
  88. isPermission={permission.update}
  89. key="edit"
  90. onClick={() => {
  91. setCurrent(record);
  92. setVisible(true);
  93. }}
  94. type={'link'}
  95. style={{ padding: 0 }}
  96. tooltip={{
  97. title: intl.formatMessage({
  98. id: 'pages.data.option.edit',
  99. defaultMessage: '编辑',
  100. }),
  101. }}
  102. >
  103. <EditOutlined />
  104. </PermissionButton>,
  105. <PermissionButton
  106. isPermission={permission.action}
  107. key="action"
  108. type={'link'}
  109. style={{ padding: 0 }}
  110. tooltip={{
  111. title: record.state === 1 ? '撤销' : '发布',
  112. }}
  113. popConfirm={{
  114. title: `确认${record.state === 1 ? '撤销' : '发布'}`,
  115. onConfirm: () => {
  116. if (record.state === 1) {
  117. modifyState(record.id, 'un-deploy');
  118. } else {
  119. modifyState(record.id, 'deploy');
  120. }
  121. },
  122. }}
  123. >
  124. {record.state === 1 ? <StopOutlined /> : <PlayCircleOutlined />}
  125. </PermissionButton>,
  126. <PermissionButton
  127. isPermission={permission.delete}
  128. tooltip={{
  129. title: record.state !== 1 ? '删除' : '请先禁用该协议,再删除',
  130. }}
  131. style={{ padding: 0 }}
  132. disabled={record.state === 1}
  133. popConfirm={{
  134. title: '确认删除',
  135. disabled: record.state === 1,
  136. onConfirm: async () => {
  137. const resp: any = await service.remove(record.id);
  138. if (resp.status === 200) {
  139. message.success(
  140. intl.formatMessage({
  141. id: 'pages.data.option.success',
  142. defaultMessage: '操作成功!',
  143. }),
  144. );
  145. actionRef.current?.reload();
  146. } else {
  147. message.error(resp?.message || '操作失败');
  148. }
  149. },
  150. }}
  151. key="delete"
  152. type="link"
  153. >
  154. <DeleteOutlined />
  155. </PermissionButton>,
  156. ],
  157. },
  158. ];
  159. const location = useLocation();
  160. useEffect(() => {
  161. if ((location as any).query?.save === 'true') {
  162. setCurrent(undefined);
  163. setVisible(true);
  164. }
  165. }, []);
  166. return (
  167. <PageContainer>
  168. <SearchComponent<ProtocolItem>
  169. field={columns}
  170. target="Protocol"
  171. onSearch={(data) => {
  172. actionRef.current?.reset?.();
  173. setSearchParams(data);
  174. }}
  175. />
  176. <ProTableCard<ProtocolItem>
  177. columns={columns}
  178. actionRef={actionRef}
  179. params={searchParams}
  180. options={{ fullScreen: true }}
  181. request={(params) =>
  182. service.query({
  183. ...params,
  184. sorts: [
  185. {
  186. name: 'createTime',
  187. order: 'desc',
  188. },
  189. ],
  190. })
  191. }
  192. rowKey="id"
  193. search={false}
  194. pagination={{ pageSize: 10 }}
  195. headerTitle={[
  196. <PermissionButton
  197. onClick={() => {
  198. setVisible(true);
  199. setCurrent(undefined);
  200. }}
  201. style={{ marginRight: 12 }}
  202. isPermission={permission.add}
  203. key="button"
  204. icon={<PlusOutlined />}
  205. type="primary"
  206. >
  207. {intl.formatMessage({
  208. id: 'pages.data.option.add',
  209. defaultMessage: '新增',
  210. })}
  211. </PermissionButton>,
  212. ]}
  213. cardRender={(record) => (
  214. <ProcotolCard
  215. {...record}
  216. actions={[
  217. <PermissionButton
  218. isPermission={permission.update}
  219. key="edit"
  220. onClick={() => {
  221. setCurrent(record);
  222. setVisible(true);
  223. }}
  224. type={'link'}
  225. style={{ padding: 0 }}
  226. tooltip={{
  227. title: intl.formatMessage({
  228. id: 'pages.data.option.edit',
  229. defaultMessage: '编辑',
  230. }),
  231. }}
  232. >
  233. <EditOutlined />
  234. 编辑
  235. </PermissionButton>,
  236. <PermissionButton
  237. isPermission={permission.action}
  238. key="action"
  239. type={'link'}
  240. style={{ padding: 0 }}
  241. tooltip={{
  242. title: record.state === 1 ? '撤销' : '发布',
  243. }}
  244. popConfirm={{
  245. title: `确认${record.state === 1 ? '撤销' : '发布'}`,
  246. onConfirm: () => {
  247. if (record.state === 1) {
  248. modifyState(record.id, 'un-deploy');
  249. } else {
  250. modifyState(record.id, 'deploy');
  251. }
  252. },
  253. }}
  254. >
  255. {record.state === 1 ? <StopOutlined /> : <PlayCircleOutlined />}
  256. {record.state === 1 ? '撤销' : '发布'}
  257. </PermissionButton>,
  258. <PermissionButton
  259. isPermission={permission.delete}
  260. tooltip={{
  261. title: record.state !== 1 ? '删除' : '请先禁用该协议,再删除',
  262. }}
  263. disabled={record.state === 1}
  264. popConfirm={{
  265. title: '确认删除',
  266. disabled: record.state === 1,
  267. onConfirm: async () => {
  268. const resp: any = await service.remove(record.id);
  269. if (resp.status === 200) {
  270. message.success(
  271. intl.formatMessage({
  272. id: 'pages.data.option.success',
  273. defaultMessage: '操作成功!',
  274. }),
  275. );
  276. actionRef.current?.reload();
  277. } else {
  278. message.error(resp?.message || '操作失败');
  279. }
  280. },
  281. }}
  282. key="delete"
  283. type="link"
  284. >
  285. <DeleteOutlined />
  286. </PermissionButton>,
  287. ]}
  288. />
  289. )}
  290. />
  291. {visible && (
  292. <Save
  293. data={current}
  294. close={() => {
  295. setVisible(false);
  296. }}
  297. reload={() => {
  298. actionRef.current?.reload();
  299. setVisible(false);
  300. }}
  301. />
  302. )}
  303. </PageContainer>
  304. );
  305. };
  306. export default Protocol;