添加通知服务

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

View File

@ -1,6 +1,6 @@
export class AuthInfo {
ip?: string;
type: AuthInfoType;
type: AuthDataType;
info?: any;
_id?: string;
@ -17,4 +17,8 @@ export enum LoginStatus {
'fail',
}
export type AuthInfoType = 'loginLog' | 'authToken';
export enum AuthDataType {
'loginLog' = 'loginLog',
'authToken' = 'authToken',
'notification' = 'notification',
}

80
back/data/notify.ts Normal file
View File

@ -0,0 +1,80 @@
export enum NotificationMode {
'goCqHttpBot' = 'goCqHttpBot',
'serverChan' = 'serverChan',
'bark' = 'bark',
'telegramBot' = 'telegramBot',
'dingtalkBot' = 'dingtalkBot',
'weWorkBot' = 'weWorkBot',
'weWorkApp' = 'weWorkApp',
'iGot' = 'iGot',
'pushPlus' = 'pushPlus',
'email' = 'email',
}
abstract class NotificationBaseInfo {
public type!: NotificationMode;
}
export class GoCqHttpBotNotification extends NotificationBaseInfo {
public GOBOT_URL = '';
public GOBOT_TOKEN = '';
public GOBOT_QQ = '';
}
export class ServerChanNotification extends NotificationBaseInfo {
public SCKEY = '';
}
export class BarkNotification extends NotificationBaseInfo {
public BARK_PUSH = '';
public BARK_SOUND = '';
public BARK_GROUP = 'qinglong';
}
export class TelegramBotNotification extends NotificationBaseInfo {
public TG_BOT_TOKEN = '';
public TG_USER_ID = '';
public TG_PROXY_HOST = '';
public TG_PROXY_PORT = '';
public TG_PROXY_AUTH = '';
public TG_API_HOST = 'api.telegram.org';
}
export class DingtalkBotNotification extends NotificationBaseInfo {
public DD_BOT_TOKEN = '';
public DD_BOT_SECRET = '';
}
export class WeWorkBotNotification extends NotificationBaseInfo {
public QYWX_KEY = '';
}
export class WeWorkAppNotification extends NotificationBaseInfo {
public QYWX_AM = '';
}
export class IGotNotification extends NotificationBaseInfo {
public IGOT_PUSH_KEY = '';
}
export class PushPlusNotification extends NotificationBaseInfo {
public PUSH_PLUS_TOKEN = '';
public PUSH_PLUS_USER = '';
}
export class EmailNotification extends NotificationBaseInfo {
public service: string = '';
public user: string = '';
public pass: string = '';
}
export type NotificationInfo =
| GoCqHttpBotNotification
| ServerChanNotification
| BarkNotification
| TelegramBotNotification
| DingtalkBotNotification
| WeWorkBotNotification
| IGotNotification
| PushPlusNotification
| EmailNotification;

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
back/services/notify.ts Normal file
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() {}
}

View File

@ -27,7 +27,13 @@
"allowJs": true,
"noEmit": false
},
"include": ["src/**/*", "config/**/*", ".umirc.ts", "typings.d.ts"],
"include": [
"src/**/*",
"config/**/*",
".umirc.ts",
"typings.d.ts",
"back/**/*"
],
"exclude": [
"node_modules",
"lib",