| | |
| | | import com.yeshi.buwan.dto.config.TencentSMSConfig; |
| | | import com.yeshi.buwan.exception.SMSException; |
| | | import com.yeshi.buwan.service.inter.SMSService; |
| | | import com.yeshi.buwan.service.inter.system.SystemConfigService; |
| | | import com.yeshi.buwan.util.Constant; |
| | | import com.yeshi.buwan.util.RedisKeyEnum; |
| | | import com.yeshi.buwan.util.RedisManager; |
| | |
| | | @Resource |
| | | private RedisManager redisManager; |
| | | |
| | | @Override |
| | | public void sendBindVCode(String uid, String phone, int codeLength) throws SMSException { |
| | | String limitKey = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSLIMIT, uid + ""); |
| | | String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + 0); |
| | | if (!StringUtil.isNullOrEmpty(redisManager.getCommonString(limitKey))) |
| | | throw new SMSException(1001, "请过60秒再试"); |
| | | @Resource |
| | | private DetailSystemConfigService detailSystemConfigService; |
| | | |
| | | private void sendSMSCode(String phone, String msg) throws SMSException { |
| | | |
| | | String msgCode = StringUtil.getNumberVerifyCode(codeLength); |
| | | |
| | | TencentSMSConfig tencentSMSConfig = Constant.tencentSMSConfig; |
| | | |
| | | // 验证码模板 |
| | | String msg = tencentSMSConfig.getContentBind().replace("[签名]", tencentSMSConfig.getSign()).replace("[验证码]", |
| | | msgCode); |
| | | SmsSingleSenderResult result = TencentSMSUtil.sendSingleMsg(Integer.parseInt(tencentSMSConfig.getAppId()), tencentSMSConfig.getAppKey(), phone, msg); |
| | | if (result == null) |
| | | throw new SMSException(2, "短信发送失败"); |
| | |
| | | } else if (result.result != 0) {// 发送失败 |
| | | throw new SMSException(result.result, "短信发送失败"); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void sendBindVCode(String uid, String phone, int codeLength) throws SMSException { |
| | | String limitKey = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSLIMIT, uid + ""); |
| | | String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + 0); |
| | | if (!StringUtil.isNullOrEmpty(redisManager.getCommonString(limitKey))) |
| | | throw new SMSException(1001, "请过60秒再试"); |
| | | |
| | | String msgCode = StringUtil.getNumberVerifyCode(codeLength); |
| | | // 验证码模板 |
| | | TencentSMSConfig tencentSMSConfig = Constant.tencentSMSConfig; |
| | | String msg = tencentSMSConfig.getContentBind().replace("[签名]", tencentSMSConfig.getSign()).replace("[验证码]", |
| | | msgCode); |
| | | sendSMSCode(phone, msg); |
| | | //保存验证码 |
| | | redisManager.cacheCommonString(key, msgCode, 60 * 5); |
| | | //60s后再发送 |
| | |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public void sendLoginVCode(String phone, int codeLength, String detailSystemId, int version) throws SMSException { |
| | | String limitKey = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSLIMIT, phone); |
| | | String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + 1); |
| | | if (!StringUtil.isNullOrEmpty(redisManager.getCommonString(limitKey))) |
| | | throw new SMSException(1001, "请过60秒再试"); |
| | | |
| | | String msgCode = StringUtil.getNumberVerifyCode(codeLength); |
| | | // 验证码模板 |
| | | TencentSMSConfig tencentSMSConfig = Constant.tencentSMSConfig; |
| | | String msg = tencentSMSConfig.getContentBind().replace("[签名]", detailSystemConfigService.getConfigValueByKey("tencent_sms_sign", detailSystemId, version)).replace("[验证码]", |
| | | msgCode); |
| | | sendSMSCode(phone, msg); |
| | | //保存验证码 |
| | | redisManager.cacheCommonString(key, msgCode, 60 * 5); |
| | | //60s后再发送 |
| | | redisManager.cacheCommonString(limitKey, "1", 60); |
| | | } |
| | | |
| | | @Override |
| | | public boolean verifyLoginVCode(String phone, String code, String detailSystemId, int version) { |
| | | |
| | | String value = detailSystemConfigService.getConfigValueByKey("test_phone_account", detailSystemId, version); |
| | | |
| | | //测试账号 |
| | | if (value != null) { |
| | | if (phone.equalsIgnoreCase(value.split("#")[0]) && code.equalsIgnoreCase(value.split("#")[1])) { |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | |
| | | String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + 1); |
| | | String cacheCode = redisManager.getCommonString(key); |
| | | if (cacheCode != null && cacheCode.equalsIgnoreCase(code)) { |
| | | redisManager.removeCommonString(key); |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | } |