localhost_oa_based/src/modules/system/online/online.controller.ts

46 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-02-28 17:02:46 +08:00
import { Body, Controller, Get, Post } from '@nestjs/common';
import { ApiExtraModels, ApiOperation, ApiTags } from '@nestjs/swagger';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { ApiResult } from '~/common/decorators/api-result.decorator';
import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator';
import { BusinessException } from '~/common/exceptions/biz.exception';
import { ErrorEnum } from '~/constants/error-code.constant';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { AuthUser } from '~/modules/auth/decorators/auth-user.decorator';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { Perm, definePermission } from '~/modules/auth/decorators/permission.decorator';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { KickDto } from './online.dto';
import { OnlineUserInfo } from './online.model';
import { OnlineService } from './online.service';
2024-02-28 08:32:35 +08:00
export const permissions = definePermission('system:online', {
LIST: 'list',
2024-02-29 09:29:03 +08:00
KICK: 'kick'
2024-02-28 17:02:46 +08:00
} as const);
2024-02-28 08:32:35 +08:00
@ApiTags('System - 在线用户模块')
@ApiSecurityAuth()
@ApiExtraModels(OnlineUserInfo)
@Controller('online')
export class OnlineController {
constructor(private onlineService: OnlineService) {}
@Get('list')
@ApiOperation({ summary: '查询当前在线用户' })
@ApiResult({ type: [OnlineUserInfo] })
@Perm(permissions.LIST)
async list(@AuthUser() user: IAuthUser): Promise<OnlineUserInfo[]> {
2024-02-28 17:02:46 +08:00
return this.onlineService.listOnlineUser(user.uid);
2024-02-28 08:32:35 +08:00
}
@Post('kick')
@ApiOperation({ summary: '下线指定在线用户' })
@Perm(permissions.KICK)
async kick(@Body() dto: KickDto, @AuthUser() user: IAuthUser): Promise<void> {
2024-02-28 17:02:46 +08:00
if (dto.id === user.uid) throw new BusinessException(ErrorEnum.NOT_ALLOWED_TO_LOGOUT_USER);
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
await this.onlineService.kickUser(dto.id, user.uid);
2024-02-28 08:32:35 +08:00
}
}