system.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. const router = Router();
  8. const systemService = new SystemService();
  9. // 获取系统配置(公开)
  10. router.get(
  11. '/config',
  12. asyncHandler(async (_req, res) => {
  13. const config = await systemService.getPublicConfig();
  14. res.json({ success: true, data: config });
  15. })
  16. );
  17. // 更新系统配置(需要管理员权限)
  18. router.put(
  19. '/config',
  20. authenticate,
  21. authorize('admin'),
  22. [
  23. body('allowRegistration').optional().isBoolean(),
  24. body('defaultUserRole').optional().isIn(['admin', 'editor', 'operator']),
  25. validateRequest,
  26. ],
  27. asyncHandler(async (req, res) => {
  28. await systemService.updateConfig(req.body);
  29. res.json({ success: true, message: '配置已更新' });
  30. })
  31. );
  32. router.get(
  33. '/publish-proxy',
  34. authenticate,
  35. authorize('admin'),
  36. asyncHandler(async (_req, res) => {
  37. const config = await systemService.getPublishProxyAdminConfig();
  38. res.json({ success: true, data: config });
  39. })
  40. );
  41. router.get(
  42. '/publish-proxy/cities',
  43. authenticate,
  44. asyncHandler(async (_req, res) => {
  45. const cities = await systemService.getPublishProxyCitiesFromApi();
  46. res.json({ success: true, data: { cities } });
  47. })
  48. );
  49. router.get(
  50. '/publish-proxy/regions',
  51. authenticate,
  52. asyncHandler(async (_req, res) => {
  53. const regions = await systemService.getPublishProxyRegionsFromCsv();
  54. res.json({ success: true, data: { regions } });
  55. })
  56. );
  57. router.put(
  58. '/publish-proxy',
  59. authenticate,
  60. authorize('admin'),
  61. [
  62. body('productKey').optional().isString(),
  63. validateRequest,
  64. ],
  65. asyncHandler(async (req, res) => {
  66. await systemService.updatePublishProxyAdminConfig(req.body);
  67. res.json({ success: true, message: '发布代理配置已更新' });
  68. })
  69. );
  70. // 获取系统状态(需要管理员权限)
  71. router.get(
  72. '/status',
  73. authenticate,
  74. authorize('admin'),
  75. asyncHandler(async (_req, res) => {
  76. const status = await systemService.getSystemStatus();
  77. res.json({ success: true, data: status });
  78. })
  79. );
  80. export default router;