feat(auth): 添加获取用户信息接口
All checks were successful
Auto-build / Automatic-Packaging (push) Successful in 14s
All checks were successful
Auto-build / Automatic-Packaging (push) Successful in 14s
- 在 AuthController 中新增 getUserInfo 接口,用于获取当前登录用户的信息 - 创建 UserInfoVO 类,封装用户 ID、用户名和头像信息 - 在 AuthService 中实现 getUserInfo 方法,从数据库查询用户并转换为 UserInfoVO - 头像字段使用 Base64 编码返回- 使用 StpUtil 获取当前登录用户的 ID 进行查询
This commit is contained in:
parent
1aabc49521
commit
192bb72251
@ -1,13 +1,11 @@
|
||||
package com.kane.animo.auth.controller;
|
||||
|
||||
import com.kane.animo.auth.domain.form.LoginForm;
|
||||
import com.kane.animo.auth.domain.vo.UserInfoVO;
|
||||
import com.kane.animo.auth.service.AuthService;
|
||||
import com.kane.animo.model.R;
|
||||
import jakarta.annotation.Resource;
|
||||
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.*;
|
||||
|
||||
/**
|
||||
* 认证
|
||||
@ -40,4 +38,13 @@ public class AuthController {
|
||||
public R<String> register(@RequestBody LoginForm from) {
|
||||
return service.register(from);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
* @return 用户信息
|
||||
*/
|
||||
@GetMapping("/getUserInfo")
|
||||
public R<UserInfoVO> getUserInfo() {
|
||||
return R.success(service.getUserInfo());
|
||||
}
|
||||
}
|
||||
|
||||
26
src/main/java/com/kane/animo/auth/domain/vo/UserInfoVO.java
Normal file
26
src/main/java/com/kane/animo/auth/domain/vo/UserInfoVO.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.kane.animo.auth.domain.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* 用户信息
|
||||
* @author Kane
|
||||
* @since 2025/11/12 16:50
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class UserInfoVO {
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
private Long userId;
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 头像
|
||||
*/
|
||||
private String avatar;
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
package com.kane.animo.auth.service;
|
||||
|
||||
import com.kane.animo.auth.domain.form.LoginForm;
|
||||
import com.kane.animo.auth.domain.vo.UserInfoVO;
|
||||
import com.kane.animo.model.R;
|
||||
|
||||
/**
|
||||
@ -22,4 +23,10 @@ public interface AuthService {
|
||||
* @return 注册结果
|
||||
*/
|
||||
R<String> register(LoginForm from);
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
* @return 用户信息
|
||||
*/
|
||||
UserInfoVO getUserInfo();
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
||||
import com.kane.animo.auth.domain.User;
|
||||
import com.kane.animo.auth.domain.form.LoginForm;
|
||||
import com.kane.animo.auth.domain.vo.UserInfoVO;
|
||||
import com.kane.animo.auth.mapper.UserMapper;
|
||||
import com.kane.animo.auth.service.AuthService;
|
||||
import com.kane.animo.exception.ServiceException;
|
||||
@ -12,6 +13,9 @@ import com.kane.animo.model.R;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* 认证服务
|
||||
* @author Kane
|
||||
@ -64,4 +68,25 @@ public class AuthServiceImpl implements AuthService {
|
||||
userMapper.insert(user);
|
||||
return login(from);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
*
|
||||
* @return 用户信息
|
||||
*/
|
||||
@Override
|
||||
public UserInfoVO getUserInfo() {
|
||||
User one = new LambdaQueryChainWrapper<>(userMapper)
|
||||
.eq(User::getId, StpUtil.getLoginIdAsLong())
|
||||
.one();
|
||||
String avatar = null;
|
||||
try {
|
||||
avatar = Base64.getEncoder().encodeToString(one.getAvatar().readAllBytes());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return new UserInfoVO()
|
||||
.setName(one.getUser())
|
||||
.setUserId(one.getId())
|
||||
.setAvatar(avatar);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user