开发指南:http://www.yuntongxun.com/doc/ready/about/1_1_1_1.html
sdk文档:http://www.yuntongxun.com/doc/sdk/sdkabout/2_1_1_1.html
rest API :http://www.yuntongxun.com/doc/rest/restabout/3_1_1_1.html
IVR API :http://www.yuntongxun.com/doc/ivr/ivrabout/4_1_1_1.html
/** * 2>向手机发送验证码 * @param request( telephone 手机号码) * @return 向前台返回的结果 */ public void sendSmsIdentifyingCode() { String phone_no =getRequest().getParameter("phone_no"); String templateId =getRequest().getParameter("templateId"); //request.getParameter("phone_no"); //设置多少分钟内短信有效(单位:分钟) int timeOutMin = 5; String randomInt = creatNum()+"";//生成验证随机数 ObjectNode smsResult =sendInfo(phone_no, timeOutMin, randomInt , templateId );//电话号码--有效时间--验证码随机数--模板ID System.out.println(randomInt); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html"); PrintWriter out = null; try { out = response.getWriter(); } catch (IOException e) { e.printStackTrace(); } if (smsResult == null) { out.println(ResultMessageUtils.creatJsonResultMessage(CoreStatus.RESULT_900009)); } else if (smsResult.get("statusCode").textValue().equals("000000")) { Map<String, String> telephoneCode = new HashMap<>(); String code = smsResult.get("randomNum").asText().replace("\"", ""); telephoneCode.put("code", code); telephoneCode.put("time", String.valueOf(DateUtils.getSystemDate().getTime())); telephoneCode.put("message", "Success"); // 存入Redis中 cacheClient.setObject("tel" + phone_no, telephoneCode); out.print( ResultMessageUtils.creatJsonResultMessage(CoreStatus.RESULT_000000)); } else { ObjectNode result; result = JsonUtils.createObjectNode(); result.put("code", smsResult.get("statusCode").textValue()); result.put("message", smsResult.get("statusMsg").textValue()); result.put("datatime", DateUtils.getDefaultDateTimeToString()); // return result.toString(); out.println(result.toString()); } } /** * 向手机发送信息 * * @param phone_no * @param timeOutMin(可以为空) * @param randomNum * @param msm_type * @param templateId 短信模板Id * @return smsResult */ public ObjectNode sendInfo(String phone_no, Integer timeOutMin, String randomNum,String templateId) { // Map<String, String> telephoneMap = (Map) cacheClient.getObject("tel" + phone_no); List<String> messageSubstitutes = new ArrayList(); if (timeOutMin != null) { messageSubstitutes.add(String.valueOf(randomNum)); messageSubstitutes.add(String.valueOf(timeOutMin)); } List<String> telephoneList = new ArrayList<>(); telephoneList.add(phone_no); String smsResultString = smsService.sendSms(telephoneList, templateId, messageSubstitutes); ObjectNode smsResult = (ObjectNode) JsonUtils.stringToJsonObject(smsResultString); smsResult.put("randomNum", randomNum.replace("\"", "")); return smsResult; } /** * 生成随机验证码(密码) * * @return */ public Integer creatNum() { int randomInt = new Random().nextInt(900000) + 100000;//设置短信发送内容 return randomInt; }
发送信息的service---
import static net.jeeshop.web.action.front.cache.utils.RestfulUtils.POST; import static net.jeeshop.web.action.front.cache.utils.RestfulUtils.callService; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import net.jeeshop.web.action.front.cache.utils.DateUtils; import net.jeeshop.web.action.front.cache.utils.JsonUtils; import net.jeeshop.web.action.front.cache.utils.security.Base64Utils; import net.jeeshop.web.action.front.cache.utils.security.MD5Utils; /** * 发送信息的服务类 */ @Service public class SmsService { /** * 短信网关的服务器地址 */ private String serviceUrl=""; /** * 短信网关的服务器端口 */ private int servicePort=; /** * 短信网关的账户ID */ private String accountId=""; /** * 短信网关的账户认证令牌 */ private String accountAuth=""; /** * 短信网关的应用ID */ private String appId=""; /** * 短信网关的应用认证令牌 */ private String appToken=""; /** * 短信网关的短信模板应用ID */ private String templateId=""; /** * 短信网络组成url段落的第一部分 */ private String urlParagraph1=""; /** * 短信网络组成url段落的第二部分 */ private String urlParagraph2=""; private static final Logger log = LoggerFactory.getLogger(SmsService.class); /** * 向手机发送短信 * * @param telephones 需要接收短信的电话列表 * @param templateId 短信模板 * @param messageSubstitutes 消息模板中需要进行补位的消息列表 * @return 字符串格式的Json返回信息 */ public String sendSms(List<String> telephones, String templateId, List<String> messageSubstitutes) { StringBuilder url = new StringBuilder(serviceUrl); String timestamp = DateUtils.getTimestamp(); url.append(":").append(servicePort).append(urlParagraph1).append(accountId).append(urlParagraph2) .append(MD5Utils.MD5(accountId + accountAuth + timestamp)); ObjectNode message = JsonUtils.getJsonMapper().createObjectNode(); StringBuilder telephoneString = new StringBuilder(); for (String telephone : telephones) { if (telephoneString.length() != 0) { telephoneString.append(","); } telephoneString.append(telephone); } message.put("to", telephoneString.toString()); message.put("appId", appId); message.put("templateId", templateId); ArrayNode datas = message.putArray("datas"); for (String messageSubstitute : messageSubstitutes) { datas.add(messageSubstitute); } HashMap<String, String> header = new HashMap<>(); String authorization = Base64Utils.encode(accountId + ":" + timestamp); header.put("Authorization", authorization); String x= callService(url.toString(), POST, message.toString(), header); try { System.out.println(new String(x.getBytes(), "GBK")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return x; } }