123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- import React, { useState, useEffect } from "react";
- import { Form, Input, Select, Button, Card, Descriptions, Tag, message, Spin, Row, Col, Tooltip } from "antd";
- import { useParams, useNavigate, useLocation } from "react-router-dom";
- import { ArrowLeftOutlined, CopyOutlined } from "@ant-design/icons";
- import { toolsLibraryApi } from "../services/api";
- import moment from "moment";
- const { TextArea } = Input;
- const { Option } = Select;
- const ToolsLibraryDetail = () => {
- const [form] = Form.useForm();
- const [data, setData] = useState(null);
- const [loading, setLoading] = useState(true);
- const [saving, setSaving] = useState(false);
- const { id } = useParams();
- const navigate = useNavigate();
- const location = useLocation();
- const isEditMode = location.search.includes("mode=edit");
- const getStatusColor = (status) => {
- const statusMap = {
- normal: "success",
- offline: "default",
- unpublished: "default",
- published: "success",
- };
- return statusMap[status] || "warning";
- };
- const getStatusText = (status) => {
- const statusMap = {
- normal: "正常",
- offline: "已下线",
- unpublished: "待发布",
- published: "已发布",
- };
- return statusMap[status] || status;
- };
- const getCallTypeText = (type) => {
- const typeMap = {
- api: "API调用",
- browser_auto_operate: "浏览器自动操作",
- };
- return typeMap[type] || type;
- };
- const getApiProviderText = (provider) => {
- const providerMap = {
- official: "官方",
- "302ai": "302AI",
- official_api: "官方API",
- };
- return providerMap[provider] || provider;
- };
- const fetchData = async () => {
- try {
- const response = await toolsLibraryApi.getDetail(id);
- setData(response.data);
- form.setFieldsValue(response.data);
- } catch (error) {
- message.error("获取详情失败");
- } finally {
- setLoading(false);
- }
- };
- const handleSave = async (values) => {
- setSaving(true);
- try {
- await toolsLibraryApi.update(id, values);
- message.success("更新成功");
- navigate("/tools-library");
- } catch (error) {
- message.error("更新失败");
- } finally {
- setSaving(false);
- }
- };
- const handleCopyParams = () => {
- const paramsDefinition = form.getFieldValue("params_definition");
- if (paramsDefinition) {
- navigator.clipboard
- .writeText(paramsDefinition)
- .then(() => {
- message.success("参数定义已复制到剪贴板");
- })
- .catch(() => {
- message.error("复制失败");
- });
- } else {
- message.warning("参数定义为空");
- }
- };
- useEffect(() => {
- fetchData();
- }, [id]);
- if (loading) {
- return (
- <div style={{ textAlign: "center", padding: "50px" }}>
- <Spin size="large" />
- </div>
- );
- }
- if (!data) {
- return <div>数据不存在</div>;
- }
- return (
- <div className="detail-container">
- <Button
- icon={<ArrowLeftOutlined />}
- onClick={() => navigate("/tools-library")}
- style={{ marginBottom: 16 }}
- >
- 返回列表
- </Button>
- {isEditMode ? (
- <Card
- title="编辑工具"
- style={{ marginBottom: 24 }}
- >
- <Form
- form={form}
- layout="vertical"
- onFinish={handleSave}
- className="w-full"
- >
- <Row gutter={24}>
- <Col span={12}>
- <Form.Item
- label="工具名称"
- name="tools_name"
- rules={[{ required: true, message: "请输入工具名称" }]}
- >
- <Input />
- </Form.Item>
- </Col>
- <Col span={12}>
- {" "}
- <Form.Item
- label="工具功能名称"
- name="tools_function_name"
- rules={[{ required: true, message: "请输入工具功能名称" }]}
- >
- <Input />
- </Form.Item>
- </Col>
- </Row>
- <Row gutter={24}>
- <Col span={12}>
- <Form.Item
- label="状态"
- name="status"
- rules={[{ required: true, message: "请选择状态" }]}
- >
- <Select>
- <Option value="normal">正常</Option>
- <Option value="offline">已下线</Option>
- <Option value="unpublished">待发布</Option>
- <Option value="published">已发布</Option>
- </Select>
- </Form.Item>
- </Col>
- <Col span={12}>
- <Form.Item
- label="调用方式"
- name="call_type"
- >
- <Select>
- <Option value="api">API调用</Option>
- <Option value="browser_auto_operate">浏览器自动操作</Option>
- </Select>
- </Form.Item>
- </Col>
- </Row>
- <Row gutter={24}>
- <Col span={12}>
- {" "}
- <Form.Item
- label="工具全称"
- name="tools_full_name"
- >
- <Input />
- </Form.Item>
- </Col>
- <Col span={12}>
- {" "}
- <Form.Item
- label="工具版本"
- name="tools_version"
- >
- <Input />
- </Form.Item>
- </Col>
- </Row>
- <Row gutter={24}>
- <Col span={12}>
- <Form.Item
- label="API提供方"
- name="api_provider"
- >
- <Select>
- <Option value="official">官方</Option>
- <Option value="302ai">302AI</Option>
- <Option value="official_api">官方API</Option>
- </Select>
- </Form.Item>
- </Col>
- <Col span={12}>
- {" "}
- <Form.Item
- label="API路径"
- name="api_url_path"
- >
- <Input />
- </Form.Item>
- </Col>
- </Row>
- <Form.Item
- label="工具描述"
- name="tools_desc"
- >
- <TextArea rows={4} />
- </Form.Item>
- <Form.Item
- label="操作路径数据"
- name="operate_path_data"
- >
- <TextArea rows={4} />
- </Form.Item>
- <Form.Item
- label={
- <div style={{ display: "flex", alignItems: "center", gap: "8px" }}>
- 参数定义
- <Tooltip title="点击复制参数定义">
- <Button
- type="text"
- style={{ color: "blue" }}
- size="small"
- icon={<CopyOutlined />}
- onClick={handleCopyParams}
- />
- </Tooltip>
- </div>
- }
- name="params_definition"
- >
- <TextArea rows={6} />
- </Form.Item>
- <Form.Item
- label="响应数据说明"
- name="response_desc"
- >
- <TextArea rows={4} />
- </Form.Item>
- <div className="button-group">
- <Button onClick={() => navigate("/tools-library")}>取消</Button>
- <Button
- type="primary"
- htmlType="submit"
- loading={saving}
- >
- 保存
- </Button>
- </div>
- </Form>
- </Card>
- ) : (
- <Card title="工具详情">
- <Descriptions
- column={2}
- bordered
- labelStyle={{ width: "150px" }}
- >
- <Descriptions.Item label="工具ID">{data.tools_id}</Descriptions.Item>
- <Descriptions.Item label="工具名称">{data.tools_name}</Descriptions.Item>
- <Descriptions.Item label="工具功能名称">{data.tools_function_name}</Descriptions.Item>
- <Descriptions.Item label="工具全称">{data.tools_full_name}</Descriptions.Item>
- <Descriptions.Item label="工具版本">
- <Tag color="geekblue">{data.tools_version}</Tag>
- </Descriptions.Item>
- <Descriptions.Item label="自动接入任务ID">{data.access_task_id}</Descriptions.Item>
- <Descriptions.Item label="状态">
- <Tag color={getStatusColor(data.status)}>{getStatusText(data.status)}</Tag>
- </Descriptions.Item>
- <Descriptions.Item label="调用方式">
- <Tag color="blue">{getCallTypeText(data.call_type)}</Tag>
- </Descriptions.Item>
- <Descriptions.Item label="API提供方">
- <Tag color="purple">{getApiProviderText(data.api_provider)}</Tag>
- </Descriptions.Item>
- <Descriptions.Item label="API路径">{data.api_url_path}</Descriptions.Item>
- <Descriptions.Item
- label="工具描述"
- span={2}
- >
- {data.tools_desc}
- </Descriptions.Item>
- <Descriptions.Item
- label="操作路径数据"
- span={2}
- >
- <div style={{ maxHeight: "200px", overflow: "auto" }}>
- <pre style={{ whiteSpace: "pre-wrap", wordBreak: "break-word" }}>{data.operate_path_data || "无"}</pre>
- </div>
- </Descriptions.Item>
- <Descriptions.Item
- label={
- <div style={{ display: "flex", alignItems: "center", gap: "8px" }}>
- 参数定义
- <Tooltip title="点击复制参数定义">
- <Button
- type="text"
- style={{ color: "blue" }}
- size="small"
- icon={<CopyOutlined />}
- onClick={() => {
- const paramsDefinition = data.params_definition;
- if (paramsDefinition) {
- navigator.clipboard.writeText(paramsDefinition).then(() => {
- message.success("参数定义已复制到剪贴板");
- }).catch(() => {
- message.error("复制失败,请手动复制");
- });
- } else {
- message.warning("参数定义为空,无法复制");
- }
- }}
- />
- </Tooltip>
- </div>
- }
- span={2}
- >
- <div style={{ maxHeight: "200px", overflow: "auto" }}>
- <pre style={{ whiteSpace: "pre-wrap", wordBreak: "break-word" }}>{data.params_definition || "无"}</pre>
- </div>
- </Descriptions.Item>
- <Descriptions.Item
- label="响应数据说明"
- span={2}
- >
- <div style={{ maxHeight: "200px", overflow: "auto" }}>
- <pre style={{ whiteSpace: "pre-wrap", wordBreak: "break-word" }}>{data.response_desc || "无"}</pre>
- </div>
- </Descriptions.Item>
- <Descriptions.Item label="创建时间">
- {moment(data.create_time).format("YYYY-MM-DD HH:mm:ss")}
- </Descriptions.Item>
- <Descriptions.Item label="更新时间">
- {moment(data.update_time).format("YYYY-MM-DD HH:mm:ss")}
- </Descriptions.Item>
- </Descriptions>
- </Card>
- )}
- </div>
- );
- };
- export default ToolsLibraryDetail;
|