阿里大于短信接口调用

短信验证码的速度阿里大于还是值得依赖的,至少在我测试过程中几乎没有大于10分钟的延迟。

先到 阿里大于注册:https://www.alidayu.com/, 获取ALIDAYU_KEY和ALIDAYU_SECRET

再到阿里大于后台创建短信模板,该模板编号在发送短信时会被调用。

public classSmsUtils {

/**

* 调用阿里大于短信平台

*

*@parammobile

*@return

*/

public staticString[]generateCode(String mobile) {

Map codeMap =newHashMap();

intcodeNumber = (int) (Math.random() * (9999-1000+1)) +1000;

String code = String.valueOf(codeNumber);

codeMap.put("code",code);

codeMap.put("product","productName");

String paramJson = JSON.toJSONString(codeMap);

LinkedHashMap responseMap = AliDaYu.smsSend(Constants.REGISTER_TEMPLATE_CODE,mobile,paramJson);

String[] args =newString[2];

args[0] = code;

args[1] = JSONObject.toJSONString(responseMap);

returnargs;

}


public classAliDaYu {

private staticLoggerlogger= LogManager.getLogger(AliDaYu.class);

/**

* 发送短信接口

*

*@paramtemplateCode短信模板号

*@paramphone用户电话号码,多个用 , 号隔开;

*@paramparamJson参数json字符串

*@return状态参数, 200 成功, 400 失败, 500 异常;

*/

public staticLinkedHashMapsmsSend(String templateCode,String phone,String paramJson) {

LinkedHashMap returninfo =newLinkedHashMap();

try{

TaobaoClient client =newDefaultTaobaoClient(Constants.SMS_SEND_URL,Constants.ALIDAYU_KEY,Constants.ALIDAYU_SECRET);

AlibabaAliqinFcSmsNumSendRequest req =newAlibabaAliqinFcSmsNumSendRequest();

req.setSmsType("normal");

req.setSmsFreeSignName("companyName");

req.setSmsParamString(paramJson);

req.setRecNum(phone);

req.setSmsTemplateCode(templateCode);

AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);

LinkedHashMap result = JSON.parseObject(rsp.getBody(),LinkedHashMap.class);

System.out.println("result:"+ JSON.toJSONString(result));

if(result.get("error_response") !=null) {

returninfo.put("status",400);

if("15".equals(((Map) result.get("error_response")).get("code").toString())) {

returninfo.put("msg",("请勿频繁发送"));

}else{

returninfo.put("msg",((Map) result.get("error_response")).get("sub_msg"));

}

logger.error("短信发送失败:phone:"+ phone +",参数:"+ paramJson);

returnreturninfo;

}

if(result.get("err_code") ==null) {

String stmp ="false";

try{

stmp = ((Map) ((Map) result.get("alibaba_aliqin_fc_sms_num_send_response")).get("result")).get("success").toString();

}catch(Exception e) {

stmp ="false";

}

if("true".equals(stmp)) {

returninfo.put("status",200);

returninfo.put("msg","短信发送成功");

logger.info("短信发送成功:phone:"+ phone +",参数:"+ paramJson);

returnreturninfo;

}else{

returninfo.put("status",400);

returninfo.put("msg","短信发送失败,请重试");

logger.error("短信发送失败:phone:"+ phone +",参数:"+ paramJson);

returnreturninfo;

}

}

returninfo.put("status",500);

returninfo.put("msg","短信发送异常,稍后重试");

logger.error("短信发送异常:phone:"+ phone +",参数:"+ paramJson);

returnreturninfo;

}catch(Exception e) {

StringWriter sw =newStringWriter();

PrintWriter pw =newPrintWriter(sw);

e.printStackTrace(pw);

logger.error("短信发送异常,"+ sw.toString());

returninfo.put("status",500);

returninfo.put("msg","短信发送异常,稍后重试");

returnreturninfo;

}

}

/**

* 获取签名字符串

*

*@paramparams

*@return

*/

private staticStringgetSign(Map params,String secretinfo) {

List keys =newArrayList(params.keySet());

Collections.sort(keys);

String prestr = secretinfo;

//按照文档要求,拼接时参数

for(inti =0;i < keys.size();i++) {

String key = keys.get(i);

String value = params.get(key);

prestr = prestr + key + value;

}

prestr += secretinfo;

returnMD5SignUtils.MD5Encode(prestr, null).toUpperCase();

}

你可能感兴趣的:(阿里大于短信接口调用)