用户模块:获取当前用户
This commit is contained in:
parent
ce52a2b2c2
commit
4670360495
@ -5,12 +5,10 @@ import com.huangge1199.picture.exception.ErrorCode;
|
||||
import com.huangge1199.picture.exception.ThrowUtils;
|
||||
import com.huangge1199.picture.model.dto.user.UserLoginRequest;
|
||||
import com.huangge1199.picture.model.dto.user.UserRegisterRequest;
|
||||
import com.huangge1199.picture.model.entity.User;
|
||||
import com.huangge1199.picture.model.vo.LoginUserVO;
|
||||
import com.huangge1199.picture.service.UserService;
|
||||
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.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@ -53,5 +51,15 @@ public class UserController {
|
||||
return R.ok(loginUserVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户
|
||||
*/
|
||||
@GetMapping("/get/login")
|
||||
public R<LoginUserVO> getLoginUser(HttpServletRequest request) {
|
||||
User user = userService.getLoginUser(request);
|
||||
return R.ok(userService.getLoginUserVO(user));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -49,4 +49,12 @@ public interface UserService extends IService<User> {
|
||||
*/
|
||||
LoginUserVO getLoginUserVO(User user);
|
||||
|
||||
/**
|
||||
* 获取当前登录用户
|
||||
*
|
||||
* @param request 请求
|
||||
* @return 当前登录用户信息
|
||||
*/
|
||||
User getLoginUser(HttpServletRequest request);
|
||||
|
||||
}
|
||||
|
@ -116,6 +116,23 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
||||
return loginUserVO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getLoginUser(HttpServletRequest request) {
|
||||
// 先判断是否已登录
|
||||
Object userObj = request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
|
||||
User currentUser = (User) userObj;
|
||||
if (currentUser == null || currentUser.getId() == null) {
|
||||
throw new MyException(ErrorCode.NOT_LOGIN_ERROR);
|
||||
}
|
||||
// 从数据库查询(追求性能的话可以注释,直接返回上述结果)
|
||||
long userId = currentUser.getId();
|
||||
currentUser = this.getById(userId);
|
||||
if (currentUser == null) {
|
||||
throw new MyException(ErrorCode.NOT_LOGIN_ERROR);
|
||||
}
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user