routes.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import express from 'express';
  2. const router = express.Router();
  3. // GET /api/log — 获取操作日志
  4. router.get('/', async (_req, res) => {
  5. try {
  6. const Parse = (globalThis as any).Parse;
  7. const q = new Parse.Query('OperationLog');
  8. q.descending('createdAt');
  9. q.limit(500);
  10. const list = await q.find({ useMasterKey: true });
  11. const data = list.map((l: any) => ({
  12. id: l.id,
  13. action: l.get('action'),
  14. objectType: l.get('objectType'),
  15. objectId: l.get('objectId'),
  16. operatorId: l.get('operatorId'),
  17. operatorName: l.get('operatorName'),
  18. details: l.get('details'),
  19. ip: l.get('ip'),
  20. status: l.get('status'),
  21. timestamp: l.get('createdAt'),
  22. }));
  23. res.json(data);
  24. } catch (e: any) {
  25. res.status(500).json({ error: e.message });
  26. }
  27. });
  28. // POST /api/log — 添加操作日志
  29. router.post('/', async (req, res) => {
  30. try {
  31. const Parse = (globalThis as any).Parse;
  32. const entry = req.body;
  33. const OperationLog = Parse.Object.extend('OperationLog');
  34. const obj = new OperationLog();
  35. obj.set('action', entry.action || '');
  36. obj.set('objectType', entry.objectType || '');
  37. obj.set('objectId', entry.objectId || '');
  38. obj.set('operatorId', entry.operatorId || '');
  39. obj.set('operatorName', entry.operatorName || '');
  40. obj.set('details', entry.details || '');
  41. obj.set('ip', entry.ip || '');
  42. obj.set('status', entry.status || 'success');
  43. const saved = await obj.save(null, { useMasterKey: true });
  44. res.json({ ok: true, id: saved.id || '' });
  45. } catch (e: any) {
  46. res.status(500).json({ error: e.message });
  47. }
  48. });
  49. export default router;