User优化

This commit is contained in:
Xubx 2024-09-03 23:12:58 +08:00
parent 3be9a64ae5
commit d32524253e
2 changed files with 11 additions and 6 deletions

View File

@ -41,8 +41,8 @@ public class UserController {
}
@RequestMapping("getById")
public JSONObject queryById(@RequestParam("id") Integer id){
return userService.queryById(id);
public JSONObject queryById(@RequestParam("openId") String openId){
return userService.queryById(openId);
}
@RequestMapping("delete")
public JSONObject delete(@RequestParam("id")Integer id){

View File

@ -21,6 +21,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@Service
@ -34,6 +35,8 @@ public class UserService extends ServiceImpl<UserMapper, User> {
return new JSONObject().fluentPut("success", false).fluentPut("message", "参数错误");
}
List<User> userList = list();
User getUserIdByopenId = userList.stream().filter(e -> Objects.equals(e.getOpenId(), user.getOpenId())).findFirst().orElse(null);
user.setId(getUserIdByopenId != null ? getUserIdByopenId.getId() : null);
List<String> openIdList = userList.stream().map(User::getOpenId).collect(Collectors.toList());
if (user.getId() == null && !CollectionUtils.isEmpty(openIdList) && openIdList.contains(user.getOpenId())) {
return new JSONObject().fluentPut("message", "该用户已存在").fluentPut("success", false);
@ -78,11 +81,13 @@ public class UserService extends ServiceImpl<UserMapper, User> {
}
}
public JSONObject queryById(Integer id) {
if (id == null) {
return new JSONObject().fluentPut("success", false).fluentPut("message", "缺少ID");
public JSONObject queryById(String openId) {
if (openId == null) {
return new JSONObject().fluentPut("success", false).fluentPut("message", "缺少openID");
}
User user = getById(id);
List<User> userList = list();
User user = userList.stream().filter(e -> Objects.equals(e.getOpenId(), openId)).findFirst().orElse(null);
return new JSONObject().fluentPut("data", user);
}