1.实现前端登录功能 但是同时可能还得判断当前的账号有没有权限登录 这样的逻辑 代码 不能让代码特别冗余 也不能有太多查询表操作 浪费资源
2.实现
①contrller
@Post('/h5Login', { summary: 'H5登录' })
async LoginByH5(@Body() body) {
const res: any = await this.businessLoginService.H5Login(body);
return this.ok(res);
}
② service
// H5 登录
async H5Login(login) {
if (!login.password || !login.phone) {
throw new CoolCommException('账户或者密码不能为空~');
}
const user = await this.businessStudentEntity.findOneBy({
phone: login?.phone,
password: md5(login?.password),
});
if (!user) {
throw new CoolCommException('账户或密码不正确~');
}
if (user.isEnabled == 0) {
throw new CoolCommException('账户没有权限~');
}
const { expire, refreshExpire } = this.coolConfig.jwt.token;
const result = {
expire,
token: await this.generateTokenClient(user, expire),
refreshExpire,
refreshToken: await this.generateTokenClient(user, refreshExpire, true),
};
await this.cacheManager.set(
`business:client:token:${user.id}`,
result.token,
{ ttl: expire }
);
await this.cacheManager.set(
`business:client:token:refresh:${user.id}`,
result.refreshToken,
{ ttl: refreshExpire }
);
return result;
}
3.说明
拿我这个代码来说,我这个时node 的操作 跟java中的接口写法差不多 但是代码和格式有根本的区别 这样的service 判断比较简洁 只需要查询一次数据表 就可以