feat-获取用户功能demo
This commit is contained in:
parent
9a9d2e7018
commit
66d02822f2
@ -24,7 +24,7 @@ public class BasicController {
|
|||||||
/**
|
/**
|
||||||
* 网站主页
|
* 网站主页
|
||||||
*/
|
*/
|
||||||
// @GetMapping("/**")
|
@GetMapping("/index")
|
||||||
public ResponseEntity<BaseResponse> index() {
|
public ResponseEntity<BaseResponse> index() {
|
||||||
log.info("访问主页");
|
log.info("访问主页");
|
||||||
BaseResponse response = new BaseResponse("欢迎", 200, "Success", "这是故事管理系统主页");
|
BaseResponse response = new BaseResponse("欢迎", 200, "Success", "这是故事管理系统主页");
|
||||||
@ -40,4 +40,15 @@ public class BasicController {
|
|||||||
return userService.login(username, password);
|
return userService.login(username, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/user/{userId}")
|
||||||
|
public ResponseEntity<BaseResponse> getUserCurrent(@PathVariable String userId) {
|
||||||
|
log.info("获取用户 {}", userId);
|
||||||
|
if (!userId.matches("^[0-9]+$")) {
|
||||||
|
return ResponseEntity.status(403).body(
|
||||||
|
new BaseResponse("PathValueError", 40301, "参数错误", null)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return userService.getUserInfo(userId);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,4 +21,10 @@ public class UserDAO {
|
|||||||
log.info("\t> Mysql 读取");
|
log.info("\t> Mysql 读取");
|
||||||
return userMapper.getPasswordByUsername(userName);
|
return userMapper.getPasswordByUsername(userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public UserDemoDO getUserById(String userId) {
|
||||||
|
log.info("[DAO] 执行 getUserById 方法");
|
||||||
|
log.info("\t> Mysql 读取");
|
||||||
|
return userMapper.getUserById(Long.parseLong(userId));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,4 +12,7 @@ import org.apache.ibatis.annotations.Select;
|
|||||||
public interface UserMapper {
|
public interface UserMapper {
|
||||||
@Select("SELECT * FROM stories_system.user_demo WHERE username = #{username}")
|
@Select("SELECT * FROM stories_system.user_demo WHERE username = #{username}")
|
||||||
UserDemoDO getPasswordByUsername(String username);
|
UserDemoDO getPasswordByUsername(String username);
|
||||||
|
|
||||||
|
@Select("SELECT * FROM stories_system.user_demo WHERE id = #{userId}")
|
||||||
|
UserDemoDO getUserById(long userId);
|
||||||
}
|
}
|
||||||
|
@ -9,4 +9,6 @@ import org.springframework.http.ResponseEntity;
|
|||||||
*/
|
*/
|
||||||
public interface UserService {
|
public interface UserService {
|
||||||
ResponseEntity<BaseResponse> login(String username, String password);
|
ResponseEntity<BaseResponse> login(String username, String password);
|
||||||
|
|
||||||
|
ResponseEntity<BaseResponse> getUserInfo(String userId);
|
||||||
}
|
}
|
||||||
|
@ -22,10 +22,10 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Override
|
@Override
|
||||||
public ResponseEntity<BaseResponse> login(String username, String password) {
|
public ResponseEntity<BaseResponse> login(String username, String password) {
|
||||||
UserDemoDO userDemoDO = userDAO.getPasswordByUserName(username);
|
UserDemoDO userDemoDO = userDAO.getPasswordByUserName(username);
|
||||||
log.info(String.valueOf(userDemoDO));
|
|
||||||
if (userDemoDO != null){
|
if (userDemoDO != null){
|
||||||
|
log.info(userDemoDO.toString());
|
||||||
if (userDemoDO.getPassword().equals(password)){
|
if (userDemoDO.getPassword().equals(password)){
|
||||||
BaseResponse response = new BaseResponse("登录成功", 200, "Success", "用户已登录");
|
BaseResponse response = new BaseResponse("登录成功", 200, "Success", userDemoDO);
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
} else {
|
} else {
|
||||||
BaseResponse response = new BaseResponse("登录失败", 404, "Error", "密码错误");
|
BaseResponse response = new BaseResponse("登录失败", 404, "Error", "密码错误");
|
||||||
@ -36,4 +36,18 @@ public class UserServiceImpl implements UserService {
|
|||||||
return ResponseEntity.status(404).body(response);
|
return ResponseEntity.status(404).body(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResponseEntity<BaseResponse> getUserInfo(String userId) {
|
||||||
|
UserDemoDO getUser = userDAO.getUserById(userId);
|
||||||
|
if (getUser != null) {
|
||||||
|
return ResponseEntity.ok().body(
|
||||||
|
new BaseResponse("Success", 200, "获取成功", getUser)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return ResponseEntity.status(404).body(
|
||||||
|
new BaseResponse("UserNotExist", 40401, "用户不存在", null)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user