更新通知服务

This commit is contained in:
whyour 2021-09-16 21:01:29 +08:00
parent 27226e7222
commit 9534cda1f9
5 changed files with 35 additions and 20 deletions

View File

@ -11,7 +11,7 @@ export default defineConfig({
favicon: '/images/g5.ico',
proxy: {
'/api': {
target: 'http://127.0.0.1:5678/',
target: 'http://127.0.0.1:5600/',
changeOrigin: true,
},
},

View File

@ -47,13 +47,6 @@ export default ({ app }: { app: Application }) => {
}
}
const data = fs.readFileSync(config.authConfigFile, 'utf8');
if (data) {
const { token } = JSON.parse(data);
if (token && headerToken === token) {
return next();
}
}
if (
!headerToken &&
req.path &&
@ -71,6 +64,14 @@ export default ({ app }: { app: Application }) => {
return next();
}
const data = fs.readFileSync(config.authConfigFile, 'utf8');
if (data) {
const { token } = JSON.parse(data);
if (token && headerToken === token) {
return next();
}
}
const err: any = new Error('UnauthorizedError');
err.status = 401;
next(err);

View File

@ -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({

View File

@ -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);
}

View File

@ -1 +1 @@
{ "username": "admin", "password": "adminadmin" }
{ "username": "admin", "password": "admin" }