AutoAccessTaskAdd.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import React, { useState } from "react";
  2. import { Form, Input, Button, Card, message, Space, Select } from "antd";
  3. import { ArrowLeftOutlined, SaveOutlined } from "@ant-design/icons";
  4. import { useNavigate } from "react-router-dom";
  5. import { autoAccessTasksApi } from "../services/api";
  6. import { generateSearchTaskId } from "./PendingToolsAdd";
  7. const { TextArea } = Input;
  8. const { Option } = Select;
  9. export const ACCESS_PROVIDER_OPTIONS = [
  10. { value: "official", label: "official" },
  11. { value: "302ai", label: "302ai" },
  12. ];
  13. const AutoAccessTaskAdd = () => {
  14. const [form] = Form.useForm();
  15. const [loading, setLoading] = useState(false);
  16. const [accessType, setAccessType] = useState(undefined);
  17. const navigate = useNavigate();
  18. // 接入方式选项
  19. const ACCESS_TYPE_OPTIONS = [
  20. { value: "api_no_crack", label: "API无破解" },
  21. { value: "api_crack", label: "API破解" },
  22. { value: "browser_auto_operate", label: "浏览器自动操作" },
  23. ];
  24. const handleSubmit = async (values) => {
  25. try {
  26. setLoading(true);
  27. const submitData = {
  28. access_task_id: generateSearchTaskId(),
  29. tools_name: values.toolsName,
  30. tools_function_name: values.toolsFunctionName,
  31. tools_function_desc: values.toolsFunctionDesc,
  32. access_type: values.accessType,
  33. api_provider: values.apiProvider || null,
  34. api_doc: values.apiDoc || null,
  35. operate_path_data: values.operatePathData || null,
  36. };
  37. await autoAccessTasksApi.create(submitData);
  38. message.success("新增接入任务成功!");
  39. navigate("/auto-access-tasks");
  40. } catch (error) {
  41. console.error("新增接入任务失败:", error);
  42. message.error("新增接入任务失败,请重试");
  43. } finally {
  44. setLoading(false);
  45. }
  46. };
  47. const handleBack = () => {
  48. navigate("/auto-access-tasks");
  49. };
  50. const handleAccessTypeChange = (value) => {
  51. setAccessType(value);
  52. // 清空相关字段
  53. form.setFieldsValue({
  54. apiProvider: undefined,
  55. apiDoc: undefined,
  56. operatePathData: undefined,
  57. });
  58. };
  59. return (
  60. <div className="p-6">
  61. <Card
  62. title={
  63. <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", width: "100%" }}>
  64. <Button
  65. type="text"
  66. icon={<ArrowLeftOutlined />}
  67. onClick={handleBack}
  68. >
  69. 返回列表
  70. </Button>
  71. <span style={{ fontSize: "18px", fontWeight: "bold", flex: 1, textAlign: "center" }}>新增接入任务</span>
  72. <div style={{ width: "80px" }}></div>
  73. </div>
  74. }
  75. >
  76. <Form
  77. form={form}
  78. layout="vertical"
  79. onFinish={handleSubmit}
  80. autoComplete="off"
  81. >
  82. <Form.Item
  83. label="工具名称"
  84. name="toolsName"
  85. rules={[
  86. { required: true, message: "请输入工具名称" },
  87. { max: 100, message: "工具名称不能超过100个字符" },
  88. ]}
  89. >
  90. <Input placeholder="请输入工具名称" />
  91. </Form.Item>
  92. <Form.Item
  93. label="接入方式"
  94. name="accessType"
  95. rules={[{ required: true, message: "请选择接入方式" }]}
  96. >
  97. <Select
  98. placeholder="请选择接入方式"
  99. onChange={handleAccessTypeChange}
  100. >
  101. {ACCESS_TYPE_OPTIONS.map((option) => (
  102. <Option
  103. key={option.value}
  104. value={option.value}
  105. >
  106. {option.label}
  107. </Option>
  108. ))}
  109. </Select>
  110. </Form.Item>
  111. {/* API接入方式时显示的字段 */}
  112. {(accessType === "api_no_crack" || accessType === "api_crack") && (
  113. <>
  114. <Form.Item
  115. label="API提供方"
  116. name="apiProvider"
  117. rules={[{ required: true, message: "请选择API提供方" }]}
  118. >
  119. <Select placeholder="请选择API提供方">
  120. {ACCESS_PROVIDER_OPTIONS.map((option) => (
  121. <Option
  122. key={option.value}
  123. value={option.value}
  124. >
  125. {option.label}
  126. </Option>
  127. ))}
  128. </Select>
  129. </Form.Item>
  130. <Form.Item
  131. label="API文档"
  132. name="apiDoc"
  133. rules={[
  134. { required: true, message: "请输入API文档" },
  135. { max: 500, message: "API文档不能超过500个字符" },
  136. ]}
  137. >
  138. <TextArea
  139. rows={4}
  140. placeholder="请输入API文档内容或链接"
  141. showCount
  142. maxLength={500}
  143. />
  144. </Form.Item>
  145. </>
  146. )}
  147. {/* 浏览器自动操作接入方式时显示的字段 */}
  148. {accessType === "browser_auto_operate" && (
  149. <Form.Item
  150. label="操作路径数据"
  151. name="operatePathData"
  152. rules={[
  153. { required: true, message: "请输入操作路径数据" },
  154. { max: 1000, message: "操作路径数据不能超过1000个字符" },
  155. ]}
  156. >
  157. <TextArea
  158. rows={6}
  159. placeholder="请输入浏览器自动操作的路径数据"
  160. showCount
  161. maxLength={1000}
  162. />
  163. </Form.Item>
  164. )}
  165. <Form.Item
  166. label="工具功能名称"
  167. name="toolsFunctionName"
  168. rules={[{ max: 100, message: "工具功能名称不能超过100个字符" }]}
  169. >
  170. <Input placeholder="请输入工具功能名称" />
  171. </Form.Item>
  172. <Form.Item
  173. label="工具功能描述"
  174. name="toolsFunctionDesc"
  175. rules={[{ max: 500, message: "工具功能描述不能超过500个字符" }]}
  176. >
  177. <TextArea
  178. rows={4}
  179. placeholder="请输入工具功能描述"
  180. showCount
  181. maxLength={500}
  182. />
  183. </Form.Item>
  184. <Form.Item style={{ textAlign: "center" }}>
  185. <Space>
  186. <Button
  187. type="primary"
  188. htmlType="submit"
  189. loading={loading}
  190. icon={<SaveOutlined />}
  191. >
  192. 保存
  193. </Button>
  194. <Button onClick={handleBack}>取消</Button>
  195. </Space>
  196. </Form.Item>
  197. </Form>
  198. </Card>
  199. </div>
  200. );
  201. };
  202. export default AutoAccessTaskAdd;