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

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

View File

@ -1,5 +1,6 @@
package com.bonus.core.customer.service; 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.CustChangePasswordDTO;
import com.bonus.core.customer.dto.CustForgetPasswordDTO; import com.bonus.core.customer.dto.CustForgetPasswordDTO;
import com.bonus.core.customer.vo.CustInfoVo; import com.bonus.core.customer.vo.CustInfoVo;
@ -9,9 +10,9 @@ public interface CustInfoService {
CustInfoVo queryCustInfoDetail(CustInfo custInfo); 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); 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.alibaba.nacos.shaded.javax.annotation.Nullable;
import com.bonus.common.core.constant.CacheConstants; import com.bonus.common.core.constant.CacheConstants;
import com.bonus.common.core.exception.ServiceException; 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.common.redis.service.RedisService;
import com.bonus.constant.LeCodeUseSceneEnum; import com.bonus.constant.LeCodeUseSceneEnum;
import com.bonus.constant.LeConstants; 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.context.annotation.Lazy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.*; import java.util.*;
@Service @Service
public class CustInfoServiceImpl implements CustInfoService { public class CustInfoServiceImpl implements CustInfoService {
private static final Logger log = LoggerFactory.getLogger(CustInfoServiceImpl.class); private static final Logger log = LoggerFactory.getLogger(CustInfoServiceImpl.class);
@Autowired @Autowired
private RedisService redisService; private RedisService redisService;
@Autowired @Autowired
@Lazy @Lazy
private CustCasualApi custCasualApi; private CustCasualApi custCasualApi;
@Autowired @Autowired
private CustInfoMapper custInfoMapper; private CustInfoMapper custInfoMapper;
// @Autowired // @Autowired
// @Lazy // @Lazy
@ -90,77 +92,97 @@ public class CustInfoServiceImpl implements CustInfoService {
public void custChangePassword(CustChangePasswordDTO content) { @Override
CustInfo custInfoQuery = new CustInfo(); public AjaxResult custChangePassword(CustChangePasswordDTO content) {
custInfoQuery.setCustId(content.getCustId()); AjaxResult ajaxResult = new AjaxResult();
CustInfo custInfo = custInfoMapper.selectOne(custInfoQuery); CustInfo custInfoQuery = new CustInfo();
if (ObjectUtil.isNull(custInfo)) { custInfoQuery.setCustId(content.getCustId());
log.error("小程序修改密码错误:人员不存在:" + String.valueOf(custInfo)); CustInfo custInfo = custInfoMapper.selectOne(custInfoQuery);
throw new ServiceException("小程序修改密码错误:人员不存在"); if (ObjectUtil.isNull(custInfo)) {
} else { log.error("小程序修改密码错误:人员不存在:" + String.valueOf(custInfo));
content.setOldPassword(AesEncryptUtil.aesDecode(content.getOldPassword())); // throw new ServiceException("小程序修改密码错误:人员不存在");
content.setNewPassword(AesEncryptUtil.aesDecode(content.getNewPassword())); ajaxResult.put("msg", "小程序修改密码错误:人员不存在");
String oldPassword = content.getOldPassword(); ajaxResult.put("code", "500");
String newPassword = content.getNewPassword(); } else {
if (Objects.equals(oldPassword, newPassword)) { content.setOldPassword(AesEncryptUtil.aesDecode(content.getOldPassword()));
log.error("小程序修改密码错误:两次密码不能一致"); content.setNewPassword(AesEncryptUtil.aesDecode(content.getNewPassword()));
throw new ServiceException("小程序修改密码错误:两次密码不能一致"); String oldPassword = content.getOldPassword();
} 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 {
String newPassword = content.getNewPassword(); String newPassword = content.getNewPassword();
BCryptPasswordEncoder bCrypt = new BCryptPasswordEncoder(); if (Objects.equals(oldPassword, newPassword)) {
String password = bCrypt.encode(newPassword); log.error("小程序修改密码错误:两次密码不能一致");
custInfo.setPwd(password); ajaxResult.put("msg", "小程序修改密码错误:两次密码不能一致");
custInfoMapper.updateById(custInfo); 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) { public static void main(String[] args) {
String key = cacheKey + smsCodeVerifyDTO.getTelephoneNumber(); BCryptPasswordEncoder bCrypt = new BCryptPasswordEncoder();
String code = redisService.getCacheObject(key); //RedisUtil.getString(key); boolean flag = bCrypt.matches("Bonus$2026", "$2a$10$vrcmG0TyvgH5tS9g8ptaVOK2K3pYWVAa13SWEK7pQBGRtNAPlGV7O");
log.info("redis缓存验证码code : {}", code); System.out.println(flag);
return ObjectUtil.isNotEmpty(code) && ObjectUtil.equal(code, smsCodeVerifyDTO.getCode()); }
}
@Override @Override
public String getOrderQRCode(Integer sourceType, String paramValue) { public AjaxResult custForgetPassword(CustForgetPasswordDTO content) {
String stime = String.valueOf(System.currentTimeMillis() / 1000L); // content.setMobile(AesEncryptUtil.aesDecode(content.getMobile()));
return "xnzn{\"s\":" + sourceType + ",\"y\":" + LeCodeUseSceneEnum.PAY.key() + ",\"p\":\"" + paramValue + "\",\"t\":" + stime + "}"; 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 + "}";
}
} }