accounts.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const express = require("express");
  2. const router = express.Router();
  3. const { executeQuery } = require("../config/database");
  4. // 列表:支持按 account 和 status 查询,分页
  5. router.get("/", async (req, res) => {
  6. try {
  7. const { page = 1, pageSize = 10, account, status } = req.query;
  8. const offset = (page - 1) * pageSize;
  9. let whereConditions = [];
  10. let sqlParams = [];
  11. let countParams = [];
  12. if (account) {
  13. whereConditions.push("account LIKE ?");
  14. sqlParams.push(`%${account}%`);
  15. countParams.push(`%${account}%`);
  16. }
  17. if (status !== undefined && status !== "") {
  18. whereConditions.push("status = ?");
  19. const statusVal = parseInt(status);
  20. sqlParams.push(statusVal);
  21. countParams.push(statusVal);
  22. }
  23. const whereClause = whereConditions.length > 0 ? `WHERE ${whereConditions.join(" AND ")}` : "";
  24. const sql = `
  25. SELECT id, hostname, account, info, status, create_time, update_time
  26. FROM chrome_login_data
  27. ${whereClause}
  28. ORDER BY create_time DESC
  29. LIMIT ? OFFSET ?
  30. `;
  31. const countSql = `SELECT COUNT(*) as total FROM chrome_login_data ${whereClause}`;
  32. sqlParams.push(parseInt(pageSize), offset);
  33. const [data, countResult] = await Promise.all([
  34. executeQuery(sql, sqlParams),
  35. executeQuery(countSql, countParams),
  36. ]);
  37. res.json({
  38. data,
  39. total: countResult[0]?.total || 0,
  40. page: parseInt(page),
  41. pageSize: parseInt(pageSize),
  42. });
  43. } catch (error) {
  44. console.error("Error fetching accounts:", error);
  45. res.status(500).json({ error: "Internal server error" });
  46. }
  47. });
  48. // 详情
  49. router.get("/:id", async (req, res) => {
  50. try {
  51. const { id } = req.params;
  52. const sql = `
  53. SELECT id, hostname, account, info, status, create_time, update_time
  54. FROM chrome_login_data
  55. WHERE id = ?
  56. `;
  57. const rows = await executeQuery(sql, [id]);
  58. if (!rows || rows.length === 0) {
  59. return res.status(404).json({ error: "Account not found" });
  60. }
  61. res.json(rows[0]);
  62. } catch (error) {
  63. console.error("Error fetching account detail:", error);
  64. res.status(500).json({ error: "Internal server error" });
  65. }
  66. });
  67. // 编辑更新
  68. router.put("/:id", async (req, res) => {
  69. try {
  70. const { id } = req.params;
  71. const { hostname, account, info, status } = req.body;
  72. const sql = `
  73. UPDATE chrome_login_data
  74. SET hostname = ?, account = ?, info = ?, status = ?, update_time = NOW()
  75. WHERE id = ?
  76. `;
  77. const params = [
  78. hostname ?? null,
  79. account ?? null,
  80. info ?? null,
  81. status ?? null,
  82. id,
  83. ];
  84. await executeQuery(sql, params);
  85. res.json({ message: "Account updated successfully" });
  86. } catch (error) {
  87. console.error("Error updating account:", error);
  88. res.status(500).json({ error: "Internal server error" });
  89. }
  90. });
  91. module.exports = router;