build: trident-sync prepare

This commit is contained in:
xiaojunnuo
2023-01-29 13:44:19 +08:00
parent dcd1023a39
commit 07a45b4530
589 changed files with 36886 additions and 2 deletions
@@ -0,0 +1,52 @@
import { Config, Inject, Provide } from '@midwayjs/decorator';
import { UserService } from '../../authority/service/user-service';
import * as jwt from 'jsonwebtoken';
import { CommonException } from '../../../basic/exception/common-exception';
/**
* 系统用户
*/
@Provide()
export class LoginService {
@Inject()
userService: UserService;
@Config('biz.jwt')
private jwt: any;
/**
* login
*/
async login(user) {
console.assert(user.username != null, '用户名不能为空');
const info = await this.userService.findOne({ username: user.username });
if (info == null) {
throw new CommonException('用户名或密码错误');
}
const right = this.userService.checkPassword(user.password, info.password);
if (!right) {
throw new CommonException('用户名或密码错误');
}
return this.generateToken(info);
}
/**
* 生成token
* @param user 用户对象
*/
async generateToken(user) {
const tokenInfo = {
username: user.username,
id: user.id,
};
const expire = this.jwt.expire;
const token = jwt.sign(tokenInfo, this.jwt.secret, {
expiresIn: expire,
});
return {
token,
expire,
};
}
}