修改密码、忘记密码接口修改

This commit is contained in:
tqzhang 2025-02-08 13:49:20 +08:00
parent cfd97c03d9
commit 7d3970198a
3 changed files with 119 additions and 84 deletions

View File

@ -29,8 +29,14 @@ public class CustInfoController extends BaseController {
@ApiOperation("小程序修改密码")
@PostMapping({"/change/password"})
public AjaxResult changePassword(@Validated @RequestBody CustChangePasswordDTO content) {
this.custInfoService.custChangePassword(content);
return success();
AjaxResult ajaxResult = new AjaxResult();
try {
ajaxResult = this.custInfoService.custChangePassword(content);
return ajaxResult;
} catch (Exception e) {
log.error("小程序修改密码异常", e);
return AjaxResult.error("小程序修改密码异常");
}
}
@ApiOperation("忘记密码")
@ -38,8 +44,14 @@ public class CustInfoController extends BaseController {
public AjaxResult forgetPassword(@Validated @RequestBody CustForgetPasswordDTO content) {
//content.setMobile(AesEncryptUtil.aesDecode(content.getMobile()));
//content.setNewPassword(AesEncryptUtil.aesDecode(content.getNewPassword()));
this.custInfoService.custForgetPassword(content);
return success();
AjaxResult ajaxResult = new AjaxResult();
try {
ajaxResult = this.custInfoService.custForgetPassword(content);
return ajaxResult;
} catch (Exception e) {
log.error("忘记密码异常", e);
return AjaxResult.error("忘记密码异常");
}
}
@ApiOperation(value = "获取支付二维码")

View File

@ -1,5 +1,6 @@
package com.bonus.core.customer.service;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.core.customer.dto.CustChangePasswordDTO;
import com.bonus.core.customer.dto.CustForgetPasswordDTO;
import com.bonus.core.customer.vo.CustInfoVo;
@ -9,9 +10,9 @@ public interface CustInfoService {
CustInfoVo queryCustInfoDetail(CustInfo custInfo);
void custChangePassword(CustChangePasswordDTO content);
AjaxResult custChangePassword(CustChangePasswordDTO content);
void custForgetPassword(CustForgetPasswordDTO content);
AjaxResult custForgetPassword(CustForgetPasswordDTO content);
String getOrderQRCode(Integer sourceType, String paramValue);

View File

@ -4,6 +4,7 @@ import cn.hutool.core.util.ObjectUtil;
import com.alibaba.nacos.shaded.javax.annotation.Nullable;
import com.bonus.common.core.constant.CacheConstants;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.redis.service.RedisService;
import com.bonus.constant.LeCodeUseSceneEnum;
import com.bonus.constant.LeConstants;
@ -27,18 +28,19 @@ import org.springframework.beans.factory.annotation.Autowired;
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
private RedisService redisService;
@Autowired
@Lazy
private CustCasualApi custCasualApi;
@Autowired
private CustInfoMapper custInfoMapper;
private static final Logger log = LoggerFactory.getLogger(CustInfoServiceImpl.class);
@Autowired
private RedisService redisService;
@Autowired
@Lazy
private CustCasualApi custCasualApi;
@Autowired
private CustInfoMapper custInfoMapper;
// @Autowired
// @Lazy
@ -90,77 +92,97 @@ public class CustInfoServiceImpl implements CustInfoService {
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 {
content.setOldPassword(AesEncryptUtil.aesDecode(content.getOldPassword()));
content.setNewPassword(AesEncryptUtil.aesDecode(content.getNewPassword()));
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 static void main(String[] args) {
BCryptPasswordEncoder bCrypt = new BCryptPasswordEncoder();
boolean flag = bCrypt.matches("Bonus$2026", "$2a$10$vrcmG0TyvgH5tS9g8ptaVOK2K3pYWVAa13SWEK7pQBGRtNAPlGV7O");
System.out.println(flag);
}
public void custForgetPassword(CustForgetPasswordDTO content) {
// content.setMobile(AesEncryptUtil.aesDecode(content.getMobile()));
content.setNewPassword(AesEncryptUtil.aesDecode(content.getNewPassword()));
SmsCodeVerifyDTO smsCodeVerifyDTO = new SmsCodeVerifyDTO();
smsCodeVerifyDTO.setTelephoneNumber(content.getMobile());
smsCodeVerifyDTO.setCode(content.getCode());
if (!verifySmsCode(smsCodeVerifyDTO, CacheConstants.VERIFICATION_CODE)) {
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("修改密码错误:人员不存在:" + custInfo);
throw new ServiceException("修改密码错误:人员不存在", RetCodeEnum.PAY_PERSONAL_NO_EXIT.getKey());
} else {
@Override
public AjaxResult custChangePassword(CustChangePasswordDTO content) {
AjaxResult ajaxResult = new AjaxResult();
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("小程序修改密码错误:人员不存在");
ajaxResult.put("msg", "小程序修改密码错误:人员不存在");
ajaxResult.put("code", "500");
} else {
content.setOldPassword(AesEncryptUtil.aesDecode(content.getOldPassword()));
content.setNewPassword(AesEncryptUtil.aesDecode(content.getNewPassword()));
String oldPassword = content.getOldPassword();
String newPassword = content.getNewPassword();
BCryptPasswordEncoder bCrypt = new BCryptPasswordEncoder();
String password = bCrypt.encode(newPassword);
custInfo.setPwd(password);
custInfoMapper.updateById(custInfo);
}
}
}
if (Objects.equals(oldPassword, newPassword)) {
log.error("小程序修改密码错误:两次密码不能一致");
ajaxResult.put("msg", "小程序修改密码错误:两次密码不能一致");
ajaxResult.put("code", "500");
// throw new ServiceException("小程序修改密码错误:两次密码不能一致");
} else {
BCryptPasswordEncoder bCrypt = new BCryptPasswordEncoder();
if (!bCrypt.matches(oldPassword, custInfo.getPwd())) {
log.error("小程序修改密码错误:原密码不正确");
ajaxResult.put("msg", "小程序修改密码错误:原密码不正确");
ajaxResult.put("code", "500");
// throw new ServiceException("小程序修改密码错误:原密码不正确");
} else {
String password = bCrypt.encode(newPassword);
custInfo.setPwd(password);
custInfoMapper.updateById(custInfo);
this.custCasualApi.updateLoginState(content.getCustId(), content.getOpenid());
ajaxResult.put("msg", "修改成功");
ajaxResult.put("code", "200");
}
}
}
return ajaxResult;
}
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());
}
public static void main(String[] args) {
BCryptPasswordEncoder bCrypt = new BCryptPasswordEncoder();
boolean flag = bCrypt.matches("Bonus$2026", "$2a$10$vrcmG0TyvgH5tS9g8ptaVOK2K3pYWVAa13SWEK7pQBGRtNAPlGV7O");
System.out.println(flag);
}
@Override
public String getOrderQRCode(Integer sourceType, String paramValue) {
String stime = String.valueOf(System.currentTimeMillis() / 1000L);
return "xnzn{\"s\":" + sourceType + ",\"y\":" + LeCodeUseSceneEnum.PAY.key() + ",\"p\":\"" + paramValue + "\",\"t\":" + stime + "}";
}
@Override
public AjaxResult custForgetPassword(CustForgetPasswordDTO content) {
// content.setMobile(AesEncryptUtil.aesDecode(content.getMobile()));
AjaxResult ajaxResult = new AjaxResult();
content.setNewPassword(AesEncryptUtil.aesDecode(content.getNewPassword()));
SmsCodeVerifyDTO smsCodeVerifyDTO = new SmsCodeVerifyDTO();
smsCodeVerifyDTO.setTelephoneNumber(content.getMobile());
smsCodeVerifyDTO.setCode(content.getCode());
if (!verifySmsCode(smsCodeVerifyDTO, CacheConstants.VERIFICATION_CODE)) {
// throw new ServiceException("验证码异常");
ajaxResult.put("msg", "验证码异常");
ajaxResult.put("code", "500");
} else {
CustInfo custInfoQuery = new CustInfo();
custInfoQuery.setMobile(SM4EncryptUtils.sm4Encryptbyconfig(content.getMobile()));
CustInfo custInfo = custInfoMapper.selectOne(custInfoQuery);
if (ObjectUtil.isNull(custInfo)) {
log.error("修改密码错误:人员不存在:" + custInfo);
// throw new ServiceException("修改密码错误:人员不存在", RetCodeEnum.PAY_PERSONAL_NO_EXIT.getKey());
ajaxResult.put("msg", "修改密码错误:人员不存在");
ajaxResult.put("code", "500");
} else {
String newPassword = content.getNewPassword();
BCryptPasswordEncoder bCrypt = new BCryptPasswordEncoder();
String password = bCrypt.encode(newPassword);
custInfo.setPwd(password);
custInfoMapper.updateById(custInfo);
ajaxResult.put("msg", "修改成功");
ajaxResult.put("code", "200");
}
}
return ajaxResult;
}
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());
}
@Override
public String getOrderQRCode(Integer sourceType, String paramValue) {
String stime = String.valueOf(System.currentTimeMillis() / 1000L);
return "xnzn{\"s\":" + sourceType + ",\"y\":" + LeCodeUseSceneEnum.PAY.key() + ",\"p\":\"" + paramValue + "\",\"t\":" + stime + "}";
}
}