添加通知服务

This commit is contained in:
whyour
2021-09-16 18:01:25 +08:00
parent c51621ee04
commit 27226e7222
5 changed files with 171 additions and 8 deletions
+27 -5
View File
@@ -8,7 +8,8 @@ import jwt from 'jsonwebtoken';
import { authenticator } from '@otplib/preset-default';
import { exec } from 'child_process';
import DataStore from 'nedb';
import { AuthInfo, LoginStatus } from '../data/auth';
import { AuthDataType, AuthInfo, LoginStatus } from '../data/auth';
import { NotificationInfo } from '../data/notify';
@Service()
export default class AuthService {
@@ -100,7 +101,7 @@ export default class AuthService {
);
await this.getLoginLog();
await this.insertDb({
type: 'loginLog',
type: AuthDataType.loginLog,
info: { timestamp, address, ip, status: LoginStatus.success },
});
return {
@@ -121,7 +122,7 @@ export default class AuthService {
);
await this.getLoginLog();
await this.insertDb({
type: 'loginLog',
type: AuthDataType.loginLog,
info: { timestamp, address, ip, status: LoginStatus.fail },
});
return { code: 400, message: config.authError };
@@ -133,9 +134,9 @@ export default class AuthService {
public async getLoginLog(): Promise<AuthInfo[]> {
return new Promise((resolve) => {
this.authDb.find({ type: 'loginLog' }).exec((err, docs) => {
this.authDb.find({ type: AuthDataType.loginLog }).exec((err, docs) => {
if (err || docs.length === 0) {
resolve(docs);
resolve([]);
} else {
const result = docs.sort(
(a, b) => b.info.timestamp - a.info.timestamp,
@@ -247,4 +248,25 @@ export default class AuthService {
JSON.stringify({ ...authInfo, ...info }),
);
}
public async getNotificationMode(): Promise<NotificationInfo> {
return new Promise((resolve) => {
this.authDb
.find({ type: AuthDataType.notification })
.exec((err, docs) => {
if (err || docs.length === 0) {
resolve({} as NotificationInfo);
} else {
resolve(docs[0].info);
}
});
});
}
public async updateNotificationMode(notificationInfo: NotificationInfo) {
return await this.insertDb({
type: AuthDataType.notification,
info: { notificationInfo },
});
}
}
+51
View File
@@ -0,0 +1,51 @@
import { Service, Inject } from 'typedi';
import winston from 'winston';
import AuthService from './auth';
@Service()
export default class NotifyService {
private modeMap = new Map([
['goCqHttpBot', this.goCqHttpBot],
['serverChan', this.serverChan],
['bark', this.bark],
['telegramBot', this.telegramBot],
['dingtalkBot', this.dingtalkBot],
['weWorkBot', this.weWorkBot],
['weWorkApp', this.weWorkApp],
['iGot', this.iGot],
['pushPlus', this.pushPlus],
['email', this.email],
]);
constructor(
@Inject('logger') private logger: winston.Logger,
private authService: AuthService,
) {}
private async notify() {
const { type } = await this.authService.getNotificationMode();
if (type) {
const notificationModeAction = this.modeMap.get(type);
notificationModeAction?.call(this);
}
}
private async goCqHttpBot() {}
private async serverChan() {}
private async bark() {}
private async telegramBot() {}
private async dingtalkBot() {}
private async weWorkBot() {}
private async weWorkApp() {}
private async iGot() {}
private async pushPlus() {}
private async email() {}
}