Skip to content

Commit 49a6b28

Browse files
committed
feat: 账号查询
1 parent 987bbee commit 49a6b28

File tree

5 files changed

+56
-12
lines changed

5 files changed

+56
-12
lines changed

src/api/account/account.controller.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { CurrentUser, ICurrentUserType } from '@src/decorators';
1717
import { Request } from 'express';
1818
import { AccountPageVo, AccountVo } from './vo/account.vo';
1919
import { QueryAccountDto } from './dto/account.query';
20+
import { AccountEntity } from './entities/account.entity';
2021

2122
@UseGuards(AuthGuard)
2223
@Controller('account')
@@ -64,6 +65,14 @@ export class AccountController {
6465
return await this.accountService.getAccountPageApi(queryOption, currentInfo);
6566
}
6667

68+
@Get('list')
69+
async getAccountListApi(
70+
@CurrentUser('userInfo') currentInfo: ICurrentUserType,
71+
@Query('status') status: number
72+
): Promise<Pick<AccountEntity, 'id' | 'username'>[]> {
73+
return await this.accountService.getAccountListApi(currentInfo, status);
74+
}
75+
6776
@Get(':id')
6877
async getAccountByIdApi(
6978
@Param('id', new ParseIntPipe()) id: number

src/api/account/account.service.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class AccountService {
3535
request: Request,
3636
currentInfo: ICurrentUserType
3737
): Promise<string> {
38-
console.log(request);
38+
console.log('进来了------------------》》', request);
3939
// 1.判断当前商户下是否已经存在该账号
4040
const accountEntity: Pick<AccountEntity, 'id'> | null = await this.accountRepository.findOne({
4141
where: { username: req.username, tenantId: currentInfo.tenantId },
@@ -54,9 +54,9 @@ export class AccountService {
5454
sort: req.sort,
5555
password: password,
5656
tenantId: currentInfo.tenantId,
57-
parentId: currentInfo.accountId,
57+
parentId: req.parentId ? req.parentId : currentInfo.id,
5858
salt: salt,
59-
lastLoginIp: this.toolsService.getReqIP(request),
59+
// lastLoginIp: this.toolsService.getReqIP(request),
6060
lastLoginDate: new Date(),
6161
});
6262
await this.accountRepository.save(data);
@@ -180,7 +180,7 @@ export class AccountService {
180180
if ([StatusEnum.NORMAL, StatusEnum.FORBIDDEN].includes(status)) {
181181
query.set('status', Equal(status + ''));
182182
}
183-
const { accountType, accountId, tenantId } = currentInfo;
183+
const { accountType, id, tenantId } = currentInfo;
184184
/**
185185
* 1.如果是超管,查询到全部的账号
186186
* 2.如果不是超管,是主账号的时候查询下面全部的账号
@@ -194,19 +194,18 @@ export class AccountService {
194194
} else if (accountType == AccountTypeEnum.PRIMARY_ACCOUNT) {
195195
query.set('tenantId', Equal(tenantId + ''));
196196
} else if (accountType == AccountTypeEnum.NORMAL_ACCOUNT) {
197-
query.set('parentId', Equal(accountId + ''));
197+
query.set('parentId', Equal(id + ''));
198198
}
199199
}
200200

201-
console.log(mapToObj(query), '???????????????????');
202201
const total = await this.accountRepository
203202
.createQueryBuilder('account')
204203
.where(mapToObj(query))
205204
.getCount();
206205
const queryBuilder = this.queryAccountBuilder;
207206
const data = await queryBuilder
208207
.where(mapToObj(query))
209-
.orderBy({ id: 'DESC' })
208+
.orderBy({ accountType: 'DESC', id: 'DESC' })
210209
.offset((pageNumber - 1) * pageSize)
211210
.limit(pageSize)
212211
.getRawMany();
@@ -218,6 +217,37 @@ export class AccountService {
218217
};
219218
}
220219

220+
/**
221+
* @Author: 水痕
222+
* @Date: 2023-10-10 18:56:27
223+
* @LastEditors: 水痕
224+
* @Description: 获取当前账号下的子账号
225+
* @param {ICurrentUserType} currentInfo
226+
* @param {number} status
227+
* @return {*}
228+
*/
229+
async getAccountListApi(
230+
currentInfo: ICurrentUserType,
231+
status: number
232+
): Promise<Pick<AccountEntity, 'id' | 'username'>[]> {
233+
const { id, accountType } = currentInfo;
234+
console.log(status, '状态');
235+
const query = new Map<string, FindOperator<string>>();
236+
if (Object.is(accountType, AccountTypeEnum.SUPER_ACCOUNT)) {
237+
query.set('parentId', Equal(-1 + ''));
238+
} else {
239+
query.set('parentId', Equal(id + ''));
240+
}
241+
if ([StatusEnum.FORBIDDEN, StatusEnum.NORMAL].includes(status)) {
242+
query.set('status', Equal(status + ''));
243+
}
244+
const accountEntity: Pick<AccountEntity, 'id' | 'username'>[] =
245+
await this.accountRepository.find({
246+
where: mapToObj(query),
247+
select: ['id', 'username'],
248+
});
249+
return accountEntity;
250+
}
221251
/**
222252
* @Author: 水痕
223253
* @Date: 2023-10-07 20:28:16

src/api/account/dto/account.dto.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ export class AccountDto {
1111
@Type(() => Number)
1212
@IsOptional({ message: '排序' })
1313
sort!: number;
14+
15+
@Min(1, { message: '父节点id最小值为1' })
16+
@IsInt({ message: '父节点id必须是整数' })
17+
@Type(() => Number)
18+
@IsOptional({ message: '父节点id' })
19+
parentId!: number;
1420
}

src/api/role/role.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class RoleService {
3030
* @return {*}
3131
*/
3232
async createRoleApi(req: RoleDto, currentInfo: ICurrentUserType): Promise<string> {
33-
const { accountId, tenantId } = currentInfo;
33+
const { id, tenantId } = currentInfo;
3434
// 1.判断当前账号下角色是否存在
3535
const roleEntity: Pick<RoleEntity, 'id'> | null = await this.roleRepository.findOne({
3636
where: { name: req.name, tenantId: tenantId },
@@ -42,7 +42,7 @@ export class RoleService {
4242
const roleData = this.roleRepository.create({
4343
name: req.name,
4444
tenantId,
45-
accountId,
45+
accountId: id,
4646
sort: req.sort,
4747
description: req.description,
4848
});
@@ -129,7 +129,7 @@ export class RoleService {
129129
pageNumber = PageEnum.PAGE_NUMBER,
130130
pageSize = PageEnum.PAGE_SIZE,
131131
} = queryOption;
132-
const { accountType, accountId, tenantId } = currentInfo;
132+
const { accountType, id, tenantId } = currentInfo;
133133
const query = new Map<string, FindOperator<string>>();
134134
if (name) {
135135
query.set('name', ILike(`%${name}%`));
@@ -142,7 +142,7 @@ export class RoleService {
142142
} else if (accountType == AccountTypeEnum.PRIMARY_ACCOUNT) {
143143
query.set('tenantId', Equal(tenantId + ''));
144144
} else if (accountType == AccountTypeEnum.NORMAL_ACCOUNT) {
145-
query.set('accountId', Equal(accountId + ''));
145+
query.set('accountId', Equal(id + ''));
146146
}
147147
const queryBuilder = this.queryRoleBuilder;
148148
const data = await queryBuilder

src/decorators/current-user.decorator.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { createParamDecorator, ExecutionContext } from '@nestjs/common';
55
*/
66
export interface ICurrentUserType {
77
id: number; // 账号id
8-
accountId: number; // 账号id
98
username: string; // 用户名
109
tenantId: number; // 商户id
1110
accountType: number; // 账号类型:0普通账号,1是主账号,2是超管

0 commit comments

Comments
 (0)