Skip to content

Commit 5f220dd

Browse files
committed
feat: 单独抽取vo层
1 parent 380035d commit 5f220dd

File tree

15 files changed

+63
-62
lines changed

15 files changed

+63
-62
lines changed

src/modules/admin/system/access/controllers/access/vo/access.vo.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { QueryVo } from '@src/dto/query.vo';
2-
import { QueryListVo } from '@src/dto/query.list.vo';
1+
import { QueryVo } from '@src/vo/query.vo';
2+
import { QueryListVo } from '@src/vo/query.list.vo';
33
import { ApiProperty } from '@nestjs/swagger';
44

55
export class AccessVo extends QueryVo {

src/modules/admin/system/access/controllers/menus/vo/menus.vo.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { QueryVo } from '@src/dto/query.vo';
1+
import { QueryVo } from '@src/vo/query.vo';
22
import { ApiProperty } from '@nestjs/swagger';
33

44
export class MenusListVo extends QueryVo {

src/modules/admin/system/account/controllers/account/vo/account.vo.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ApiProperty } from '@nestjs/swagger';
2-
import { QueryListVo } from '@src/dto/query.list.vo';
3-
import { QueryVo } from '@src/dto/query.vo';
2+
import { QueryListVo } from '@src/vo/query.list.vo';
3+
import { QueryVo } from '@src/vo/query.vo';
44

55
export class AccountVo extends QueryVo {
66
@ApiProperty({ description: '用户名' })

src/modules/admin/system/account/controllers/login/vo/login.vo.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ApiProperty } from '@nestjs/swagger';
2-
import { QueryVo } from '@src/dto/query.vo';
2+
import { QueryVo } from '@src/vo/query.vo';
33

44
export class LoginVo extends QueryVo {
55
@ApiProperty({ description: '账号绑定的手机号码' })

src/modules/admin/system/account/services/account-role/account-role.service.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RoleEntity } from './../../../role/entities/role.entity';
2-
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
2+
import { Injectable, HttpException, HttpStatus, Logger } from '@nestjs/common';
33
import { InjectRepository } from '@nestjs/typeorm';
44
import { AccountRoleEntity } from '../../entities/account.role.entity';
55
import { Repository, getManager, EntityManager, getConnection } from 'typeorm';
@@ -11,6 +11,7 @@ import { DistributionRoleDto } from '../../controllers/account-role/dto/distribu
1111

1212
@Injectable()
1313
export class AccountRoleService {
14+
private readonly logger: Logger = new Logger(AccountRoleService.name);
1415
constructor(
1516
@InjectRepository(AccountRoleEntity)
1617
private readonly accountRoleRepository: Repository<AccountRoleEntity>,
@@ -45,18 +46,22 @@ export class AccountRoleService {
4546
.transaction(async (entityManager: EntityManager) => {
4647
await entityManager.delete<AccountRoleEntity>(AccountRoleEntity, { accountId });
4748
for (const item of roleList) {
48-
const result = entityManager.create<AccountRoleEntity>(AccountRoleEntity, {
49-
accountId,
50-
roleId: item,
51-
});
49+
const result: AccountRoleEntity = entityManager.create<AccountRoleEntity>(
50+
AccountRoleEntity,
51+
{
52+
accountId,
53+
roleId: item,
54+
},
55+
);
5256
await entityManager.save(result);
5357
}
5458
})
5559
.then(() => {
5660
return '分配角色成功';
5761
})
5862
.catch((e: HttpException) => {
59-
throw new HttpException(`给账号分配角色失败:${e}`, HttpStatus.OK);
63+
this.logger.error('给账号分配角色错误', e.message);
64+
throw new HttpException(`给账号分配角色失败:${e.message}`, HttpStatus.OK);
6065
});
6166
}
6267

src/modules/admin/system/account/services/account/account.service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export class AccountService {
165165
throw new HttpException('系统默认生成的账号不能修改信息', HttpStatus.OK);
166166
}
167167
const { username, email, mobile, status, platform } = updateAccountDto;
168-
const result = await this.accountRepository.findOne(id);
168+
const result: AccountEntity | undefined = await this.accountRepository.findOne(id);
169169
await this.accountRepository.save(
170170
Object.assign(result, { username, email, mobile, status, platform }),
171171
);
@@ -269,7 +269,7 @@ export class AccountService {
269269
.where(mapToObj(query))
270270
.getCount();
271271
// 处理当前手机号码或者邮箱不合法的时候
272-
const formatData = data.map((item) => {
272+
const formatData: AccountVo[] = data.map((item) => {
273273
const { username, mobile, email } = item;
274274
return {
275275
...item,

src/modules/admin/system/account/services/login/login.service.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
1+
import { Injectable, HttpException, HttpStatus, Logger } from '@nestjs/common';
22
import { LoginDto } from '../../controllers/login/dto/login.dto';
33
import { AccountEntity } from '../../entities/account.entity';
44
import { InjectRepository } from '@nestjs/typeorm';
@@ -10,6 +10,7 @@ import { LoginVo } from '../../controllers/login/vo/login.vo';
1010

1111
@Injectable()
1212
export class LoginService {
13+
private logger: Logger = new Logger(LoginService.name);
1314
constructor(
1415
@InjectRepository(AccountEntity)
1516
private readonly accountRepository: Repository<AccountEntity>,
@@ -33,50 +34,46 @@ export class LoginService {
3334
let sqlPassword: string | undefined;
3435
let findAccount: AccountEntity | undefined;
3536
if (isMobilePhone(username, 'zh-CN')) {
36-
const findResult: AccountEntity | undefined = await getConnection()
37+
const findResult: Pick<AccountEntity, 'password'> | undefined = await getConnection()
3738
.createQueryBuilder(AccountEntity, 'account')
3839
.select([])
3940
.addSelect('account.password', 'password')
4041
.where('(account.mobile = :mobile)', { mobile: username })
4142
.getRawOne();
42-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
4343
sqlPassword = findResult?.password;
4444
findAccount = await this.accountRepository.findOne({ where: { mobile: username } });
4545
} else if (isEmail(username)) {
46-
const findResult: AccountEntity | undefined = await getConnection()
46+
const findResult: Pick<AccountEntity, 'password'> | undefined = await getConnection()
4747
.createQueryBuilder(AccountEntity, 'account')
4848
.select([])
4949
.addSelect('account.password', 'password')
5050
.where('(account.email = :email)', { email: username })
5151
.getRawOne();
52-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
5352
sqlPassword = findResult?.password;
5453
findAccount = await this.accountRepository.findOne({ where: { email: username } });
5554
} else {
56-
const findResult: AccountEntity | undefined = await getConnection()
55+
const findResult: Pick<AccountEntity, 'password'> | undefined = await getConnection()
5756
.createQueryBuilder(AccountEntity, 'account')
5857
.select([])
5958
.addSelect('account.password', 'password')
6059
.where('(account.username = :username)', { username })
6160
.getRawOne();
62-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
6361
sqlPassword = findResult?.password;
6462
findAccount = await this.accountRepository.findOne({ where: { username } });
6563
}
6664
if (sqlPassword && this.toolsService.checkPassword(password, sqlPassword) && findAccount) {
6765
const lastLogin = this.accountLastLoginRepository.create({
68-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
69-
accountId: findAccount!.id,
66+
accountId: findAccount.id,
7067
lastLoginIp: ipAddress,
7168
});
7269
await this.accountLastLoginRepository.save(lastLogin);
73-
console.log(findAccount, '当前用户');
70+
this.logger.log('当前用户', findAccount);
7471
return Object.assign(findAccount, { token: this.toolsService.generateToken(findAccount) });
7572
} else {
7673
throw new HttpException('用户名或密码错误', HttpStatus.OK);
7774
}
7875
} catch (e) {
79-
console.log(e, '?');
76+
this.logger.error('用户名或密码错误', e);
8077
throw new HttpException('用户名或密码错误', HttpStatus.OK);
8178
}
8279
}

src/modules/admin/system/role/controllers/role/vo/role.vo.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ApiProperty } from '@nestjs/swagger';
2-
import { QueryVo } from '@src/dto/query.vo';
3-
import { QueryListVo } from '@src/dto/query.list.vo';
2+
import { QueryListVo } from '@src/vo/query.list.vo';
3+
import { QueryVo } from '@src/vo/query.vo';
44

55
export class RoleVo extends QueryVo {
66
@ApiProperty({ description: '角色名称' })

src/modules/admin/system/role/services/role-access/role-access.service.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,22 @@ export class RoleAccessService {
5858
* @return {*}
5959
*/
6060
async allMenus(): Promise<AllMenusVo[]> {
61-
const menusList = await this.accessRepository.find({
62-
where: [{ type: AccessTypeEnum.MODULE }, { type: AccessTypeEnum.MENUS }],
63-
select: ['id', 'moduleName', 'actionName', 'parentId'],
64-
order: { sort: 'ASC', createdAt: 'DESC' },
65-
});
66-
return menusList.map((item: AccessEntity) => {
67-
return {
68-
id: item.id,
69-
key: String(item.id),
70-
title: item.moduleName ? item.moduleName : item.actionName,
71-
parentId: item.parentId,
72-
};
73-
});
61+
const menusList: Pick<AccessEntity, 'id' | 'moduleName' | 'actionName' | 'parentId'>[] =
62+
await this.accessRepository.find({
63+
where: [{ type: AccessTypeEnum.MODULE }, { type: AccessTypeEnum.MENUS }],
64+
select: ['id', 'moduleName', 'actionName', 'parentId'],
65+
order: { sort: 'ASC', createdAt: 'DESC' },
66+
});
67+
return menusList.map(
68+
(item: Pick<AccessEntity, 'id' | 'moduleName' | 'actionName' | 'parentId'>) => {
69+
return {
70+
id: item.id,
71+
key: String(item.id),
72+
title: item.moduleName ? item.moduleName : item.actionName,
73+
parentId: item.parentId,
74+
};
75+
},
76+
);
7477
}
7578

7679
/**

src/modules/admin/system/role/services/role/role.service.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
22
import { InjectRepository } from '@nestjs/typeorm';
33
import { RoleEntity } from '../../entities/role.entity';
4-
import { Repository, getConnection } from 'typeorm';
4+
import { Repository, getConnection, ILike, Equal } from 'typeorm';
55
import { CreateRoleDto } from '../../controllers/role/dto/create.role.dto';
66
import { UpdateRoleDto } from '../../controllers/role/dto/update.role.dto';
77
import { RoleListVo, RoleVo } from '../../controllers/role/vo/role.vo';
88
import { RoleReqDto } from '../../controllers/role/dto/role.req.dto';
99
import { PageEnum, StatusEnum } from '@src/enums';
1010
import { RoleEnum } from '@src/enums/role.enum';
1111
import { AccountRoleEntity } from '../../../account/entities/account.role.entity';
12+
import { mapToObj } from '@src/utils';
1213

1314
@Injectable()
1415
export class RoleService {
@@ -29,21 +30,24 @@ export class RoleService {
2930
*/
3031
async createRole(createRoleDto: CreateRoleDto): Promise<string> {
3132
const { name, isDefault } = createRoleDto;
32-
const findNameResult = await this.roleRepository.findOne({ where: { name }, select: ['id'] });
33+
const findNameResult: Pick<RoleEntity, 'id'> | undefined = await this.roleRepository.findOne({
34+
where: { name },
35+
select: ['id'],
36+
});
3337
if (findNameResult) {
3438
throw new HttpException(`${name}当前角色已经存在,不能重复创建`, HttpStatus.OK);
3539
}
3640
// 如果是默认角色的时候要判断下
3741
if (Object.is(isDefault, RoleEnum.DEFAULT)) {
38-
const findDefault = await this.roleRepository.findOne({
42+
const findDefault: Pick<RoleEntity, 'id'> | undefined = await this.roleRepository.findOne({
3943
where: { isDefault },
4044
select: ['id'],
4145
});
4246
if (findDefault) {
4347
throw new HttpException('已经存在默认角色不能重复创建', HttpStatus.OK);
4448
}
4549
}
46-
const role = this.roleRepository.create(createRoleDto);
50+
const role: RoleEntity = this.roleRepository.create(createRoleDto);
4751
await this.roleRepository.save(role);
4852
return '创建角色成功';
4953
}
@@ -58,7 +62,7 @@ export class RoleService {
5862
*/
5963
async destroyRoleById(id: number): Promise<string> {
6064
// 判断当前角色是否已经被占用(有账号绑定了该角色)
61-
const accountRoleFindResult: AccountRoleEntity | undefined =
65+
const accountRoleFindResult: Pick<AccountRoleEntity, 'id'> | undefined =
6266
await this.accountRoleRepository.findOne({
6367
where: { roleId: id },
6468
select: ['id'],
@@ -133,21 +137,16 @@ export class RoleService {
133137
name,
134138
status,
135139
} = roleReqDto;
136-
const queryConditionList = [];
140+
const query = new Map();
137141
if (name) {
138-
queryConditionList.push('role.name LIKE :name');
142+
query.set('name', ILike(name));
139143
}
140-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
141-
if (
142-
/^\d$/.test(String(status)) &&
143-
[StatusEnum.NORMAL, StatusEnum.FORBIDDEN].includes(Number(status))
144-
) {
145-
queryConditionList.push('role.status = :status');
144+
if ([StatusEnum.NORMAL, StatusEnum.FORBIDDEN].includes(Number(status))) {
145+
query.set('status', Equal(status));
146146
}
147-
const queryCondition = queryConditionList.join(' AND ');
148147
const [data, total] = await getConnection()
149148
.createQueryBuilder(RoleEntity, 'role')
150-
.where(queryCondition, { name: `%${name}%`, status })
149+
.where(mapToObj(query))
151150
.skip((pageNumber - 1) * pageSize)
152151
.take(pageSize)
153152
.printSql()

src/modules/shared/services/init-db/init-db.service.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { InjectRepository } from '@nestjs/typeorm';
44
import { Repository } from 'typeorm';
55
import { AccessEntity } from '@src/modules/admin/system/access/entities/access.entity';
66
import adminConfig from '@src/config/admin.config';
7-
import { ObjectType } from '@src/types/obj-type';
87

98
@Injectable()
109
export class InitDbService {
@@ -30,8 +29,8 @@ export class InitDbService {
3029
* @return {*}
3130
*/
3231
private async initAccount(): Promise<void> {
33-
const username = adminConfig.defaultAccount;
34-
const password = adminConfig.defaultPassword;
32+
const username: string = adminConfig.defaultAccount;
33+
const password: string = adminConfig.defaultPassword;
3534
const isExist = await this.accountRepository.findOne({ where: { username } });
3635
if (!isExist) {
3736
const account = this.accountRepository.create({ username, password, isSuper: 1 });
@@ -48,7 +47,7 @@ export class InitDbService {
4847
* @return {*}
4948
*/
5049
private async initAccess(): Promise<void> {
51-
const accessList: ObjectType[] = [
50+
const accessList: Record<string, number | string>[] = [
5251
{
5352
moduleName: '系统管理',
5453
parentId: 0,

src/types/obj-type.ts

-1
This file was deleted.

src/utils/map.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { ObjectType } from '@src/types/obj-type';
2-
1+
type ObjectType = Record<string, number | string | boolean>;
32
/**
43
* @Author: 水痕
54
* @Date: 2021-03-26 14:35:21
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)