Skip to content

Commit 987bbee

Browse files
committed
feat: 默认账号
1 parent 5f0c985 commit 987bbee

File tree

6 files changed

+103
-20
lines changed

6 files changed

+103
-20
lines changed

src/api/account/account.controller.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class AccountController {
2626
@Post()
2727
async createAccountApi(
2828
@Body() req: AccountDto,
29-
@CurrentUser() currentInfo: ICurrentUserType,
29+
@CurrentUser('userInfo') currentInfo: ICurrentUserType,
3030
request: Request
3131
): Promise<string> {
3232
return await this.accountService.createAccountApi(req, request, currentInfo);
@@ -41,8 +41,11 @@ export class AccountController {
4141
}
4242

4343
@Put('/status/:id')
44-
async modifyAccountStatusByIdApi(@Param('id', new ParseIntPipe()) id: number): Promise<string> {
45-
return await this.accountService.modifyAccountStatusByIdApi(id);
44+
async modifyAccountStatusByIdApi(
45+
@Param('id', new ParseIntPipe()) id: number,
46+
@CurrentUser('userInfo') currentUser: ICurrentUserType
47+
): Promise<string> {
48+
return await this.accountService.modifyAccountStatusByIdApi(id, currentUser);
4649
}
4750

4851
@Put(':id')
@@ -56,7 +59,7 @@ export class AccountController {
5659
@Get()
5760
async getAccountPageApi(
5861
@Query() queryOption: QueryAccountDto,
59-
@CurrentUser() currentInfo: ICurrentUserType
62+
@CurrentUser('userInfo') currentInfo: ICurrentUserType
6063
): Promise<AccountPageVo> {
6164
return await this.accountService.getAccountPageApi(queryOption, currentInfo);
6265
}

src/api/account/account.service.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class AccountService {
3535
request: Request,
3636
currentInfo: ICurrentUserType
3737
): Promise<string> {
38+
console.log(request);
3839
// 1.判断当前商户下是否已经存在该账号
3940
const accountEntity: Pick<AccountEntity, 'id'> | null = await this.accountRepository.findOne({
4041
where: { username: req.username, tenantId: currentInfo.tenantId },
@@ -71,7 +72,7 @@ export class AccountService {
7172
* @return {*}
7273
*/
7374
async deleteAccountByIdApi(id: number, currentUser: ICurrentUserType): Promise<string> {
74-
const { accountId } = currentUser;
75+
const { id: accountId } = currentUser;
7576
// TODO 判断不能删除自己及下面有账号的
7677
if (Object.is(id, accountId)) {
7778
throw new HttpException('自己不能删除自己', HttpStatus.OK);
@@ -105,7 +106,11 @@ export class AccountService {
105106
* @param {number} id
106107
* @return {*}
107108
*/
108-
async modifyAccountStatusByIdApi(id: number): Promise<string> {
109+
async modifyAccountStatusByIdApi(id: number, currentUser: ICurrentUserType): Promise<string> {
110+
const { id: accountId } = currentUser;
111+
if (Object.is(id, accountId)) {
112+
throw new HttpException('自己不能修改自己', HttpStatus.OK);
113+
}
109114
const accountEntity: Pick<AccountEntity, 'status'> | null =
110115
await this.accountRepository.findOne({
111116
where: { id },
@@ -134,6 +139,14 @@ export class AccountService {
134139
* @return {*}
135140
*/
136141
async modifyAccountByIdApi(id: number, req: AccountDto): Promise<string> {
142+
// 判断当前的用户名之前是否有
143+
const accountEntity: Pick<AccountEntity, 'id'> | null = await this.accountRepository.findOne({
144+
where: { username: req.username },
145+
select: ['id'],
146+
});
147+
if (accountEntity && accountEntity.id != id) {
148+
throw new HttpException(`[${req.username}]可能已经存在`, HttpStatus.OK);
149+
}
137150
const { affected } = await this.accountRepository.update(id, req);
138151
if (affected) {
139152
return '修改成功';
@@ -156,6 +169,7 @@ export class AccountService {
156169
const {
157170
status,
158171
username,
172+
tenantId: queryTenantId,
159173
pageNumber = PageEnum.PAGE_NUMBER,
160174
pageSize = PageEnum.PAGE_SIZE,
161175
} = queryOption;
@@ -172,13 +186,19 @@ export class AccountService {
172186
* 2.如果不是超管,是主账号的时候查询下面全部的账号
173187
* 3.如果都不是,只能查询账号下的数据
174188
*/
175-
if (accountType == AccountTypeEnum.SUPER_ACCOUNT) {
176-
console.log('超管不需要');
177-
} else if (accountType == AccountTypeEnum.PRIMARY_ACCOUNT) {
178-
query.set('tenantId', Equal(tenantId + ''));
179-
} else if (accountType == AccountTypeEnum.NORMAL_ACCOUNT) {
180-
query.set('parentId', Equal(accountId + ''));
189+
if (queryTenantId) {
190+
query.set('tenantId', Equal(queryTenantId + ''));
191+
} else {
192+
if (accountType == AccountTypeEnum.SUPER_ACCOUNT) {
193+
console.log('超管不需要');
194+
} else if (accountType == AccountTypeEnum.PRIMARY_ACCOUNT) {
195+
query.set('tenantId', Equal(tenantId + ''));
196+
} else if (accountType == AccountTypeEnum.NORMAL_ACCOUNT) {
197+
query.set('parentId', Equal(accountId + ''));
198+
}
181199
}
200+
201+
console.log(mapToObj(query), '???????????????????');
182202
const total = await this.accountRepository
183203
.createQueryBuilder('account')
184204
.where(mapToObj(query))
@@ -189,7 +209,6 @@ export class AccountService {
189209
.orderBy({ id: 'DESC' })
190210
.offset((pageNumber - 1) * pageSize)
191211
.limit(pageSize)
192-
.printSql()
193212
.getRawMany();
194213
return {
195214
data,
@@ -222,7 +241,7 @@ export class AccountService {
222241
idList: number[],
223242
currentUser: ICurrentUserType
224243
): Promise<string> {
225-
const { accountId } = currentUser;
244+
const { id: accountId } = currentUser;
226245
// TODO 判断不能删除自己及下面有账号的
227246
if (idList.includes(accountId)) {
228247
throw new HttpException('自己不能删除自己', HttpStatus.OK);
@@ -264,7 +283,8 @@ export class AccountService {
264283
idList: number[],
265284
currentUser: ICurrentUserType
266285
): Promise<string> {
267-
const { accountId } = currentUser;
286+
const { id: accountId } = currentUser;
287+
console.log(idList, accountId, '????==============');
268288
if (idList.includes(accountId)) {
269289
throw new HttpException('自己不能修改自己', HttpStatus.OK);
270290
}
@@ -319,7 +339,7 @@ export class AccountService {
319339
.addSelect('account1.username', 'parentName')
320340
.from(AccountEntity, 'account1'),
321341
'account1',
322-
'account.parentId=account1.id'
342+
'account.parentId=account1.parentId'
323343
)
324344
.leftJoinAndMapOne(
325345
'xx',
@@ -329,7 +349,7 @@ export class AccountService {
329349
.addSelect('tenant.name', 'tenantName')
330350
.from(TenantEntity, 'tenant'),
331351
'tenant',
332-
'account.tenantId=tenant.id'
352+
'account.tenantId=tenant.tenantId'
333353
);
334354
}
335355
}

src/api/login/login.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class LoginService {
8080
private get queryLoginBuilder(): SelectQueryBuilder<LoginAccountVo> {
8181
return this.accountRepository
8282
.createQueryBuilder('account')
83-
.select('account.id', 'id')
83+
.addSelect('account.id', 'id')
8484
.addSelect('account.username', 'username')
8585
.addSelect('account.tenantId', 'tenantId')
8686
.addSelect('account.status', 'status')

src/api/tenant/dto/tenant.dto.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,17 @@ export class RechargeDto {
6161
@IsNotEmpty({ message: '充值金额不能为空' })
6262
amount!: number;
6363
}
64+
65+
export class CreateDefaultAccountDto {
66+
@IsNotEmpty({ message: '账号名不能为空' })
67+
username!: string;
68+
69+
@IsNotEmpty({ message: '商户id不能为空' })
70+
tenantId!: number;
71+
72+
@Min(1, { message: '排序最小值为1' })
73+
@IsInt({ message: '排序必须是整数' })
74+
@Type(() => Number)
75+
@IsOptional({ message: '排序' })
76+
sort!: number;
77+
}

src/api/tenant/tenant.controller.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from '@nestjs/common';
1313
import { CurrentUser, ICurrentUserType } from '@src/decorators';
1414
import { AuthGuard } from '@src/guard/auth.guard';
15-
import { RechargeDto, TenantDto } from './dto/tenant.dto';
15+
import { CreateDefaultAccountDto, RechargeDto, TenantDto } from './dto/tenant.dto';
1616
import { QueryTenantDto } from './dto/tenant.query';
1717
import { TenantService } from './tenant.service';
1818
import { TenantPageVo, TenantVo } from './vo/tenant.vo';
@@ -82,4 +82,9 @@ export class TenantController {
8282
async rechargeTenantApi(@Body() req: RechargeDto): Promise<string> {
8383
return await this.tenantService.rechargeTenantApi(req);
8484
}
85+
86+
@Post('defaultAccount')
87+
async createDefaultAccount(@Body() req: CreateDefaultAccountDto): Promise<string> {
88+
return await this.tenantService.createDefaultAccount(req);
89+
}
8590
}

src/api/tenant/tenant.service.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import {
1111
Repository,
1212
SelectQueryBuilder,
1313
} from 'typeorm';
14-
import { RechargeDto, TenantDto } from './dto/tenant.dto';
14+
import { CreateDefaultAccountDto, RechargeDto, TenantDto } from './dto/tenant.dto';
1515
import { QueryTenantDto } from './dto/tenant.query';
1616
import { PageEnum, StatusEnum } from '@src/enums';
1717
import { TenantPageVo, TenantVo } from './vo/tenant.vo';
1818
import { AreaEntity } from '../area/entities/area.entity';
1919
import { mapToObj } from '@src/utils';
2020
import { AccountEntity } from '../account/entities/account.entity';
2121
import { ICurrentUserType } from '@src/decorators';
22+
import { ToolsService } from '@src/plugin/tools/tools.service';
23+
import { ConfigService } from '@nestjs/config';
24+
import { AccountTypeEnum } from '@src/enums/account.type.enum';
2225

2326
@Injectable()
2427
export class TenantService {
@@ -27,6 +30,8 @@ export class TenantService {
2730
private readonly tenantRepository: Repository<TenantEntity>,
2831
@InjectRepository(AccountEntity)
2932
private readonly accountRepository: Repository<AccountEntity>,
33+
private readonly toolsService: ToolsService,
34+
private readonly configService: ConfigService,
3035
private readonly dataSource: DataSource
3136
) {}
3237

@@ -309,6 +314,42 @@ export class TenantService {
309314
await queryRunner.release();
310315
}
311316
}
317+
318+
/**
319+
* @Author: 水痕
320+
* @Date: 2023-10-10 17:23:46
321+
* @LastEditors: 水痕
322+
* @Description: 创建默认商户登录账号
323+
* @param {CreateDefaultAccountDto} req
324+
* @return {*}
325+
*/
326+
async createDefaultAccount(req: CreateDefaultAccountDto): Promise<string> {
327+
// 1.判断当前商户下是否已经存在该账号
328+
const accountEntity: Pick<AccountEntity, 'id'> | null = await this.accountRepository.findOne({
329+
where: { username: req.username, tenantId: req.tenantId },
330+
select: ['id'],
331+
});
332+
if (accountEntity?.id) {
333+
throw new HttpException(`[${req.username}]可能已经存在,请先检查`, HttpStatus.OK);
334+
}
335+
// 默认密码加密
336+
const salt = this.toolsService.getRandomSalt;
337+
const defaultPassword: string = this.configService.get('defaultPassword') ?? '123456';
338+
const password = this.toolsService.makePassword(defaultPassword, salt);
339+
// 创建数据
340+
const data = this.accountRepository.create({
341+
username: req.username,
342+
sort: req.sort,
343+
password: password,
344+
tenantId: req.tenantId,
345+
parentId: -1,
346+
salt: salt,
347+
accountType: AccountTypeEnum.PRIMARY_ACCOUNT, // 主账号
348+
lastLoginDate: new Date(),
349+
});
350+
await this.accountRepository.save(data);
351+
return '创建成功';
352+
}
312353
/**
313354
* @Author: 水痕
314355
* @Date: 2023-10-07 11:05:41

0 commit comments

Comments
 (0)