system.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { Router } from 'express';
  2. import { body } from 'express-validator';
  3. import { SystemService } from '../services/SystemService.js';
  4. import { authenticate, authorize } from '../middleware/auth.js';
  5. import { asyncHandler } from '../middleware/error.js';
  6. import { validateRequest } from '../middleware/validate.js';
  7. import type { Request, Response, NextFunction } from 'express';
  8. const router = Router();
  9. const systemService = new SystemService();
  10. function isLoopbackRequest(req: Request): boolean {
  11. const ip = req.ip || '';
  12. return ip === '127.0.0.1'
  13. || ip === '::1'
  14. || ip.startsWith('::ffff:127.');
  15. }
  16. // 本地回环请求放行,否则需任一已登录用户(不要求管理员)
  17. function requireAuthOrLoopback(req: Request, res: Response, next: NextFunction): void {
  18. if (isLoopbackRequest(req)) {
  19. next();
  20. return;
  21. }
  22. authenticate(req, res, next);
  23. }
  24. // 获取系统配置(公开)
  25. router.get(
  26. '/config',
  27. asyncHandler(async (_req, res) => {
  28. const config = await systemService.getPublicConfig();
  29. res.json({ success: true, data: config });
  30. })
  31. );
  32. // 更新系统配置(需要管理员权限)
  33. router.put(
  34. '/config',
  35. authenticate,
  36. authorize('admin'),
  37. [
  38. body('allowRegistration').optional().isBoolean(),
  39. body('defaultUserRole').optional().isIn(['admin', 'editor', 'operator']),
  40. validateRequest,
  41. ],
  42. asyncHandler(async (req, res) => {
  43. await systemService.updateConfig(req.body);
  44. res.json({ success: true, message: '配置已更新' });
  45. })
  46. );
  47. router.get(
  48. '/publish-proxy',
  49. authenticate,
  50. authorize('admin'),
  51. asyncHandler(async (_req, res) => {
  52. const config = await systemService.getPublishProxyAdminConfig();
  53. res.json({ success: true, data: config });
  54. })
  55. );
  56. router.get(
  57. '/publish-proxy/cities',
  58. authenticate,
  59. asyncHandler(async (_req, res) => {
  60. const cities = await systemService.getPublishProxyCitiesFromApi();
  61. res.json({ success: true, data: { cities } });
  62. })
  63. );
  64. router.get(
  65. '/publish-proxy/regions',
  66. authenticate,
  67. asyncHandler(async (_req, res) => {
  68. const regions = await systemService.getPublishProxyRegionsFromCsv();
  69. res.json({ success: true, data: { regions } });
  70. })
  71. );
  72. router.put(
  73. '/publish-proxy',
  74. authenticate,
  75. authorize('admin'),
  76. [
  77. body('productKey').optional().isString(),
  78. body('signature').optional().isString(),
  79. validateRequest,
  80. ],
  81. asyncHandler(async (req, res) => {
  82. await systemService.updatePublishProxyAdminConfig(req.body);
  83. res.json({ success: true, message: '发布代理配置已更新' });
  84. })
  85. );
  86. router.get(
  87. '/python-service',
  88. requireAuthOrLoopback,
  89. asyncHandler(async (_req, res) => {
  90. const config = await systemService.getPythonServiceAdminConfig();
  91. res.json({ success: true, data: config });
  92. })
  93. );
  94. router.put(
  95. '/python-service',
  96. requireAuthOrLoopback,
  97. [
  98. body('url').optional().isString(),
  99. validateRequest,
  100. ],
  101. asyncHandler(async (req, res) => {
  102. await systemService.updatePythonServiceAdminConfig(req.body);
  103. res.json({ success: true, message: 'Python 服务配置已更新' });
  104. })
  105. );
  106. router.post(
  107. '/python-service/check',
  108. requireAuthOrLoopback,
  109. [
  110. body('url').optional().isString(),
  111. validateRequest,
  112. ],
  113. asyncHandler(async (req, res) => {
  114. const result = await systemService.checkPythonService(req.body.url);
  115. res.json({ success: true, data: result });
  116. })
  117. );
  118. // 获取系统状态(需要管理员权限)
  119. router.get(
  120. '/status',
  121. authenticate,
  122. authorize('admin'),
  123. asyncHandler(async (_req, res) => {
  124. const status = await systemService.getSystemStatus();
  125. res.json({ success: true, data: status });
  126. })
  127. );
  128. export default router;