autoAccessTasks.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. const express = require("express");
  2. const router = express.Router();
  3. const { executeQuery } = require("../config/database");
  4. router.get("/", async (req, res) => {
  5. try {
  6. const { page = 1, pageSize = 10, search, status } = req.query;
  7. const offset = (page - 1) * pageSize;
  8. // 构建WHERE条件
  9. let whereConditions = [];
  10. let queryParams = [];
  11. // 添加搜索条件
  12. if (search) {
  13. whereConditions.push("tools_name LIKE ?");
  14. queryParams.push(`%${search}%`);
  15. }
  16. // 添加状态过滤条件
  17. if (status !== undefined && status !== "") {
  18. whereConditions.push("status = ?");
  19. queryParams.push(parseInt(status));
  20. }
  21. // 构建WHERE子句
  22. const whereClause = whereConditions.length > 0 ? `WHERE ${whereConditions.join(" AND ")}` : "";
  23. const sql = `
  24. SELECT access_task_id, search_task_id, tools_name, tools_function_name,
  25. access_type, tools_function_desc, api_doc, api_class_name, api_provider,
  26. operate_path_data, origin_content_link, status, fail_reason,
  27. create_time, update_time
  28. FROM tools_auto_access_task
  29. ${whereClause}
  30. ORDER BY create_time DESC
  31. LIMIT ? OFFSET ?
  32. `;
  33. const countSql = `SELECT COUNT(*) as total FROM tools_auto_access_task ${whereClause}`;
  34. // 为查询添加分页参数
  35. const sqlParams = [...queryParams, parseInt(pageSize), offset];
  36. const countParams = [...queryParams];
  37. const [data, countResult] = await Promise.all([executeQuery(sql, sqlParams), executeQuery(countSql, countParams)]);
  38. res.json({
  39. data,
  40. total: countResult[0].total,
  41. page: parseInt(page),
  42. pageSize: parseInt(pageSize),
  43. });
  44. } catch (error) {
  45. console.error("Error fetching auto access tasks:", error);
  46. res.status(500).json({ error: "Internal server error" });
  47. }
  48. });
  49. router.get("/:id", async (req, res) => {
  50. try {
  51. const { id } = req.params;
  52. const sql = `
  53. SELECT access_task_id, search_task_id, tools_name, tools_function_name,
  54. access_type, tools_function_desc, api_doc, api_class_name, api_provider,
  55. operate_path_data, origin_content_link, status, fail_reason,
  56. create_time, update_time
  57. FROM tools_auto_access_task
  58. WHERE access_task_id = ?
  59. `;
  60. const data = await executeQuery(sql, [id]);
  61. if (data.length === 0) {
  62. return res.status(404).json({ error: "Task not found" });
  63. }
  64. res.json(data[0]);
  65. } catch (error) {
  66. console.error("Error fetching auto access task detail:", error);
  67. res.status(500).json({ error: "Internal server error" });
  68. }
  69. });
  70. router.put("/:id", async (req, res) => {
  71. try {
  72. const { id } = req.params;
  73. const {
  74. search_task_id,
  75. tools_name,
  76. tools_function_name,
  77. access_type,
  78. tools_function_desc,
  79. api_doc,
  80. api_class_name,
  81. api_provider,
  82. operate_path_data,
  83. origin_content_link,
  84. status,
  85. fail_reason,
  86. } = req.body;
  87. const sql = `
  88. UPDATE tools_auto_access_task
  89. SET search_task_id = ?, tools_name = ?, tools_function_name = ?,
  90. access_type = ?, tools_function_desc = ?, api_doc = ?,
  91. api_class_name = ?, api_provider = ?, operate_path_data = ?, origin_content_link = ?,
  92. status = ?, fail_reason = ?, update_time = NOW()
  93. WHERE access_task_id = ?
  94. `;
  95. await executeQuery(sql, [
  96. search_task_id ?? null,
  97. tools_name ?? null,
  98. tools_function_name ?? null,
  99. access_type ?? null,
  100. tools_function_desc ?? null,
  101. api_doc ?? null,
  102. api_class_name ?? null,
  103. api_provider ?? null,
  104. operate_path_data ?? null,
  105. origin_content_link ?? null,
  106. status ?? null,
  107. fail_reason ?? null,
  108. id,
  109. ]);
  110. res.json({ message: "Task updated successfully" });
  111. } catch (error) {
  112. console.error("Error updating auto access task:", error);
  113. res.status(500).json({ error: "Internal server error" });
  114. }
  115. });
  116. router.post("/", async (req, res) => {
  117. try {
  118. const {
  119. access_task_id,
  120. tools_name,
  121. tools_function_name,
  122. tools_function_desc,
  123. access_type,
  124. api_provider,
  125. api_doc,
  126. operate_path_data,
  127. status = 0,
  128. } = req.body;
  129. // 验证必填字段
  130. if (!access_task_id || !tools_name || access_type === undefined) {
  131. return res.status(400).json({ error: "Missing required fields" });
  132. }
  133. // 根据接入方式验证条件必填字段
  134. if (access_type === "api_no_crack" || access_type === "api_crack") {
  135. // API接入方式
  136. if (!api_provider || !api_doc) {
  137. return res.status(400).json({ error: "API provider and API doc are required for API access type" });
  138. }
  139. } else if (access_type === "browser_auto_operate") {
  140. // 浏览器自动操作接入方式
  141. if (!operate_path_data) {
  142. return res.status(400).json({ error: "Operation path data is required for browser automation access type" });
  143. }
  144. }
  145. const sql = `
  146. INSERT INTO tools_auto_access_task (
  147. access_task_id, tools_name, tools_function_name, tools_function_desc,
  148. access_type, api_provider, api_doc, operate_path_data, status,
  149. create_time, update_time
  150. ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
  151. `;
  152. await executeQuery(sql, [
  153. access_task_id,
  154. tools_name,
  155. tools_function_name || null,
  156. tools_function_desc || null,
  157. access_type,
  158. api_provider || null,
  159. api_doc || null,
  160. operate_path_data || null,
  161. status,
  162. ]);
  163. res.status(201).json({
  164. message: "Auto access task created successfully",
  165. access_task_id
  166. });
  167. } catch (error) {
  168. console.error("Error creating auto access task:", error);
  169. res.status(500).json({ error: "Internal server error" });
  170. }
  171. });
  172. module.exports = router;