mirror of
https://github.com/whyour/qinglong.git
synced 2026-07-01 04:40:38 +08:00
更新通知服务
This commit is contained in:
+14
-8
@@ -10,12 +10,16 @@ import { exec } from 'child_process';
|
||||
import DataStore from 'nedb';
|
||||
import { AuthDataType, AuthInfo, LoginStatus } from '../data/auth';
|
||||
import { NotificationInfo } from '../data/notify';
|
||||
import NotificationService from './notify';
|
||||
|
||||
@Service()
|
||||
export default class AuthService {
|
||||
private authDb = new DataStore({ filename: config.authDbFile });
|
||||
|
||||
constructor(@Inject('logger') private logger: winston.Logger) {
|
||||
constructor(
|
||||
@Inject('logger') private logger: winston.Logger,
|
||||
private notificationService: NotificationService,
|
||||
) {
|
||||
this.authDb.loadDatabase((err) => {
|
||||
if (err) throw err;
|
||||
});
|
||||
@@ -48,7 +52,7 @@ export default class AuthService {
|
||||
} = content;
|
||||
|
||||
if (
|
||||
(cUsername === 'admin' && cPassword === 'adminadmin') ||
|
||||
(cUsername === 'admin' && cPassword === 'admin') ||
|
||||
!cUsername ||
|
||||
!cPassword
|
||||
) {
|
||||
@@ -94,10 +98,11 @@ export default class AuthService {
|
||||
lastaddr: address,
|
||||
isTwoFactorChecking: false,
|
||||
});
|
||||
exec(
|
||||
`notify "登陆通知" "你于${new Date(
|
||||
await this.notificationService.notify(
|
||||
'登陆通知',
|
||||
`你于${new Date(
|
||||
timestamp,
|
||||
).toLocaleString()}在${address}登陆成功,ip地址${ip}"`,
|
||||
).toLocaleString()}在 ${address} 登陆成功,ip地址 ${ip}"`,
|
||||
);
|
||||
await this.getLoginLog();
|
||||
await this.insertDb({
|
||||
@@ -115,10 +120,11 @@ export default class AuthService {
|
||||
lastip: ip,
|
||||
lastaddr: address,
|
||||
});
|
||||
exec(
|
||||
`notify "登陆通知" "你于${new Date(
|
||||
await this.notificationService.notify(
|
||||
'登陆通知',
|
||||
`你于${new Date(
|
||||
timestamp,
|
||||
).toLocaleString()}在${address}登陆失败,ip地址${ip}"`,
|
||||
).toLocaleString()}在 ${address} 登陆失败,ip地址 ${ip}"`,
|
||||
);
|
||||
await this.getLoginLog();
|
||||
await this.insertDb({
|
||||
|
||||
+11
-3
@@ -1,9 +1,10 @@
|
||||
import { NotificationInfo } from '../data/notify';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import winston from 'winston';
|
||||
import AuthService from './auth';
|
||||
|
||||
@Service()
|
||||
export default class NotifyService {
|
||||
export default class NotificationService {
|
||||
private modeMap = new Map([
|
||||
['goCqHttpBot', this.goCqHttpBot],
|
||||
['serverChan', this.serverChan],
|
||||
@@ -17,14 +18,21 @@ export default class NotifyService {
|
||||
['email', this.email],
|
||||
]);
|
||||
|
||||
private title = '';
|
||||
private content = '';
|
||||
private params!: Omit<NotificationInfo, 'type'>;
|
||||
|
||||
constructor(
|
||||
@Inject('logger') private logger: winston.Logger,
|
||||
private authService: AuthService,
|
||||
) {}
|
||||
|
||||
private async notify() {
|
||||
const { type } = await this.authService.getNotificationMode();
|
||||
public async notify(title: string, content: string) {
|
||||
const { type, ...rest } = await this.authService.getNotificationMode();
|
||||
if (type) {
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
this.params = rest;
|
||||
const notificationModeAction = this.modeMap.get(type);
|
||||
notificationModeAction?.call(this);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user