index.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { ActionType, ProColumns, ProTable } from '@ant-design/pro-components';
  2. import React, { useRef, useState } from 'react';
  3. import { App, Button, Modal } from 'antd';
  4. import {
  5. deleteAppRobotCommand,
  6. listAppRobotCommand,
  7. updateAppRobotCommand,
  8. } from '@/services/api/appRobotCommand';
  9. import { isEqual } from 'lodash-es';
  10. import AddAppRobotCommand from '@/pages/AppManage/components/AddAppRobotCommand';
  11. import { whitespaceForbidden } from '@/constants/rules';
  12. type TableListItem = API.ListAppRobotCommandVo;
  13. /** 应用机器人管理组件 */
  14. const AppRobotCommandManage: React.FC<{
  15. /** 受控的打开关闭 */
  16. open: boolean;
  17. /** 打开关闭的事件 */
  18. setOpen: (v: boolean) => void;
  19. /** 应用编码 */
  20. appId: string;
  21. /** 机器人编码 */
  22. robotCode: string;
  23. }> = ({ open, setOpen, appId, robotCode }) => {
  24. // Table action 的引用,便于自定义触发
  25. const actionRef = useRef<ActionType>();
  26. // 正在编辑的行
  27. const [editableKeys, setEditableRowKeys] = useState<React.Key[]>([]);
  28. const { message } = App.useApp();
  29. /**
  30. * 获取数据
  31. * @param params 参数
  32. * @param sort 排序
  33. */
  34. const getDataSource = async (params: any, sort: any) => {
  35. const res = await listAppRobotCommand({
  36. ...params,
  37. sort,
  38. appId,
  39. robotCode,
  40. });
  41. return Promise.resolve({
  42. success: true,
  43. data: res?.data?.list,
  44. total: res?.data?.total,
  45. });
  46. };
  47. // 表格列的配置描述
  48. const columns: ProColumns<TableListItem>[] = [
  49. {
  50. title: '命令',
  51. dataIndex: 'command',
  52. copyable: true,
  53. sorter: true,
  54. editable: false,
  55. },
  56. {
  57. title: 'url',
  58. dataIndex: 'url',
  59. copyable: true,
  60. search: false,
  61. formItemProps: {
  62. rules: [
  63. {
  64. required: true,
  65. },
  66. whitespaceForbidden,
  67. ],
  68. },
  69. },
  70. {
  71. title: '说明',
  72. dataIndex: 'description',
  73. copyable: true,
  74. search: false,
  75. },
  76. {
  77. title: '创建时间',
  78. dataIndex: 'createTime',
  79. valueType: 'dateTime',
  80. search: false,
  81. sorter: true,
  82. editable: false,
  83. },
  84. {
  85. title: '更新时间',
  86. dataIndex: 'updateTime',
  87. valueType: 'dateTime',
  88. search: false,
  89. sorter: true,
  90. editable: false,
  91. },
  92. {
  93. title: '操作',
  94. key: 'option',
  95. valueType: 'option',
  96. render: (text, record, index, action) => (
  97. <Button
  98. danger
  99. onClick={() => {
  100. action?.startEditable?.(record?.command as string);
  101. }}
  102. >
  103. 编辑
  104. </Button>
  105. ),
  106. },
  107. ];
  108. /**
  109. * 行保存
  110. * @param key 行 id,一般是唯一id
  111. * @param record 当前修改的行的值,只有 form 在内的会被设置
  112. * @param originRow 原始值,可以用于判断是否修改
  113. */
  114. const onSave = async (key: any, record: any, originRow: any) => {
  115. if (isEqual(record, originRow)) {
  116. return Promise.reject();
  117. }
  118. await updateAppRobotCommand({ ...record, appId, robotCode });
  119. message.success('修改成功');
  120. actionRef.current?.reload();
  121. return Promise.resolve();
  122. };
  123. /**
  124. * 行删除
  125. * @param key 行 id,一般是唯一id
  126. */
  127. const onDelete = async (key: any) => {
  128. await deleteAppRobotCommand({ command: key, appId, robotCode });
  129. message.success('删除成功');
  130. actionRef.current?.reload();
  131. return Promise.resolve();
  132. };
  133. return (
  134. <Modal
  135. title="机器人命令管理"
  136. open={open}
  137. onCancel={() => setOpen(false)}
  138. footer={false}
  139. destroyOnClose
  140. width={'100%'}
  141. >
  142. <ProTable<TableListItem>
  143. headerTitle="命令列表"
  144. rowKey="command"
  145. actionRef={actionRef}
  146. columns={columns}
  147. pagination={{
  148. showQuickJumper: true,
  149. defaultPageSize: 10,
  150. }}
  151. search={{
  152. layout: 'vertical',
  153. }}
  154. toolBarRender={() => [
  155. <AddAppRobotCommand
  156. key={'add'}
  157. actionRef={actionRef}
  158. appId={appId}
  159. robotCode={robotCode}
  160. />,
  161. ]}
  162. request={getDataSource}
  163. editable={{
  164. type: 'single',
  165. editableKeys,
  166. onChange: setEditableRowKeys,
  167. onSave,
  168. onDelete,
  169. }}
  170. />
  171. </Modal>
  172. );
  173. };
  174. export default AppRobotCommandManage;