From c0fd7885dc1d5bde003898c81f021decfb94f99d Mon Sep 17 00:00:00 2001 From: "liang.chao" <1360241448@qq.com> Date: Tue, 13 Aug 2024 10:12:37 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9F=AD=E4=BF=A1=E5=8A=9F=E8=83=BD=E5=AE=8C?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/SysSmsServiceImpl.java | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysSmsServiceImpl.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysSmsServiceImpl.java index afe1cd0..ea15c92 100644 --- a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysSmsServiceImpl.java +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysSmsServiceImpl.java @@ -8,7 +8,9 @@ import com.bonus.sgzb.common.core.exception.ServiceException; import com.bonus.sgzb.common.core.utils.GlobalConstants; import com.bonus.sgzb.common.core.web.domain.AjaxResult; import com.bonus.sgzb.common.redis.service.RedisService; +import com.bonus.sgzb.system.api.domain.SysUser; import com.bonus.sgzb.system.config.TencentSmsConfig; +import com.bonus.sgzb.system.mapper.SysUserMapper; import com.bonus.sgzb.system.service.ISysSmsService; import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.exception.TencentCloudSDKException; @@ -43,6 +45,8 @@ public class SysSmsServiceImpl implements ISysSmsService { @Resource private RedisService redisService; + @Resource + private SysUserMapper sysUserMapper; @Resource private TencentSmsConfig tencentSmsConfig; @@ -55,6 +59,7 @@ public class SysSmsServiceImpl implements ISysSmsService { /** * 验收通知短信 + * * @param phone 手机号 * @param msg 内容 * @return @@ -74,11 +79,12 @@ public class SysSmsServiceImpl implements ISysSmsService { } catch (Exception e) { return AjaxResult.error("发送失败:" + e.getMessage()); }*/ - return sendMsgByPhone( phone, msg); + return sendMsgByPhone(phone, msg); } /** * 登录短信验证码 + * * @param phone 手机号 * @return */ @@ -96,12 +102,13 @@ public class SysSmsServiceImpl implements ISysSmsService { } catch (Exception e) { return AjaxResult.error("发送失败:" + e.getMessage()); }*/ - return sendCodeByPhone(phone,null); + return sendCodeByPhone(phone, null); } /** * 发送消息msg到手机 + * * @param phone 手机号码 * @return AjaxResult对象 */ @@ -128,6 +135,7 @@ public class SysSmsServiceImpl implements ISysSmsService { /** * 发送验证码到手机 + * * @param phone 手机号码 * @return AjaxResult对象 */ @@ -136,6 +144,11 @@ public class SysSmsServiceImpl implements ISysSmsService { if (phone == null || phone.length() != UserConstants.PHONE_DEFAULT_LENGTH_LOGIN) { return AjaxResult.error("手机号格式错误,请输入11位数字号码"); } + // 校验是否存在该手机号 + SysUser sysUser = sysUserMapper.checkPhoneUnique(phone); + if (sysUser == null){ + return AjaxResult.error("该手机号未绑定用户,请重新确认"); + } String code = getSixBitCode(); // 校验验证码 if (code.length() != UserConstants.CODE_MIN_LENGTH_LOGIN) { @@ -151,7 +164,8 @@ public class SysSmsServiceImpl implements ISysSmsService { } // 存储验证码至Redis中,键值为:code_15588886157 , 有效期5,时间颗粒度为MINUTES:分钟 redisService.setCacheObject("code_" + phone, code, 5L, TimeUnit.MINUTES); - return success("手机号:" + phone + ",用户登录验证码:" + code + ",返回结果:" + body); +// return success("手机号:" + phone + ",用户登录验证码:" + code + ",返回结果:" + body); + return success("验证码发送成功",body); } catch (Exception e) { return AjaxResult.error("发送失败:" + e.getMessage()); } @@ -195,15 +209,16 @@ public class SysSmsServiceImpl implements ISysSmsService { /** * 腾讯sms短信 + * * @param mobilePhone * @param templateId * @param args * @return * @throws Exception */ - public String sendMessageNew(String mobilePhone,String templateId, String... args) throws Exception { + public String sendMessageNew(String mobilePhone, String templateId, String... args) throws Exception { - try{ + try { // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密 Credential cred = new Credential(tencentSmsConfig.getAccessKeyId(), tencentSmsConfig.getAccessKeySecret()); // 实例化一个http选项,可选的,没有特殊需求可以跳过 @@ -227,7 +242,7 @@ public class SysSmsServiceImpl implements ISysSmsService { phoneNumberSet1[i] = "+86" + phoneNumberSet1[i]; } req.setPhoneNumberSet(phoneNumberSet1);//发送的手机号 - if(null != args && args.length > 0 && Arrays.stream(args) + if (null != args && args.length > 0 && Arrays.stream(args) .noneMatch(s -> s == null || s.trim().isEmpty())) { String[] templateParamSet1 = args;//模板的参数 req.setTemplateParamSet(templateParamSet1);//发送验证码 @@ -239,19 +254,19 @@ public class SysSmsServiceImpl implements ISysSmsService { log.info("腾讯云平台短信发送响应结果:{}", JSONObject.toJSONString(resp)); SendStatus[] sendStatusSet = resp.getSendStatusSet(); List sendStatuses = Arrays.asList(sendStatusSet); - if (CollectionUtils.isNotEmpty(sendStatuses)){ + if (CollectionUtils.isNotEmpty(sendStatuses)) { for (SendStatus sendStatus : sendStatuses) { - if (!"OK".equalsIgnoreCase(sendStatus.getCode())){ - throw new ServiceException(sendStatus.getMessage(),Integer.valueOf(sendStatus.getCode())); + if (!"OK".equalsIgnoreCase(sendStatus.getCode())) { + throw new ServiceException(sendStatus.getMessage(), Integer.valueOf(sendStatus.getCode())); } } } - return resp.getRequestId(); + return resp.getRequestId(); } catch (TencentCloudSDKException e) { e.printStackTrace(); log.error("短信发送失败:{}", e.getMessage()); - throw new ServiceException(e.getMessage(),Integer.valueOf(e.getErrorCode())); + throw new ServiceException(e.getMessage(), Integer.valueOf(e.getErrorCode())); } } }