system.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. // 获取系统状态(需要管理员权限)
  33. router.get(
  34. '/status',
  35. authenticate,
  36. authorize('admin'),
  37. asyncHandler(async (_req, res) => {
  38. const status = await systemService.getSystemStatus();
  39. res.json({ success: true, data: status });
  40. })
  41. );
  42. export default router;