Skip to content

Commit

Permalink
更新通知服务
Browse files Browse the repository at this point in the history
  • Loading branch information
whyour committed Sep 16, 2021
1 parent 27226e7 commit 9534cda
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand Down
15 changes: 8 additions & 7 deletions back/loaders/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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);
Expand Down
22 changes: 14 additions & 8 deletions back/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down Expand Up @@ -48,7 +52,7 @@ export default class AuthService {
} = content;

if (
(cUsername === 'admin' && cPassword === 'adminadmin') ||
(cUsername === 'admin' && cPassword === 'admin') ||
!cUsername ||
!cPassword
) {
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand Down
14 changes: 11 additions & 3 deletions back/services/notify.ts
Original file line number Diff line number Diff line change
@@ -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],
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion sample/auth.sample.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "username": "admin", "password": "adminadmin" }
{ "username": "admin", "password": "admin" }

0 comments on commit 9534cda

Please sign in to comment.