一、短信签名设置
1、短信签名是什么?
签名是在短信内容开始或者末尾跟的品牌或者应用名称,设置签名有一下几个好处:增加品牌的曝光度,增强用户的记忆让用户能更清楚的知道正在使用的应用。
2、签名可不可以不设置?
如果您不设置签名,您通过接口发送的短信将很可能会进入短信审核流程(注:进入该流程需要短信发送平台的客服人工审核,将会影响短信的发送和到达时间)或者可能直接被网关驳回,所以,为了您平台用户使用短信的稳定性,设置短信签名是十分必要的。
3、短信签名怎么设置?
一般签名设置为您的品牌名、应用名、公司名等有代表性的信息,三到八个字即可。如【信信客】、【淘宝网】。
二、短信模板设置
1、什么是短信模板?
短信模板是对您将要发送的短信进行相似性提取后的内容。举个例子:
A用户在您平台注册会员,需要发送一条短信,内容如下:
您好,您的验证码是:111111
同时,B用户也在您的平台注册会员,发送了如下短信:
您好,您的验证码是:222222
我们提取相似度以后,可以制作以下短信模板:
您好,您的验证码是:${code}
其中,${code}这种使用大括号包围起来的形式,我们称之为变量。您可以按照所在场景定义变量。
短信模板中的变量可以是数字,英文或汉字等
1.实例一:需要传递变量值:${code}=“123456”
2.实例二:需要传递变量值:${nanme}=“李先生”,${order}=“20160506168”,${amount}=“588”。
2、为什么要设置短信模板?
如果不设置短信模板,通过接口发送的短信将很可能会进入短信审核流程(注:进入该流程需要短信发送平台的客服人工审核,将会影响短信的发送和到达时间),或者直接被网关驳回,所以,为了您平台用户使用短信的稳定性,设置模板也是十分必要的。
三、如果不设置短信签名和模板,可以不可以发送短信?
可以发送,但是该短信将会进入人工审核流程,可能会影响您的短信到达时间。所以,我们强烈建议您报备短信签名和模板。
四:关于开发
官方代码样例:

TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
req.setExtend( "" );
req.setSmsType( "normal" );
req.setSmsFreeSignName( "" );
req.setSmsParamString( "" );
req.setRecNum( "13000000000" );
req.setSmsTemplateCode( "" );
AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
System.out.println(rsp.getBody());

项目实际代码:

//service中发送验证码的逻辑
@Override
public Map<String, String> sendSMSMsg(String phone, String msg) throws ApiException {
Map<String, String> map = new HashMap<String, String>();
//开发过程中,设置不发送短信
if (!StringUtils.equals("1", smsSettings.getOpen())) {
map.put("resultCode", "0");
map.put("smsPhone", phone + "");
map.put("smsTime", (new Date()).getTime() + "");
map.put("smsCode", "1234");
return map;
}
TaobaoClient client = new DefaultTaobaoClient(smsSettings.getUrl(), smsSettings.getAppkey(),
smsSettings.getSecret());
AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
Random rnd = new Random();
int code = rnd.nextInt(8999) + 1000;(0到9999四位数验证码)
req.setSmsType(smsSettings.getType());
req.setSmsFreeSignName(smsSettings.getSignName());
req.setSmsParamString("{\"code\":\"" + code + "\",\"product\":\"" + "" + "\"}");//套用模板
req.setRecNum(phone);
req.setSmsTemplateCode(smsSettings.getTplCode());
AlibabaAliqinFcSmsNumSendResponse response = client.execute(req);
logger.info("sendMsm,body: {}", response.getBody());
if (response.getBody().contains("error_response")) {// true
map.put("resultCode", "1");
map.put("resultMsg", "短信发送次数超出限制,请稍后再试。");// 对同一个手机号可发送1条/分钟,7条/小时,50条/天
} else if (response.getResult().getSuccess()) {
map.put("resultCode", "0");
map.put("smsPhone", phone + "");
map.put("smsTime", (new Date()).getTime() + "");
map.put("smsCode", code + "");
}
return map;
}

还有一种使用配置的方式
- 首先配置一个message.properties文件将url,appkey及appsecret配置好。其中url为大于官方固定的值http://gw.api.taobao.com/router/rest,另外两个参数为创建应用时的两个参数。
#短信通知平台的用户名和密码
#阿里大鱼固定url
dayu.url=http://gw.api.taobao.com/router/rest
#应用名(阿里大鱼后台创建的应用)
dayu.appKey=创建应用时获得
#应用密码
dayu.appSecret=创建应用时获得
- 1
- 2
- 3
- 4
- 5
- 6
- 7
通过spring将参数注入到DefaultTaobaoClient中
<bean id="testdayu" class="com.taobao.api.DefaultTaobaoClient">
<constructor-arg index="0" value="${dayu.url}"/>
<constructor-arg index="1" value="${dayu.appKey}"/>
<constructor-arg index="2" value="${dayu.appSecret}"/>
</bean>
- 1
- 2
- 3
- 4
- 5
在具体的实现类中将参数获取到并进行发送验证码:
public class SendVerificationCodeServiceImpl implements SendVerificationCodeService{
@Resource
private SessionFactory sessionFactory;
@Resource
private LogService logService;
//阿里大鱼固定url
private String url;
//应用名
private String appKey;
//应用密码
private String appSecret ;
//通过spring将参数注入后的client将行实例化
@Autowired
private TaobaoClient client = new DefaultTaobaoClient(url, appKey, appSecret);
private AlibabaAliqinFcSmsNumSendRequest req = commonConnect();
/** 用户修改手机号的验证码
* */
@Override
public void updatePhone(String phone,Integer number){
String json = "{\"code\":\""+number+"\",\"product\":\"参数\"}";
req.setSmsParamString(json);
req.setRecNum(phone);
req.setSmsTemplateCode(MessageConstant.UPDATE_PHONE);
try {
AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
} catch (ApiException e) {
logService.saveLog(e.toString(), this.getClass(), 4);
e.printStackTrace();
}
}
/**
* 用户修改密码成功后发送的通知
*/
@Override
public void updateSuccessNotice(String phone,String memberName) {
//该json串中参数名需要与模板中参数一致。
String json = "{\"user\":\""+memberName+"\",\"product\":\"参数\"}";
req.setSmsParamString(json);
req.setSmsTemplateCode("SMS_70145486");//配置的模板id
try {
AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
} catch (ApiException e) {
logService.saveLog(e.toString(), this.getClass(), 4);
e.printStackTrace();
}
}
/**
* 提取公共需要参数
* @return
*/
public AlibabaAliqinFcSmsNumSendRequest commonConnect(){
//以下为大于官方demo中发送文本短信固定写法。
AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
req.setExtend(MessageConstant.COMMON_EXTEND);
req.setSmsType(MessageConstant.SMS_TYPE);
req.setSmsFreeSignName(MessageConstant.SMS_FREE_SIGNNAME);
return req;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
public String getAppSecret() {
return appSecret;
}
public void setAppSecret(String appSecret) {
this.appSecret = appSecret;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
产生验证码部分代码:
Integer code = (int)((Math.random()*9+1)*100000);
- 1
这样产生的6位验证码没有以0开头的,验证码大小在111111-999999之间,防止以0开头出现验证码位数不够的问题。
5.完成后调用此方法便可以发送验证码。
点关注,不迷路
文章每周持续更新,可以微信搜索「 十分钟学编程 」第一时间阅读和催更,如果这个文章写得还不错,觉得有点东西的话 ~求点赞👍 求关注❤️ 求分享❤️
各位的支持和认可,就是我创作的最大动力,我们下篇文章见!