diff --git a/luoo_user/src/main/java/com/luoo/user/controller/CMSUserController.java b/luoo_user/src/main/java/com/luoo/user/controller/CMSUserController.java index da4993f..4f8434b 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/CMSUserController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/CMSUserController.java @@ -3,7 +3,6 @@ package com.luoo.user.controller; import java.util.Arrays; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; @@ -59,7 +58,7 @@ public class CMSUserController { @ApiOperation(value = "根据ID查询") @GetMapping("/{id}") public Result findById(@PathVariable String id) { - return Result.success(userInfoService.findById(id)); + return Result.success(userInfoService.cmsFindById(id)); } @ApiOperation(value = "分页+多条件查询") @@ -106,6 +105,6 @@ public class CMSUserController { @GetMapping("/ids/{ids}") public Result> orderByField(@PathVariable @VerifyParam(required = true) String ids) { List idList = Arrays.stream(ids.split(",")).map(String::trim).collect(Collectors.toList()); - return Result.success(userInfoService.orderByField(idList)); + return Result.success(userInfoService.cmsOrderByField(idList)); } } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java index 3e36302..1648c14 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java @@ -2,6 +2,7 @@ package com.luoo.user.service; import java.util.*; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; @@ -93,7 +94,6 @@ public class UserInfoService { return userInfoDao.getById(id); } - /** * 修改 * @@ -117,7 +117,6 @@ public class UserInfoService { userInfoDao.deleteById(id); } - public void sendSms(String deviceId, String mobile) { // 生成6位数字随机数 String checkcode = RandomStringUtils.randomNumeric(6); @@ -129,7 +128,7 @@ public class UserInfoService { Map map = new HashMap<>(); map.put("mobile", mobile); map.put("checkcode", checkcode); - rabbitTemplate.convertAndSend("sms",map); + rabbitTemplate.convertAndSend("sms", map); // 在控制台放一份(方便测试) @@ -173,20 +172,21 @@ public class UserInfoService { } public String autoLogin(String authorization, String deviceId, String deviceBrand, String ip) { - UserLoginDto userLoginDto=jwtUtil.getUserLoginDto(authorization); - if (null==userLoginDto) { - return null; - } + UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(authorization); + if (null == userLoginDto) { + return null; + } UserInfo userInfo = userInfoDao.findById(userLoginDto.getUserId()).get(); - if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { - return null; - } - userInfo.setLastLoginTime(new Date()); - userInfo.setLastLoginIp(ip); - userInfo.setLastUseDeviceId(deviceId); - userInfo.setLastUseDeviceBrand(deviceBrand); - userInfoDao.save(userInfo); - return Constants.TOKEN_PREFIX+jwtUtil.createJWT(userLoginDto.getUserId(), userInfo.getNickName(), userLoginDto.getRoles(),userLoginDto.getAvatar()); + if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { + return null; + } + userInfo.setLastLoginTime(new Date()); + userInfo.setLastLoginIp(ip); + userInfo.setLastUseDeviceId(deviceId); + userInfo.setLastUseDeviceBrand(deviceBrand); + userInfoDao.save(userInfo); + return Constants.TOKEN_PREFIX + jwtUtil.createJWT(userLoginDto.getUserId(), userInfo.getNickName(), + userLoginDto.getRoles(), userLoginDto.getAvatar()); } public long countByNickName(String nickName) { @@ -199,11 +199,11 @@ public class UserInfoService { user = userInfo; user.setId(String.valueOf(idWorker.nextId())); user.setNickName("雀乐-" + NickNameUtil.getRandomNickName()); - Date curDate = new Date(); - user.setJoinTime(curDate); - user.setLastLoginTime(curDate); - user.setStatus(UserStatusEnum.ENABLE.getStatus()); - user.setAvatar(Constants.DEFAULT_USER_AVATAR); + Date curDate = new Date(); + user.setJoinTime(curDate); + user.setLastLoginTime(curDate); + user.setStatus(UserStatusEnum.ENABLE.getStatus()); + user.setAvatar(Constants.DEFAULT_USER_AVATAR); } else { user.setLastUseDeviceId(userInfo.getLastUseDeviceId()); user.setLastUseDeviceBrand(userInfo.getLastUseDeviceBrand()); @@ -211,51 +211,58 @@ public class UserInfoService { } user.setLastLoginTime(new Date()); userInfoDao.save(user); - return Constants.TOKEN_PREFIX+jwtUtil.createJWT(user.getId(),user.getNickName(),Constants.TOKEN_ROLE_APP_USER,user.getAvatar()); + return Constants.TOKEN_PREFIX + + jwtUtil.createJWT(user.getId(), user.getNickName(), Constants.TOKEN_ROLE_APP_USER, user.getAvatar()); } public List orderByField(List idList) { return userInfoDao.orderByField(idList); } + public List cmsOrderByField(List idList) { + return orderByField(idList).stream().map(this::updateAvatarPath).collect(Collectors.toList()); + } + public List getSimpleUserOrderByField(List idList) { return userInfoDao.getSimpleUserOrderByField(idList); } public Page findSearch(UserQueryReq queryReq) { Specification specification = createUserInfoSpecification(queryReq); - PageRequest pageRequest = PageRequest.of(queryReq.getPageNum()-1, queryReq.getPageSize()); + PageRequest pageRequest = PageRequest.of(queryReq.getPageNum() - 1, queryReq.getPageSize()); Page userInfoPage = userInfoDao.findAll(specification, pageRequest); return userInfoPage.map(this::updateAvatarPath); } - + private UserInfo updateAvatarPath(UserInfo userInfo) { - if (null != userInfo.getAvatar()) { + if (null != userInfo && null != userInfo.getAvatar()) { userInfo.setAvatar(Constants.RESOURCE_PREFIX + userInfo.getAvatar()); } return userInfo; } + public Page findSearch(UserQueryReq queryReq, int page, int size) { Specification specification = createUserInfoSpecification(queryReq); PageRequest pageRequest = PageRequest.of(page - 1, size); return userInfoDao.findAll(specification, pageRequest); } + private Specification createUserInfoSpecification(UserQueryReq queryReq) { return (root, criteriaQuery, criteriaBuilder) -> { List predicateList = new ArrayList<>(); - if(StringUtils.isNotBlank(queryReq.getId())){ + if (StringUtils.isNotBlank(queryReq.getId())) { predicateList.add(criteriaBuilder.equal(root.get("id"), queryReq.getId())); } - if(StringUtils.isNotBlank(queryReq.getMobile())){ + if (StringUtils.isNotBlank(queryReq.getMobile())) { predicateList.add(criteriaBuilder.equal(root.get("mobile").as(String.class), queryReq.getMobile())); } - if(StringUtils.isNotBlank(queryReq.getNickName())){ - predicateList.add(criteriaBuilder.like(root.get("nickName"), '%'+queryReq.getNickName()+'%')); + if (StringUtils.isNotBlank(queryReq.getNickName())) { + predicateList.add(criteriaBuilder.like(root.get("nickName"), '%' + queryReq.getNickName() + '%')); } - if(StringUtils.isNotBlank(queryReq.getBadge())){ + if (StringUtils.isNotBlank(queryReq.getBadge())) { predicateList.add(criteriaBuilder.equal(root.get("badge").as(String.class), queryReq.getBadge())); } - if(Objects.nonNull(queryReq.getStatus())){ + if (Objects.nonNull(queryReq.getStatus())) { predicateList.add(criteriaBuilder.equal(root.get("status").as(Integer.class), queryReq.getStatus())); } /* @@ -263,12 +270,12 @@ public class UserInfoService { * predicateList.add(criteriaBuilder.equal(root.get("onlineStatus").as(Integer. * class), queryReq.getOnlineStatus())); } */ - - if(Objects.nonNull(queryReq.getCreateDateStart())){ + + if (Objects.nonNull(queryReq.getCreateDateStart())) { predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("joinTime").as(Date.class), queryReq.getCreateDateStart())); } - if(Objects.nonNull(queryReq.getCreateDateEnd())){ + if (Objects.nonNull(queryReq.getCreateDateEnd())) { predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("joinTime").as(Date.class), queryReq.getCreateDateEnd())); } @@ -277,5 +284,8 @@ public class UserInfoService { }; } - + public UserInfo cmsFindById(String id) { + return updateAvatarPath(userInfoDao.getById(id)); + } + }