123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- const express = require("express");
- const router = express.Router();
- const { executeQuery } = require("../config/database");
- router.get("/", async (req, res) => {
- try {
- const { page = 1, pageSize = 10, search, status } = req.query;
- const offset = (page - 1) * pageSize;
- // 构建WHERE条件
- let whereConditions = [];
- let queryParams = [];
- // 添加搜索条件
- if (search) {
- whereConditions.push("tools_name LIKE ?");
- queryParams.push(`%${search}%`);
- }
- // 添加状态过滤条件
- if (status !== undefined && status !== "") {
- whereConditions.push("status = ?");
- queryParams.push(parseInt(status));
- }
- // 构建WHERE子句
- const whereClause = whereConditions.length > 0 ? `WHERE ${whereConditions.join(" AND ")}` : "";
- const sql = `
- SELECT access_task_id, search_task_id, tools_name, tools_function_name,
- access_type, tools_function_desc, api_doc, api_class_name, api_provider,
- operate_path_data, origin_content_link, status, fail_reason,
- create_time, update_time
- FROM tools_auto_access_task
- ${whereClause}
- ORDER BY create_time DESC
- LIMIT ? OFFSET ?
- `;
- const countSql = `SELECT COUNT(*) as total FROM tools_auto_access_task ${whereClause}`;
- // 为查询添加分页参数
- const sqlParams = [...queryParams, parseInt(pageSize), offset];
- const countParams = [...queryParams];
- const [data, countResult] = await Promise.all([executeQuery(sql, sqlParams), executeQuery(countSql, countParams)]);
- res.json({
- data,
- total: countResult[0].total,
- page: parseInt(page),
- pageSize: parseInt(pageSize),
- });
- } catch (error) {
- console.error("Error fetching auto access tasks:", error);
- res.status(500).json({ error: "Internal server error" });
- }
- });
- router.get("/:id", async (req, res) => {
- try {
- const { id } = req.params;
- const sql = `
- SELECT access_task_id, search_task_id, tools_name, tools_function_name,
- access_type, tools_function_desc, api_doc, api_class_name, api_provider,
- operate_path_data, origin_content_link, status, fail_reason,
- create_time, update_time
- FROM tools_auto_access_task
- WHERE access_task_id = ?
- `;
- const data = await executeQuery(sql, [id]);
- if (data.length === 0) {
- return res.status(404).json({ error: "Task not found" });
- }
- res.json(data[0]);
- } catch (error) {
- console.error("Error fetching auto access task detail:", error);
- res.status(500).json({ error: "Internal server error" });
- }
- });
- router.put("/:id", async (req, res) => {
- try {
- const { id } = req.params;
- const {
- search_task_id,
- tools_name,
- tools_function_name,
- access_type,
- tools_function_desc,
- api_doc,
- api_class_name,
- api_provider,
- operate_path_data,
- origin_content_link,
- status,
- fail_reason,
- } = req.body;
- const sql = `
- UPDATE tools_auto_access_task
- SET search_task_id = ?, tools_name = ?, tools_function_name = ?,
- access_type = ?, tools_function_desc = ?, api_doc = ?,
- api_class_name = ?, api_provider = ?, operate_path_data = ?, origin_content_link = ?,
- status = ?, fail_reason = ?, update_time = NOW()
- WHERE access_task_id = ?
- `;
- await executeQuery(sql, [
- search_task_id ?? null,
- tools_name ?? null,
- tools_function_name ?? null,
- access_type ?? null,
- tools_function_desc ?? null,
- api_doc ?? null,
- api_class_name ?? null,
- api_provider ?? null,
- operate_path_data ?? null,
- origin_content_link ?? null,
- status ?? null,
- fail_reason ?? null,
- id,
- ]);
- res.json({ message: "Task updated successfully" });
- } catch (error) {
- console.error("Error updating auto access task:", error);
- res.status(500).json({ error: "Internal server error" });
- }
- });
- router.post("/", async (req, res) => {
- try {
- const {
- access_task_id,
- tools_name,
- tools_function_name,
- tools_function_desc,
- access_type,
- api_provider,
- api_doc,
- operate_path_data,
- status = 0,
- } = req.body;
- // 验证必填字段
- if (!access_task_id || !tools_name || access_type === undefined) {
- return res.status(400).json({ error: "Missing required fields" });
- }
- // 根据接入方式验证条件必填字段
- if (access_type === "api_no_crack" || access_type === "api_crack") {
- // API接入方式
- if (!api_provider || !api_doc) {
- return res.status(400).json({ error: "API provider and API doc are required for API access type" });
- }
- } else if (access_type === "browser_auto_operate") {
- // 浏览器自动操作接入方式
- if (!operate_path_data) {
- return res.status(400).json({ error: "Operation path data is required for browser automation access type" });
- }
- }
- const sql = `
- INSERT INTO tools_auto_access_task (
- access_task_id, tools_name, tools_function_name, tools_function_desc,
- access_type, api_provider, api_doc, operate_path_data, status,
- create_time, update_time
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
- `;
- await executeQuery(sql, [
- access_task_id,
- tools_name,
- tools_function_name || null,
- tools_function_desc || null,
- access_type,
- api_provider || null,
- api_doc || null,
- operate_path_data || null,
- status,
- ]);
- res.status(201).json({
- message: "Auto access task created successfully",
- access_task_id
- });
- } catch (error) {
- console.error("Error creating auto access task:", error);
- res.status(500).json({ error: "Internal server error" });
- }
- });
- module.exports = router;
|