Commit fbf1305f authored by muhong's avatar muhong

修改 发送短信验证码

parent b4ae9d42
package com.wwdz.ch.core.notify;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.xxdxxs.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class AliSmsSender implements SmsSender {
Logger logger = LoggerFactory.getLogger(getClass());
@Value("${dts.notify.alisms.accessKey}")
private String accessKeyId;
@Value("${dts.notify.alisms.secretKey}")
private String accessKeySecret;
@Value("${dts.notify.alisms.url}")
private String url;
@Value("${dts.notify.alisms.templateCode}")
private String templateCode;
//短信API产品名称(短信产品名固定,无需修改)
final String product = "Dysmsapi";
//短信API产品域名(接口地址固定,无需修改)
final String domain = "dysmsapi.aliyuncs.com";
public com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
@Override
public SmsResult send(String phone, String content) {
return null;
}
@Override
public SmsResult sendWithTemplate(String phone, int templateId, String[] params) {
return null;
}
public SmsResult sendWithTemplate(String phone, String verifyCode) {
try {
Map<String, Object> map = new HashMap<>();
map.put("msg", verifyCode + "短信验证码,1分钟内有效,请勿向任何人提供或泄露。");
com.aliyun.dysmsapi20170525.Client client = createClient();
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
.setPhoneNumbers(phone)
.setTemplateCode(templateCode)
.setTemplateParam(JsonUtils.fromMap(map))
.setSignName("玩物得志");
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new com.aliyun.teautil.models.RuntimeOptions());
logger.info("短线验证发送结果 : {} ", JsonUtils.from(sendSmsResponse));
if ("OK".equals(sendSmsResponse.getBody().code)) {
SmsResult smsResult = new SmsResult();
smsResult.setSuccessful(true);
smsResult.setResult(verifyCode);
return smsResult;
}
throw new RuntimeException("验证码发送失败");
} catch (Exception e) {
logger.error("AliSmsSender.sendWithTemplate(),mobile={}",phone,e );
throw new RuntimeException("验证码发送失败");
}
}
}
...@@ -37,6 +37,12 @@ dts: ...@@ -37,6 +37,12 @@ dts:
- name: refund - name: refund
templateId: 112233 templateId: 112233
alisms:
accessKey: LTAIehMFG7EhsJY8
secretKey: 1Q3mIc2tYaKT3tw3FCbKGu48AtiWSD
url: http://MQ_INST_1961444063236731_BXc2xu0h.cn-hangzhou.mq-internal.aliyuncs.com:8080
templateCode: SMS_212085019
# 微信模版通知配置 # 微信模版通知配置
# 微信模版用于通知客户或者运营者,注意配置格式;template-name,template-templateId 请参考 NotifyType 枚举值 # 微信模版用于通知客户或者运营者,注意配置格式;template-name,template-templateId 请参考 NotifyType 枚举值
wx: wx:
......
...@@ -37,6 +37,12 @@ dts: ...@@ -37,6 +37,12 @@ dts:
- name: refund - name: refund
templateId: 112233 templateId: 112233
alisms:
accessKey: LTAIehMFG7EhsJY8
secretKey: 1Q3mIc2tYaKT3tw3FCbKGu48AtiWSD
url: http://MQ_INST_1961444063236731_BXc2xu0h.cn-hangzhou.mq-internal.aliyuncs.com:8080
templateCode: SMS_212085019
# 微信模版通知配置 # 微信模版通知配置
# 微信模版用于通知客户或者运营者,注意配置格式;template-name,template-templateId 请参考 NotifyType 枚举值 # 微信模版用于通知客户或者运营者,注意配置格式;template-name,template-templateId 请参考 NotifyType 枚举值
wx: wx:
......
...@@ -3,8 +3,8 @@ package com.wwdz.ch.wx.impl; ...@@ -3,8 +3,8 @@ package com.wwdz.ch.wx.impl;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.wwdz.ch.core.captcha.CaptchaCodeManager; import com.wwdz.ch.core.captcha.CaptchaCodeManager;
import com.wwdz.ch.core.consts.ResultCode; import com.wwdz.ch.core.consts.ResultCode;
import com.wwdz.ch.core.notify.AliSmsSender;
import com.wwdz.ch.core.notify.NotifyService; import com.wwdz.ch.core.notify.NotifyService;
import com.wwdz.ch.core.notify.NotifyType;
import com.wwdz.ch.core.notify.SmsResult; import com.wwdz.ch.core.notify.SmsResult;
import com.wwdz.ch.core.type.Result; import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.type.UserTypeEnum; import com.wwdz.ch.core.type.UserTypeEnum;
...@@ -61,6 +61,8 @@ public class UserServiceImpl implements UserService { ...@@ -61,6 +61,8 @@ public class UserServiceImpl implements UserService {
@Value("${avatar-url}") @Value("${avatar-url}")
private String avatarUrl; private String avatarUrl;
@Autowired
private AliSmsSender aliSmsSender;
@Transactional @Transactional
...@@ -179,14 +181,14 @@ public class UserServiceImpl implements UserService { ...@@ -179,14 +181,14 @@ public class UserServiceImpl implements UserService {
logger.error("请求验证码出错:{}", WxResponseCode.AUTH_CAPTCHA_UNSUPPORT.desc()); logger.error("请求验证码出错:{}", WxResponseCode.AUTH_CAPTCHA_UNSUPPORT.desc());
return Result.failed(WxResponseCode.AUTH_CAPTCHA_UNSUPPORT.desc()); return Result.failed(WxResponseCode.AUTH_CAPTCHA_UNSUPPORT.desc());
} }
if (!RegexUtil.isMobileExact(dto.getMobile())) {
return Result.failed("手机号格式不正确");
}
String activeProfile = applicationContext.getEnvironment().getActiveProfiles()[0]; String activeProfile = applicationContext.getEnvironment().getActiveProfiles()[0];
String code = "123456"; String code = "123456";
if (!StringUtils.equals("dev", activeProfile)) { if (!StringUtils.equals("dev", activeProfile)) {
if (!RegexUtil.isMobileExact(dto.getMobile())) {
return Result.failed("手机号格式不正确");
}
code = CharUtil.getRandomNum(6); code = CharUtil.getRandomNum(6);
SmsResult smsResult = notifyService.notifySmsTemplate(dto.getMobile(), NotifyType.CAPTCHA, new String[]{code, "1"}); SmsResult smsResult = aliSmsSender.sendWithTemplate(dto.getMobile(), code);
if (smsResult != null) { if (smsResult != null) {
logger.info("*****腾讯云短信发送->请求验证码,短信发送结果:{}", JSONObject.toJSONString(smsResult)); logger.info("*****腾讯云短信发送->请求验证码,短信发送结果:{}", JSONObject.toJSONString(smsResult));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment