feat(新增): 获取当前登录用户信息
Signed-off-by: bunny <1319900154@qq.com>
This commit is contained in:
parent
75c2ba154a
commit
70dd7c63f1
|
@ -30,4 +30,5 @@ public class MessageConstant {
|
|||
public static final String USER_DOES_IS_EXIST = "用户已存在";
|
||||
public static final String VERIFICATION_CODE_IS_EMPTY = "请先发送验证码";
|
||||
public static final String LOGIN_DTO_IS_EMPTY = "登录参数不能为空";
|
||||
public static final String TOKEN_IS_EMPTY = "token为空";
|
||||
}
|
||||
|
|
|
@ -2,15 +2,14 @@ package com.atguigu.user.controller;
|
|||
|
||||
import com.atguigu.spzx.model.dto.h5.UserLoginDto;
|
||||
import com.atguigu.spzx.model.dto.h5.UserRegisterDto;
|
||||
import com.atguigu.spzx.model.vo.h5.UserInfoVo;
|
||||
import com.atguigu.spzx.model.vo.result.Result;
|
||||
import com.atguigu.user.service.UserInfoService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Tag(name = "会员用户接口")
|
||||
@RestController
|
||||
|
@ -32,4 +31,12 @@ public class UserInfoController {
|
|||
String token = userInfoService.login(userLoginDto);
|
||||
return Result.success(token);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取当前登录用户信息")
|
||||
@GetMapping("auth/getCurrentUserInfo")
|
||||
public Result<UserInfoVo> getCurrentUserInfo(HttpServletRequest request) {
|
||||
String token = request.getHeader("token");
|
||||
UserInfoVo userInfo = userInfoService.getCurrentUserInfo(token);
|
||||
return Result.success(userInfo);
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ package com.atguigu.user.service;
|
|||
|
||||
import com.atguigu.spzx.model.dto.h5.UserLoginDto;
|
||||
import com.atguigu.spzx.model.dto.h5.UserRegisterDto;
|
||||
import com.atguigu.spzx.model.vo.h5.UserInfoVo;
|
||||
|
||||
public interface UserInfoService {
|
||||
/**
|
||||
|
@ -18,4 +19,12 @@ public interface UserInfoService {
|
|||
* @return 返回token
|
||||
*/
|
||||
String login(UserLoginDto userLoginDto);
|
||||
|
||||
/**
|
||||
* 获取当前登录用户信息
|
||||
*
|
||||
* @param token token
|
||||
* @return 用户信息
|
||||
*/
|
||||
UserInfoVo getCurrentUserInfo(String token);
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import com.atguigu.exception.BunnyException;
|
|||
import com.atguigu.spzx.model.dto.h5.UserLoginDto;
|
||||
import com.atguigu.spzx.model.dto.h5.UserRegisterDto;
|
||||
import com.atguigu.spzx.model.entity.user.UserInfo;
|
||||
import com.atguigu.spzx.model.vo.h5.UserInfoVo;
|
||||
import com.atguigu.user.mapper.UserInfoMapper;
|
||||
import com.atguigu.user.service.UserInfoService;
|
||||
import com.atguigu.utils.EmptyUtil;
|
||||
|
@ -97,4 +98,22 @@ public class UserInfoServiceImpl implements UserInfoService {
|
|||
redisTemplate.opsForValue().set(token, JSON.toJSONString(userInfo), 30, TimeUnit.DAYS);
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录用户信息
|
||||
*
|
||||
* @param token token
|
||||
* @return 用户信息
|
||||
*/
|
||||
@Override
|
||||
public UserInfoVo getCurrentUserInfo(String token) {
|
||||
Object tokenObject = redisTemplate.opsForValue().get(token);
|
||||
emptyUtil.isEmpty(tokenObject, MessageConstant.TOKEN_IS_EMPTY);
|
||||
|
||||
UserInfo userInfo = JSON.parseObject((String) tokenObject, UserInfo.class);
|
||||
UserInfoVo userInfoVo = new UserInfoVo();
|
||||
BeanUtils.copyProperties(userInfo, userInfoVo);
|
||||
|
||||
return userInfoVo;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue