autoAccessTasks.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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, website_location, website_name, website_address, 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, website_location, website_name, website_address, 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. website_location,
  85. website_name,
  86. website_address,
  87. status,
  88. fail_reason,
  89. } = req.body;
  90. const sql = `
  91. UPDATE tools_auto_access_task
  92. SET search_task_id = ?, tools_name = ?, tools_function_name = ?,
  93. access_type = ?, tools_function_desc = ?, api_doc = ?,
  94. api_class_name = ?, api_provider = ?, operate_path_data = ?, origin_content_link = ?,
  95. website_location = ?, website_name = ?, website_address = ?, status = ?, fail_reason = ?, update_time = NOW()
  96. WHERE access_task_id = ?
  97. `;
  98. await executeQuery(sql, [
  99. search_task_id ?? null,
  100. tools_name ?? null,
  101. tools_function_name ?? null,
  102. access_type ?? null,
  103. tools_function_desc ?? null,
  104. api_doc ?? null,
  105. api_class_name ?? null,
  106. api_provider ?? null,
  107. operate_path_data ?? null,
  108. origin_content_link ?? null,
  109. website_location ?? null,
  110. website_name ?? null,
  111. website_address ?? null,
  112. status ?? null,
  113. fail_reason ?? null,
  114. id,
  115. ]);
  116. res.json({ message: "Task updated successfully" });
  117. } catch (error) {
  118. console.error("Error updating auto access task:", error);
  119. res.status(500).json({ error: "Internal server error" });
  120. }
  121. });
  122. router.post("/", async (req, res) => {
  123. try {
  124. const {
  125. access_task_id,
  126. tools_name,
  127. tools_function_name,
  128. tools_function_desc,
  129. access_type,
  130. api_provider,
  131. api_doc,
  132. operate_path_data,
  133. website_location,
  134. website_name,
  135. website_address,
  136. } = req.body;
  137. // 验证必填字段
  138. if (!access_task_id || !tools_name || access_type === undefined) {
  139. return res.status(400).json({ error: "Missing required fields" });
  140. }
  141. // 根据接入方式验证条件必填字段
  142. if (access_type === "api_no_crack" || access_type === "api_crack") {
  143. // API接入方式
  144. if (!api_provider || !api_doc) {
  145. return res.status(400).json({ error: "API provider and API doc are required for API access type" });
  146. }
  147. } else if (access_type === "browser_auto_operate") {
  148. // 浏览器自动操作接入方式
  149. if (!operate_path_data) {
  150. return res.status(400).json({ error: "Operation path data is required for browser automation access type" });
  151. }
  152. }
  153. const sql = `
  154. INSERT INTO tools_auto_access_task (
  155. access_task_id, tools_name, tools_function_name, tools_function_desc,
  156. access_type, api_provider, api_doc, operate_path_data, website_location, website_name, website_address,
  157. create_time, update_time
  158. ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
  159. `;
  160. // 保证 operate_path_data 为字符串(可能传入对象)
  161. const operatePathValue =
  162. typeof operate_path_data === "object" ? JSON.stringify(operate_path_data) : operate_path_data;
  163. await executeQuery(sql, [
  164. access_task_id,
  165. tools_name,
  166. tools_function_name || null,
  167. tools_function_desc || null,
  168. access_type,
  169. api_provider || null,
  170. api_doc || null,
  171. operatePathValue || null,
  172. website_location || null,
  173. website_name || null,
  174. website_address || null,
  175. ]);
  176. res.status(201).json({
  177. message: "Auto access task created successfully",
  178. access_task_id,
  179. });
  180. } catch (error) {
  181. console.error("Error creating auto access task:", error);
  182. res.status(500).json({ error: "Internal server error" });
  183. }
  184. });
  185. module.exports = router;