From 9f0d9622252814eb3f1db5167797aad2033eada0 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Mon, 6 Feb 2023 11:25:16 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E7=94=A8=E6=88=B7=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E7=B1=BB=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xkrs/common/account/JwtLoginFilter.java | 4 +-- .../account/TokenAuthenticationService.java | 4 +-- .../xkrs/controller/SysUserController.java | 36 ++++++++++++------- .../java/com/xkrs/straw/dao/SysUserDao.java | 3 -- .../xkrs/straw/service/SysUserService.java | 5 ++- .../service/impl/SysUserServiceImpl.java | 22 ++++++++---- 6 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/xkrs/common/account/JwtLoginFilter.java b/src/main/java/com/xkrs/common/account/JwtLoginFilter.java index f2bb93e..d5c1b85 100644 --- a/src/main/java/com/xkrs/common/account/JwtLoginFilter.java +++ b/src/main/java/com/xkrs/common/account/JwtLoginFilter.java @@ -2,7 +2,7 @@ package com.xkrs.common.account; import com.xkrs.common.encapsulation.OutputEncapsulation; import com.xkrs.common.encapsulation.PromptMessageEnum; -import com.xkrs.straw.model.vo.SysUserVo; +import com.xkrs.straw.model.entity.SysUserEntity; import com.xkrs.straw.service.SysUserService; import com.xkrs.utils.IpUtil; import org.springframework.context.i18n.LocaleContextHolder; @@ -78,7 +78,7 @@ public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter { } //更新用户登录信息 sysUserService.updateSysUserLogin(auth.getName(), IpUtil.getIpAddr(req)); - SysUserVo userByUserName = sysUserService.getUserByUserName(auth.getName()); + SysUserEntity userByUserName = sysUserService.getUserByUserName(auth.getName()); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Credentials", "false"); response.setContentType("application/json"); diff --git a/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java b/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java index fd3995d..93e23d6 100644 --- a/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java +++ b/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java @@ -2,7 +2,7 @@ package com.xkrs.common.account; import com.xkrs.common.encapsulation.OutputEncapsulation; import com.xkrs.common.encapsulation.PromptMessageEnum; -import com.xkrs.straw.model.vo.SysUserVo; +import com.xkrs.straw.model.entity.SysUserEntity; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.security.Keys; @@ -52,7 +52,7 @@ public class TokenAuthenticationService { * @param userName * @param authorities */ - static void addAuthentication(HttpServletResponse response, String userName, Collection authorities, SysUserVo sysUserEntity) { + static void addAuthentication(HttpServletResponse response, String userName, Collection authorities, SysUserEntity sysUserEntity) { Locale locale = new Locale("zh", "CN"); Map map = new HashMap(3); diff --git a/src/main/java/com/xkrs/controller/SysUserController.java b/src/main/java/com/xkrs/controller/SysUserController.java index 464d93c..5d359ef 100644 --- a/src/main/java/com/xkrs/controller/SysUserController.java +++ b/src/main/java/com/xkrs/controller/SysUserController.java @@ -10,7 +10,6 @@ import com.xkrs.model.qo.SysUserRemarkQo; import com.xkrs.model.qo.SysUserVipLevelQo; import com.xkrs.model.validation.SysUserQoInsert; import com.xkrs.model.validation.SysUserQoUpdate; -import com.xkrs.straw.model.vo.SysUserVo; import com.xkrs.service.RedisService; import com.xkrs.sms.SMSHelper; import com.xkrs.straw.dao.SysUserDao; @@ -22,12 +21,14 @@ import com.xkrs.utils.RandomUtil; import org.apache.hc.core5.util.TextUtils; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.domain.Specification; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import javax.persistence.criteria.Predicate; import javax.servlet.http.HttpServletRequest; import java.util.*; import java.util.concurrent.TimeUnit; @@ -275,11 +276,11 @@ public class SysUserController { if (sysUserEntity == null) { return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale); } - List sysUserVos = sysUserService.selectAllUser(); - if (sysUserVos == null || sysUserVos.size() == 0) { + List sysUserEntityList = sysUserService.selectAllUser(); + if (sysUserEntityList == null || sysUserEntityList.size() == 0) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时还没有任何用户数据", locale); } - return outputEncapsulationObject(PromptMessageEnum.SUCCESS, sysUserVos, locale); + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, sysUserEntityList, locale); } /** @@ -380,15 +381,24 @@ public class SysUserController { */ @GetMapping("/verificationCodeUpdate") public String verificationCodeUpdate(@RequestParam("phone") String phone) throws Exception { -// SysUserVo sysUserVo = sysUserDao.selectUserByUserName(phone); -// if (sysUserVo == null) { -// return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "手机号错误,请使用您注册的手机号", locale); -// } -// List agentOrgList = agentOrgDao.findAll(); -// String targetAgentOrgName = FirePointCodeUtils.getTargetAgentOrgName(sysUserVo.getAgentOrgId(), agentOrgList); -// String optCode = String.valueOf(RandomUtil.returnCode()); -// redisService.set(phone, optCode, 10, TimeUnit.MINUTES); -// smsHelper.dispatchSMSCode(targetAgentOrgName, phone, optCode); + + Specification specification = (root, criteriaQuery, criteriaBuilder) -> { + //查询条件集合 + List predicateList = new ArrayList<>(); + predicateList.add(criteriaBuilder.equal(root.get("userName").as(String.class), phone)); + Predicate[] predicateArray = new Predicate[predicateList.size()]; + return criteriaBuilder.and(predicateList.toArray(predicateArray)); + }; + List sysUserEntityList = sysUserDao.findAll(specification, Sort.by(Sort.Direction.DESC, "id")); + if (sysUserEntityList.size() == 0) { + return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "手机号错误,请使用您注册的手机号", locale); + } + SysUserEntity sysUserEntity = sysUserEntityList.get(0); + List agentOrgList = agentOrgDao.findAll(); + String targetAgentOrgName = FirePointCodeUtils.getTargetAgentOrgName(sysUserEntity.getAgentOrgId(), agentOrgList); + String optCode = String.valueOf(RandomUtil.returnCode()); + redisService.set(phone, optCode, 10, TimeUnit.MINUTES); + smsHelper.dispatchSMSCode(targetAgentOrgName, phone, optCode); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "", locale); } diff --git a/src/main/java/com/xkrs/straw/dao/SysUserDao.java b/src/main/java/com/xkrs/straw/dao/SysUserDao.java index ea758d0..f2db7cc 100644 --- a/src/main/java/com/xkrs/straw/dao/SysUserDao.java +++ b/src/main/java/com/xkrs/straw/dao/SysUserDao.java @@ -1,6 +1,5 @@ package com.xkrs.straw.dao; -import com.xkrs.straw.model.vo.SysUserVo; import com.xkrs.straw.model.entity.SysUserEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; @@ -9,8 +8,6 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Component; -import java.util.List; - @Component public interface SysUserDao extends JpaRepository, JpaSpecificationExecutor { diff --git a/src/main/java/com/xkrs/straw/service/SysUserService.java b/src/main/java/com/xkrs/straw/service/SysUserService.java index 67b5062..8286ea3 100644 --- a/src/main/java/com/xkrs/straw/service/SysUserService.java +++ b/src/main/java/com/xkrs/straw/service/SysUserService.java @@ -1,7 +1,6 @@ package com.xkrs.straw.service; import com.xkrs.model.qo.SysUserQo; -import com.xkrs.straw.model.vo.SysUserVo; import com.xkrs.straw.model.entity.SysUserEntity; import javax.servlet.http.HttpServletRequest; @@ -81,7 +80,7 @@ public interface SysUserService { * * @return */ - List selectAllUser(); + List selectAllUser(); /** * 启用 @@ -124,7 +123,7 @@ public interface SysUserService { * @param userName * @return */ - SysUserVo getUserByUserName(String userName); + SysUserEntity getUserByUserName(String userName); /** * 按天数修改账号到期时间 diff --git a/src/main/java/com/xkrs/straw/service/impl/SysUserServiceImpl.java b/src/main/java/com/xkrs/straw/service/impl/SysUserServiceImpl.java index ba43ae4..03f69b0 100644 --- a/src/main/java/com/xkrs/straw/service/impl/SysUserServiceImpl.java +++ b/src/main/java/com/xkrs/straw/service/impl/SysUserServiceImpl.java @@ -4,7 +4,6 @@ import com.xkrs.common.encapsulation.PromptMessageEnum; import com.xkrs.dao.StreetDao; import com.xkrs.model.entity.StreetEntity; import com.xkrs.model.qo.SysUserQo; -import com.xkrs.straw.model.vo.SysUserVo; import com.xkrs.service.RedisService; import com.xkrs.straw.dao.RelRoleAuthorityDao; import com.xkrs.straw.dao.RelUserRoleDao; @@ -18,11 +17,14 @@ import com.xkrs.straw.service.SysUserService; import com.xkrs.utils.DateTimeUtil; import org.apache.http.util.TextUtils; import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.domain.Specification; import org.springframework.security.crypto.keygen.KeyGenerators; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import javax.persistence.criteria.Predicate; import javax.servlet.http.HttpServletRequest; import java.time.LocalDateTime; import java.util.ArrayList; @@ -279,9 +281,8 @@ public class SysUserServiceImpl implements SysUserService { * @return */ @Override - public List selectAllUser() { -// return sysUserDao.selectAll(); - return null; + public List selectAllUser() { + return sysUserDao.findAll(Sort.by(Sort.Direction.DESC, "id")); } /** @@ -351,9 +352,16 @@ public class SysUserServiceImpl implements SysUserService { } @Override - public SysUserVo getUserByUserName(String userName) { -// return sysUserDao.selectUserByUserName(userName); - return null; + public SysUserEntity getUserByUserName(String userName) { + Specification specification = (root, criteriaQuery, criteriaBuilder) -> { + //查询条件集合 + List predicateList = new ArrayList<>(); + predicateList.add(criteriaBuilder.equal(root.get("userName").as(String.class), userName)); + Predicate[] predicateArray = new Predicate[predicateList.size()]; + return criteriaBuilder.and(predicateList.toArray(predicateArray)); + }; + List sysUserEntityList = sysUserDao.findAll(specification, Sort.by(Sort.Direction.DESC, "id")); + return sysUserEntityList.get(0); } /**