Spaces:
Sleeping
Sleeping
| import db from './db.js'; | |
| import crypto from 'crypto'; | |
| export class SystemService { | |
| /** | |
| * 记录审计日志 | |
| */ | |
| static logAudit(userId: string | null, action: string, status: string, payload: any = {}, ip: string = '') { | |
| try { | |
| db.prepare(` | |
| INSERT INTO audit_logs (user_id, action, status, payload, ip) | |
| VALUES (?, ?, ?, ?, ?) | |
| `).run(userId, action, status, JSON.stringify(payload), ip); | |
| } catch (err) { | |
| console.error('[AuditLog] 写入失败:', err); | |
| } | |
| } | |
| /** | |
| * 发送系统通知 | |
| */ | |
| static async sendNotification(userId: string, title: string, content: string, type: string = 'info') { | |
| const id = `NOTI_${crypto.randomBytes(4).toString('hex').toUpperCase()}`; | |
| try { | |
| db.prepare(` | |
| INSERT INTO notifications (id, user_id, title, content, type) | |
| VALUES (?, ?, ?, ?, ?) | |
| `).run(id, userId, title, content, type); | |
| console.log(`[Notification] 已发送至用户 ${userId}: ${title}`); | |
| } catch (err) { | |
| console.error('[Notification] 发送失败:', err); | |
| } | |
| } | |
| /** | |
| * 获取最近审计日志 (用于监控面板) | |
| */ | |
| static getRecentLogs(limit = 20) { | |
| return db.prepare('SELECT * FROM audit_logs ORDER BY created_at DESC LIMIT ?').all(limit); | |
| } | |
| } | |