Spaces:
Sleeping
Sleeping
File size: 1,277 Bytes
ae4ceef | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 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);
}
}
|