From 5e7ed3d6f79b6118b218abb3c238f66b89ade058 Mon Sep 17 00:00:00 2001 From: tone <3341154833@qq.com> Date: Tue, 13 May 2025 10:53:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90me=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tone-page-server/src/user/user.controller.ts | 10 +++++++--- tone-page-server/src/user/user.service.ts | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tone-page-server/src/user/user.controller.ts b/tone-page-server/src/user/user.controller.ts index 57b5824..4a14c4a 100644 --- a/tone-page-server/src/user/user.controller.ts +++ b/tone-page-server/src/user/user.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Injectable, UseGuards } from '@nestjs/common'; +import { Controller, Get, Injectable, Request, UnauthorizedException, UseGuards } from '@nestjs/common'; import { UserService } from './user.service'; import { AuthGuard } from '@nestjs/passport'; @@ -11,7 +11,11 @@ export class UserController { @UseGuards(AuthGuard('jwt')) @Get('me') - async getMe() { - return 'ok'; + async getMe(@Request() req) { + const { user } = req; + if (!user || !user.userId) { + throw new UnauthorizedException('Unauthorized'); + } + return this.userService.findOne({ userId: user.userId }); } } diff --git a/tone-page-server/src/user/user.service.ts b/tone-page-server/src/user/user.service.ts index af804a8..07a21fd 100644 --- a/tone-page-server/src/user/user.service.ts +++ b/tone-page-server/src/user/user.service.ts @@ -16,7 +16,7 @@ export class UserService { async findOne(options: UserFindOptions | UserFindOptions[]): Promise { if (Object.keys(options).length === 0) { - return null; + throw new BadRequestException('查询条件不能为空'); } return this.userRepository.findOne({ where: options }); }