diff --git a/luoo_comment/src/main/java/com/luoo/comment/client/UserClient.java b/luoo_comment/src/main/java/com/luoo/comment/client/UserClient.java index 8506208..5e416d3 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/client/UserClient.java +++ b/luoo_comment/src/main/java/com/luoo/comment/client/UserClient.java @@ -1,11 +1,18 @@ package com.luoo.comment.client; +import api.Result; +import client.vo.SimpleUser; import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; + +import java.util.List; @FeignClient("luoo-user") public interface UserClient { //todo 根据用户 IDs 获取用户昵称集合 + @GetMapping("/findUserByIds") + Result> findUserByIds(List ids); } diff --git a/luoo_comment/src/main/java/com/luoo/comment/config/BaseExceptionHandler.java b/luoo_comment/src/main/java/com/luoo/comment/config/BaseExceptionHandler.java index 580fa70..0036d98 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/config/BaseExceptionHandler.java +++ b/luoo_comment/src/main/java/com/luoo/comment/config/BaseExceptionHandler.java @@ -24,8 +24,6 @@ public class BaseExceptionHandler { @ExceptionHandler(value = AuthorityLoginException.class) @ResponseBody public Result error(AuthorityLoginException e) { - log.info("业务错误:{}", e.getMessage()); - StatusCode statusCode = null == e.getCodeEnum() ? StatusCode.MUSIC_COMMON_FAILED : e.getCodeEnum(); - return Result.failed(statusCode, e.getMessage()); + return Result.unauthorized(null); } } diff --git a/luoo_common/src/main/java/client/vo/SimpleUser.java b/luoo_common/src/main/java/client/vo/SimpleUser.java new file mode 100644 index 0000000..2198356 --- /dev/null +++ b/luoo_common/src/main/java/client/vo/SimpleUser.java @@ -0,0 +1,11 @@ +package client.vo; + +import lombok.Data; + +@Data +public class SimpleUser { + + private String userId; + + private String nickName; +} diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java index bbc37a0..4b78db0 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java @@ -1,27 +1,22 @@ package com.luoo.user.controller; -import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -import com.luoo.user.service.UserCollectService; - import annotation.GlobalInterceptor; import annotation.VerifyParam; import api.Result; +import client.vo.SimpleUser; +import com.luoo.user.service.UserCollectService; +import com.luoo.user.service.UserService; import dto.UserLoginDto; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; import util.JwtUtil; +import java.util.List; + @Api(tags = "UserCollectController") @RestController @RequestMapping("/userCollect") @@ -30,6 +25,8 @@ public class UserCollectController { private JwtUtil jwtUtil; @Autowired private UserCollectService userCollectService; + @Autowired + private UserService userService; @ApiOperation(value = "1.收藏/喜欢") @ApiImplicitParams({ @ApiImplicitParam(name = "objectId", value = "收藏/喜欢的id,此处为歌曲/期刊id", required = true), @@ -57,4 +54,12 @@ public class UserCollectController { collectType); return Result.success(); } + + @ApiOperation(value = "根据 IDs 获取用户简要信息") + @GetMapping("/findUserByIds") + public Result> findUserByIds(@RequestBody List ids) { + List userByIds = userService.findUserByIds(ids); + return Result.success(userByIds); + } + } diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserDao.java index 5c6b5a3..c35d279 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/UserDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserDao.java @@ -1,12 +1,13 @@ package com.luoo.user.dao; +import com.luoo.user.pojo.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; - -import com.luoo.user.pojo.User; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import java.util.List; + /** * 数据访问接口 * @author Administrator @@ -28,4 +29,7 @@ public interface UserDao extends JpaRepository,JpaSpecificationExec public User findByLoginname(String loginname); public long countByNickname(String nickName); + + @Query("SELECT * FROM tb_user m WHERE m.id IN ?1") + List findUserByIds(List ids); } diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/User.java b/luoo_user/src/main/java/com/luoo/user/pojo/User.java index 01b98e8..36f33af 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/User.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/User.java @@ -1,12 +1,12 @@ package com.luoo.user.pojo; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; - +import client.vo.SimpleUser; import lombok.Getter; import lombok.Setter; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; import java.io.Serializable; /** * 实体类 @@ -40,4 +40,11 @@ public class User implements Serializable{ private Integer followcount;//关注数 //private String deviceId;//设备id //private String deviceBrand;//设备品牌 + + public SimpleUser converSimpleUser(){ + SimpleUser simpleUser = new SimpleUser(); + simpleUser.setUserId(this.getId()); + simpleUser.setNickName(this.nickname); + return simpleUser; + } } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserService.java b/luoo_user/src/main/java/com/luoo/user/service/UserService.java index 1aae65f..f89a4ea 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserService.java @@ -1,14 +1,12 @@ package com.luoo.user.service; -import java.util.*; -import java.util.concurrent.TimeUnit; - -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; -import javax.servlet.http.HttpServletRequest; - +import client.vo.SimpleUser; +import com.luoo.user.dao.UserDao; +import com.luoo.user.dao.UserInfoDao; +import com.luoo.user.pojo.User; +import com.luoo.user.util.NickNameUtil; +import constants.Constants; +import dto.UserLoginDto; import org.apache.commons.lang3.RandomStringUtils; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; @@ -18,19 +16,20 @@ import org.springframework.data.jpa.domain.Specification; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; - import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; import util.IdWorker; - -import constants.Constants; -import com.luoo.user.dao.UserDao; -import com.luoo.user.dao.UserInfoDao; -import com.luoo.user.pojo.User; -import com.luoo.user.util.NickNameUtil; - -import dto.UserLoginDto; import util.JwtUtil; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import javax.servlet.http.HttpServletRequest; +import java.util.*; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + /** * 服务层 * @@ -299,4 +298,13 @@ public class UserService { public long countByNickName(String nickName) { return userDao.countByNickname(nickName); } + + + public List findUserByIds(List ids) { + List userByIds = userDao.findUserByIds(ids); + if (CollectionUtils.isEmpty(userByIds)){ + return new ArrayList<>(); + } + return userByIds.stream().map(User::converSimpleUser).collect(Collectors.toList()); + } }