From d8c982d47d26faadd6374ceb079804526762576b Mon Sep 17 00:00:00 2001 From: sxu <602087911@qq.com> Date: Sat, 1 Feb 2025 10:02:19 +0800 Subject: [PATCH] pwd --- .../core/customer/mapper/CustInfoMapper.java | 2 +- .../service/impl/CustInfoServiceImpl.java | 197 +++++++++--------- .../core/notice/notice/v2/api/SmsCodeApi.java | 65 ++++++ 3 files changed, 165 insertions(+), 99 deletions(-) create mode 100644 bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/notice/notice/v2/api/SmsCodeApi.java diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/mapper/CustInfoMapper.java b/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/mapper/CustInfoMapper.java index cc8473ce..4fdb262e 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/mapper/CustInfoMapper.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/mapper/CustInfoMapper.java @@ -8,6 +8,6 @@ import org.apache.ibatis.annotations.Param; public interface CustInfoMapper { CustInfo selectOne(CustInfo custInfo); - int updateById(@Param("custId") Long custId); + int updateById(CustInfo custInfo); } diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/service/impl/CustInfoServiceImpl.java b/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/service/impl/CustInfoServiceImpl.java index 9b056485..02dd0774 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/service/impl/CustInfoServiceImpl.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/customer/service/impl/CustInfoServiceImpl.java @@ -1,98 +1,99 @@ -//package net.xnzn.core.customer.service.impl; -// -//import cn.hutool.core.util.ObjectUtil; -//import com.baomidou.mybatisplus.core.conditions.Wrapper; -//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -//import com.bonus.common.core.exception.ServiceException; -//import net.xnzn.constant.RetCodeEnum; -//import net.xnzn.core.customer.api.CustCasualApi; -//import net.xnzn.core.customer.constants.PersonalStatusEnum; -//import net.xnzn.core.customer.dto.CustChangePasswordDTO; -//import net.xnzn.core.customer.dto.CustForgetPasswordDTO; -//import net.xnzn.core.customer.mapper.CustInfoMapper; -//import net.xnzn.core.customer.service.*; -//import net.xnzn.core.merchant.dto.SmsCodeVerifyDTO; -//import net.xnzn.domain.CustInfo; -//import net.xnzn.utils.AesEncryptUtil; -//import net.xnzn.utils.SM4EncryptUtils; -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.beans.factory.annotation.Value; -//import org.springframework.context.annotation.Lazy; -//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -//import org.springframework.stereotype.Service; -//import java.util.*; -// -//@Service -//public class CustInfoServiceImpl implements CustInfoService { -// private static final Logger log = LoggerFactory.getLogger(CustInfoServiceImpl.class); -// @Autowired -// @Lazy -// private AesEncryptUtil aesEncryptUtil; -// @Autowired -// @Lazy -// private SmsCodeApi smsCodeApi; -// @Autowired -// @Lazy -// private CustCasualApi custCasualApi; -// @Autowired -// private CustInfoMapper custInfoMapper; -// -//// @Value("${customer.h5-login-default-pwd}") -//// private String h5LoginPwd; -// -// public void custChangePassword(CustChangePasswordDTO content) { -// CustInfo custInfoQuery = new CustInfo(); -// custInfoQuery.setCustId(content.getCustId()); -// CustInfo custInfo = custInfoMapper.selectOne(custInfoQuery); -// if (ObjectUtil.isNull(custInfo)) { -// log.error("小程序修改密码错误:人员不存在:" + String.valueOf(custInfo)); -// throw new ServiceException("小程序修改密码错误:人员不存在"); -// } else { -// String oldPassword = content.getOldPassword(); -// String newPassword = content.getNewPassword(); -// if (Objects.equals(oldPassword, newPassword)) { -// log.error("小程序修改密码错误:两次密码不能一致"); -// throw new ServiceException("小程序修改密码错误:两次密码不能一致"); -// } else { -// BCryptPasswordEncoder bCrypt = new BCryptPasswordEncoder(); -// if (!bCrypt.matches(oldPassword, custInfo.getPwd())) { -// log.error("小程序修改密码错误:原密码不正确"); -// throw new ServiceException("小程序修改密码错误:原密码不正确"); -// } else { -// String password = bCrypt.encode(newPassword); -// custInfo.setPwd(password); -// custInfoMapper.updateById(custInfo); -// this.custCasualApi.updateLoginState(content.getCustId(), content.getOpenid()); -// } -// } -// } -// } -// -// public void custForgetPassword(CustForgetPasswordDTO content) { -// content.setMobile(this.aesEncryptUtil.aesDecode(content.getMobile())); -// content.setNewPassword(this.aesEncryptUtil.aesDecode(content.getNewPassword())); -// SmsCodeVerifyDTO smsCodeVerifyDTO = new SmsCodeVerifyDTO(); -// smsCodeVerifyDTO.setTelephoneNumber(content.getMobile()); -// smsCodeVerifyDTO.setCode(content.getCode()); -// if (!this.smsCodeApi.verifySmsCode(smsCodeVerifyDTO)) { -// throw new ServiceException("验证码异常"); -// } else { -// CustInfo custInfoQuery = new CustInfo(); -// custInfoQuery.setMobile(SM4EncryptUtils.sm4Encryptbyconfig(content.getMobile())); -// CustInfo custInfo = custInfoMapper.selectOne(custInfoQuery); -// if (ObjectUtil.isNull(custInfo)) { -// log.error("修改密码错误:人员不存在:" + String.valueOf(custInfo)); -// throw new ServiceException("修改密码错误:人员不存在", RetCodeEnum.PAY_PERSONAL_NO_EXIT.getKey()); -// } else { -// String newPassword = content.getNewPassword(); -// BCryptPasswordEncoder bCrypt = new BCryptPasswordEncoder(); -// String password = bCrypt.encode(newPassword); -// custInfo.setPwd(password); -// custInfoMapper.updateById(custInfo); -// } -// } -// } -// -//} +package net.xnzn.core.customer.service.impl; + +import cn.hutool.core.util.ObjectUtil; +import com.baomidou.mybatisplus.core.conditions.Wrapper; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.bonus.common.core.exception.ServiceException; +import net.xnzn.constant.RetCodeEnum; +import net.xnzn.core.customer.api.CustCasualApi; +import net.xnzn.core.customer.constants.PersonalStatusEnum; +import net.xnzn.core.customer.dto.CustChangePasswordDTO; +import net.xnzn.core.customer.dto.CustForgetPasswordDTO; +import net.xnzn.core.customer.mapper.CustInfoMapper; +import net.xnzn.core.customer.service.*; +import net.xnzn.core.merchant.dto.SmsCodeVerifyDTO; +import net.xnzn.core.notice.notice.v2.api.SmsCodeApi; +import net.xnzn.domain.CustInfo; +import net.xnzn.utils.AesEncryptUtil; +import net.xnzn.utils.SM4EncryptUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Lazy; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.stereotype.Service; +import java.util.*; + +@Service +public class CustInfoServiceImpl implements CustInfoService { + private static final Logger log = LoggerFactory.getLogger(CustInfoServiceImpl.class); + @Autowired + @Lazy + private AesEncryptUtil aesEncryptUtil; + @Autowired + @Lazy + private SmsCodeApi smsCodeApi; + @Autowired + @Lazy + private CustCasualApi custCasualApi; + @Autowired + private CustInfoMapper custInfoMapper; + +// @Value("${customer.h5-login-default-pwd}") +// private String h5LoginPwd; + + public void custChangePassword(CustChangePasswordDTO content) { + CustInfo custInfoQuery = new CustInfo(); + custInfoQuery.setCustId(content.getCustId()); + CustInfo custInfo = custInfoMapper.selectOne(custInfoQuery); + if (ObjectUtil.isNull(custInfo)) { + log.error("小程序修改密码错误:人员不存在:" + String.valueOf(custInfo)); + throw new ServiceException("小程序修改密码错误:人员不存在"); + } else { + String oldPassword = content.getOldPassword(); + String newPassword = content.getNewPassword(); + if (Objects.equals(oldPassword, newPassword)) { + log.error("小程序修改密码错误:两次密码不能一致"); + throw new ServiceException("小程序修改密码错误:两次密码不能一致"); + } else { + BCryptPasswordEncoder bCrypt = new BCryptPasswordEncoder(); + if (!bCrypt.matches(oldPassword, custInfo.getPwd())) { + log.error("小程序修改密码错误:原密码不正确"); + throw new ServiceException("小程序修改密码错误:原密码不正确"); + } else { + String password = bCrypt.encode(newPassword); + custInfo.setPwd(password); + custInfoMapper.updateById(custInfo); + this.custCasualApi.updateLoginState(content.getCustId(), content.getOpenid()); + } + } + } + } + + public void custForgetPassword(CustForgetPasswordDTO content) { + content.setMobile(this.aesEncryptUtil.aesDecode(content.getMobile())); + content.setNewPassword(this.aesEncryptUtil.aesDecode(content.getNewPassword())); + SmsCodeVerifyDTO smsCodeVerifyDTO = new SmsCodeVerifyDTO(); + smsCodeVerifyDTO.setTelephoneNumber(content.getMobile()); + smsCodeVerifyDTO.setCode(content.getCode()); + if (!this.smsCodeApi.verifySmsCode(smsCodeVerifyDTO)) { + throw new ServiceException("验证码异常"); + } else { + CustInfo custInfoQuery = new CustInfo(); + custInfoQuery.setMobile(SM4EncryptUtils.sm4Encryptbyconfig(content.getMobile())); + CustInfo custInfo = custInfoMapper.selectOne(custInfoQuery); + if (ObjectUtil.isNull(custInfo)) { + log.error("修改密码错误:人员不存在:" + String.valueOf(custInfo)); + throw new ServiceException("修改密码错误:人员不存在", RetCodeEnum.PAY_PERSONAL_NO_EXIT.getKey()); + } else { + String newPassword = content.getNewPassword(); + BCryptPasswordEncoder bCrypt = new BCryptPasswordEncoder(); + String password = bCrypt.encode(newPassword); + custInfo.setPwd(password); + custInfoMapper.updateById(custInfo); + } + } + } + +} diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/notice/notice/v2/api/SmsCodeApi.java b/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/notice/notice/v2/api/SmsCodeApi.java new file mode 100644 index 00000000..64a3f6b0 --- /dev/null +++ b/bonus-modules/bonus-smart-canteen/src/main/java/net/xnzn/core/notice/notice/v2/api/SmsCodeApi.java @@ -0,0 +1,65 @@ +package net.xnzn.core.notice.notice.v2.api; + +import cn.hutool.core.util.ObjectUtil; +import com.bonus.common.redis.service.RedisService; +import net.xnzn.core.merchant.dto.SmsCodeVerifyDTO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class SmsCodeApi { + private static final Logger log = LoggerFactory.getLogger(SmsCodeApi.class); + + @Autowired + private RedisService redisService; + +// public void sendSmsCodePost(String telephoneNumber) { +// this.sendSmsCodePost(telephoneNumber, "code_"); +// } + +// public void sendSmsCodePost(String telephoneNumber, String cacheKey) { +// String limitKey = "limit_" + telephoneNumber; +// LocalDateTime now = LocalDateTime.now(); +// LocalDateTime endOfDay = now.with(LocalTime.MAX); +// long expirTime = Math.max(1L, Duration.between(now, endOfDay).getSeconds()); +// String lastSendTimeKey = "last_send_time_" + telephoneNumber; +// String lastSendTime = RedisUtil.getString(lastSendTimeKey); +// if (lastSendTime != null) { +// long lastSendTimestamp = Long.parseLong(lastSendTime); +// long currentTimestamp = System.currentTimeMillis(); +// long timeElapsed = currentTimestamp - lastSendTimestamp; +// if (timeElapsed < 60000L) { +// throw new LeException(I18n.getMessage("notice_verify_repeat_sms_code_exception", new Object[]{60})); +// } +// } +// +// Integer times = RedisUtil.incr(limitKey, expirTime); +// if (times > 5) { +// throw new LeException(I18n.getMessage("notice_verify_limit_sms_code_time", new Object[0])); +// } else { +// int code = (int)((Math.random() * 9.0 + 1.0) * 100000.0); +// String codeString = "" + code; +// Map maps = new HashMap(); +// maps.put("telephoneNumber", telephoneNumber); +// maps.put("sendCode", codeString); +// log.info("验证码发送code : {}", codeString); +// MqUtil.send(JSON.toJSONString(maps), LeMqConstant.Topic.NOTICE_VERIFICATION_CODE); +// String key = cacheKey + telephoneNumber; +// RedisUtil.setString(key, codeString, 300L); +// RedisUtil.setString(lastSendTimeKey, String.valueOf(System.currentTimeMillis())); +// } +// } + + public boolean verifySmsCode(SmsCodeVerifyDTO smsCodeVerifyDTO) { + return this.verifySmsCode(smsCodeVerifyDTO, "code_"); + } + + public boolean verifySmsCode(SmsCodeVerifyDTO smsCodeVerifyDTO, String cacheKey) { + String key = cacheKey + smsCodeVerifyDTO.getTelephoneNumber(); + String code = redisService.getCacheObject(key); //RedisUtil.getString(key); + log.info("redis缓存验证码code : {}", code); + return ObjectUtil.isNotEmpty(code) && ObjectUtil.equal(code, smsCodeVerifyDTO.getCode()); + } +}