diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java index 5d50714ce..2c0d49307 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java @@ -5,10 +5,8 @@ import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated; import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.*; -import cn.iocoder.yudao.userserver.modules.system.enums.sms.SysSmsSceneEnum; import cn.iocoder.yudao.userserver.modules.system.service.auth.SysAuthService; import cn.iocoder.yudao.userserver.modules.system.service.sms.SysSmsCodeService; -import com.alibaba.fastjson.JSON; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -18,7 +16,6 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; -import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; @@ -57,19 +54,12 @@ public class SysAuthController { } @PostMapping("/send-sms-code") - @ApiOperation(value = "发送手机验证码",notes = "不检测该手机号是否已被注册") + @ApiOperation(value = "发送手机验证码") public CommonResult sendSmsCode(@RequestBody @Valid SysAuthSendSmsReqVO reqVO) { smsCodeService.sendSmsCode(reqVO.getMobile(), reqVO.getScene(), getClientIP()); return success(true); } - @PostMapping("/send-sms-new-code") - @ApiOperation(value = "发送手机验证码",notes = "检测该手机号是否已被注册,用于修改手机时使用") - public CommonResult sendSmsNewCode(@RequestBody @Valid SysAuthSendSmsReqVO reqVO) { - smsCodeService.sendSmsNewCode(reqVO); - return success(true); - } - @GetMapping("/send-sms-code-login") @ApiOperation(value = "向已登录用户发送验证码",notes = "修改手机时验证原手机号使用") public CommonResult sendSmsCodeLogin() { diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/enums/sms/SysSmsSceneEnum.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/enums/sms/SysSmsSceneEnum.java index c2156d218..9cea09230 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/enums/sms/SysSmsSceneEnum.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/enums/sms/SysSmsSceneEnum.java @@ -15,15 +15,26 @@ import java.util.Arrays; @AllArgsConstructor public enum SysSmsSceneEnum implements IntArrayValuable { - LOGIN_BY_SMS(1, "手机号登陆"), - CHANGE_MOBILE_BY_SMS(2, "更换手机号"), - FORGET_MOBILE_BY_SMS(3, "忘记密码"), - CHECK_CODE_BY_SMS(4, "审核验证码"), + LOGIN_BY_SMS(1,SysSmsTemplateCodeConstants.USER_SMS_LOGIN, "手机号登陆"), + CHANGE_MOBILE_BY_SMS(2,SysSmsTemplateCodeConstants.USER_SMS_UPDATE_MOBILE, "更换手机号"), + FORGET_MOBILE_BY_SMS(3,SysSmsTemplateCodeConstants.USER_SMS_RESET_PASSWORD, "忘记密码"), ; public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SysSmsSceneEnum::getScene).toArray(); + /** + * 验证那场景编号 + */ private final Integer scene; + + /** + * 模版编码 + */ + private final String code; + + /** + * 描述 + */ private final String name; @Override @@ -31,4 +42,13 @@ public enum SysSmsSceneEnum implements IntArrayValuable { return ARRAYS; } + public static String getCodeByScene(Integer scene){ + for (SysSmsSceneEnum value : values()) { + if (value.getScene().equals(scene)){ + return value.getCode(); + } + } + return null; + } + } diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/enums/sms/SysSmsTemplateCodeConstants.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/enums/sms/SysSmsTemplateCodeConstants.java index 5529a0741..8a83f75f2 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/enums/sms/SysSmsTemplateCodeConstants.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/enums/sms/SysSmsTemplateCodeConstants.java @@ -12,4 +12,14 @@ public interface SysSmsTemplateCodeConstants { */ String USER_SMS_LOGIN = "user-sms-login"; + /** + * 用户忘记密码 + */ + String USER_SMS_RESET_PASSWORD = "user-sms-reset-password"; + + /** + * 用户更新手机号 + */ + String USER_SMS_UPDATE_MOBILE = "user-sms-update-mobile"; + } diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/sms/SysSmsCodeService.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/sms/SysSmsCodeService.java index 8a052c76d..281f18ce6 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/sms/SysSmsCodeService.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/sms/SysSmsCodeService.java @@ -2,7 +2,6 @@ package cn.iocoder.yudao.userserver.modules.system.service.sms; import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.iocoder.yudao.framework.common.validation.Mobile; -import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthSendSmsReqVO; import cn.iocoder.yudao.userserver.modules.system.dal.dataobject.sms.SysSmsCodeDO; import cn.iocoder.yudao.userserver.modules.system.enums.sms.SysSmsSceneEnum; @@ -22,13 +21,6 @@ public interface SysSmsCodeService { */ void sendSmsCode(@Mobile String mobile, Integer scene, String createIp); - /** - * 发送短信验证码,并检测手机号是否已被注册 - * - * @param reqVO 请求实体 - */ - void sendSmsNewCode(SysAuthSendSmsReqVO reqVO); - /** * 验证短信验证码,并进行使用 * 如果正确,则将验证码标记成已使用 diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/sms/impl/SysSmsCodeServiceImpl.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/sms/impl/SysSmsCodeServiceImpl.java index d0fd64dfe..80fc15a8a 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/sms/impl/SysSmsCodeServiceImpl.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/sms/impl/SysSmsCodeServiceImpl.java @@ -4,11 +4,9 @@ import cn.hutool.core.map.MapUtil; import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO; import cn.iocoder.yudao.coreservice.modules.system.service.sms.SysSmsCoreService; import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService; -import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthSendSmsReqVO; import cn.iocoder.yudao.userserver.modules.system.dal.dataobject.sms.SysSmsCodeDO; import cn.iocoder.yudao.userserver.modules.system.dal.mysql.sms.SysSmsCodeMapper; import cn.iocoder.yudao.userserver.modules.system.enums.sms.SysSmsSceneEnum; -import cn.iocoder.yudao.userserver.modules.system.enums.sms.SysSmsTemplateCodeConstants; import cn.iocoder.yudao.userserver.modules.system.framework.sms.SmsCodeProperties; import cn.iocoder.yudao.userserver.modules.system.service.sms.SysSmsCodeService; import org.springframework.stereotype.Service; @@ -47,22 +45,29 @@ public class SysSmsCodeServiceImpl implements SysSmsCodeService { public void sendSmsCode(String mobile, Integer scene, String createIp) { // 创建验证码 String code = this.createSmsCode(mobile, scene, createIp); + + // 获取发送模板 + String codeTemplate = SysSmsSceneEnum.getCodeByScene(scene); + + // 如果是更换手机号发送验证码,则需要检测手机号是否被注册 + if (SysSmsSceneEnum.CHANGE_MOBILE_BY_SMS.getScene().equals(scene)){ + this.checkMobileIsRegister(mobile,scene); + } + // 发送验证码 - // TODO @宋天:这里可以拓展下 SysSmsSceneEnum,支持设置对应的短信模板编号(不同场景的短信文案是不同的)、是否要校验手机号已经注册。这样 Controller 就可以收口成一个接口了。相当于说,不同场景,不同策略 - smsCoreService.sendSingleSmsToMember(mobile, null, SysSmsTemplateCodeConstants.USER_SMS_LOGIN, + smsCoreService.sendSingleSmsToMember(mobile, null, codeTemplate, MapUtil.of("code", code)); } - @Override - public void sendSmsNewCode(SysAuthSendSmsReqVO reqVO) { + public void checkMobileIsRegister(String mobile, Integer scene) { // 检测手机号是否已被使用 - MbrUserDO userByMobile = mbrUserService.getUserByMobile(reqVO.getMobile()); + MbrUserDO userByMobile = mbrUserService.getUserByMobile(mobile); if (userByMobile != null){ throw exception(USER_SMS_CODE_IS_EXISTS); } // 发送短信 - this.sendSmsCode(reqVO.getMobile(),reqVO.getScene(),getClientIP()); + this.sendSmsCode(mobile,scene,getClientIP()); } private String createSmsCode(String mobile, Integer scene, String ip) {