一、短信簽名設置
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.完成後調用此方法便可以發送驗證碼。