一、简介
本篇文章要实现的是发送验证码短信功能。
这里我使用的是腾讯云的短信接口,原因很简单,我新注册的腾讯云,有100条短信可以免费使用,哈哈。
注册登录后出现这个页面
往下拉,可以看到下图
根据提示分别申请签名和模板;创建签名选择签名类型为公众号,这样只需要公众号截图就可以了;签名内容填写公众号的全称或简称。
我的模板内容为:您的注册验证码为{1},如非本人操作,请忽略此短信——Liazhan微服务电商。
ok,之后等审核通过。
AppID和App Key在应用列表查看,有个默认应用。
二、实战
该工具提供了在线调用、签名验证、SDK 代码生成和快速检索接口等能力,能显著降低使用云 API 的难度,推荐使用。
使用该工具根据提示输入各个参数,就可以在线生成java代码执行了,超方便。
不过代码的执行需要在项目添加依赖
<!-- 腾讯短信api -->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.38</version>
</dependency>
这里记录一下几个需要注意的地方,
1、个人密匙是构建腾讯云 API 请求的重要凭证,使用腾讯云 API 可以操作您名下的所有腾讯云资源,为了您的财产和服务安全,请妥善保存和定期更换密钥。
2、Region 显示的是必填,但是不需要填
3、PhoneNumberSet 即我们需要发送短信的目的手机号,国内的需要加前缀 +86,完整内容为"+86手机号码"
4、Sign 显示的是选填,但国内短信必填
5、TemplateParamSet 短信模板参数内容;显示的是选填,若模板有参数,则必填,有几个参数就填几个。
ok,现在我们在项目里应用。
在shop-service-impl-weixin模块添加依赖
<!-- 腾讯短信api -->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.38</version>
</dependency>
创建发送短信的工具类
SendSms内容如下:
package com.liazhan.weixin.sms;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @version:V1.0
* @Description: 腾讯短信发送类
* @author: Liazhan
* @date 2020/4/20 9:32
*/
public class SendSms {
static Logger log = LoggerFactory.getLogger(SendSms.class);
/**
* @Author Liazhan
* @Description //TODO 发送短信
* @Date 11:46 2020/4/20
* @Param [phone] 手机号
* @return String 验证码
**/
public static String send(String phone) {
try{
Credential cred = new Credential("这里输入腾讯云secretId", "这里输入腾讯云secretKey");
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("sms.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
SmsClient client = new SmsClient(cred, "", clientProfile);
//生成4位随机数
int code = (int) (Math.random() * 9000 + 1000);
String params = "{\"PhoneNumberSet\":[\"+86"+phone+"\"],\"TemplateID\":\"这里输入短信模板id\",\"Sign\":\"这里输入短信签名内容\",\"TemplateParamSet\":[\""+code+"\"],\"SmsSdkAppid\":\"这里输入SmsSdkAppid\"}";
SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class);
SendSmsResponse resp = client.SendSms(req);
log.info(SendSmsRequest.toJsonString(resp));
return String.valueOf(code);
} catch (TencentCloudSDKException e) {
log.error(e.getMessage());
}
return "";
}
}
这里的短信接口参数没有放在常量类,偷懒了。。。在实际大型的项目中,会搭建一个消息平台,专门处理发送短信、邮箱等业务,后期再看情况优化一下。
修改微信消息处理类MsgHandler
package com.liazhan.weixin.mp.handler;
import com.liazhan.weixin.mp.builder.TextBuilder;
import com.liazhan.weixin.sms.SendSms;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author Binary Wang(https://github.com/binarywang)
*/
@Component
public class MsgHandler extends AbstractHandler {
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
Map<String, Object> context, WxMpService weixinService,
WxSessionManager sessionManager) {
//向公众号发送手机号
String content = wxMessage.getContent();
//向手机号发送短信
String code = SendSms.send(content);
//返回提示
content = "已发送验证码,请注意查收!";
return new TextBuilder().build(content, wxMessage, weixinService);
}
}
依次启动config、eureka、gateway、weixin服务。
向测试公众号发送手机号。
手机也收到了验证码短信。
github项目地址https://github.com/liazhan/shop-project/tree/8b0a71035e19b4e80f6bc31d9d0419c863dfd2e1
版本号为8b0a71035e19b4e80f6bc31d9d0419c863dfd2e1