Java对接容联云通讯发短信功能

 

 

/**
 * 发短信所需常量类
 * @author asus
 *
 */
public class Constant {
    
    static String SMS_YTX_URL = "appsms.cloopen.com";    //容联的appsms的url是固定不变的
    
    static String SMS_YTX_PORT = "8883";    //端口号,也是不变的
    
    static String SMS_YTX_SID = "8a216.....24d0cef";    //容联主账户的ACCOUNT SID
    
    static String SMS_YTX_TOKEN = "f95da.....389bbb";    //容联主账户的AUTH TOKEN
    
    static String SMS_YTX_APPID = "8a216d.....aa0cf6";    //正在开发的应用的 APP ID
    
    static String SMS_YTX_VALID_MIN = "5";    //验证码有效时间 可以自己设置,分钟为单位
    
    static String  SMS_YTX_TEMPID = "485746";    //短信模板ID
    

}
public class ResponseObj {
    /**
     * 总记录数
     */
    private int total;
    /**
     * 当前记录集合
     */
    private Object data;
    /**
     * 是否成功
     */
    private boolean successful;
    /**
     * 结果消息
     */
    private String resultMsg;
    /**
     * 错误类型
     */
    private String type;
    /**
     * 添加数据
     * @param data
     * @return
     */
    public static ResponseObj successResult(Object data) {
        return new ResponseObj(1,data,true, "", "");
    }
    /**
     * 添加数据
     * @param data
     * @return
     */
    public static ResponseObj successResult(Object data,int total) {
        return new ResponseObj(total,data,true, "", "");
    }
    /**
     * 返回失败信息
     * @param exMessage
     * @return
     */
    public static ResponseObj failedResult(String exMessage) {
        return new ResponseObj(0, "", false,exMessage, "error");
    }
    /**
     * 返回失败信息和类型
     * @param exMessage
     * @param type
     * @return
     */
    public static ResponseObj failedResult(String exMessage, String type) {
        return new ResponseObj(0, "",false, exMessage, type);
    }
    /**
     * 构造函数,类型为error
     *
     */
    public ResponseObj() {
        type = "error";
    }
    public ResponseObj(boolean successful,String resultMsg){
        this.successful = successful;
        this.resultMsg = resultMsg;
    }
    public ResponseObj(int total, Object data, boolean successful,String resultMsg, String type) {
        this.total = total;
        this.data = data;
        this.successful = successful;
        this.resultMsg = resultMsg;
        this.type = type;
    }
    /**
     * @return the 总记录数
     */
    public int getTotal() {
        return total;
    }
    /**
     * @param total 总记录数 the total to set
     */
    public void setTotal(int total) {
        this.total = total;
    }
    /**
     * @return the 是否成功
     */
    public boolean isSuccessful() {
        return successful;
    }
    /**
     * @param successful 是否成功
     * the successful to set
     */
    public void setSuccessful(boolean successful) {
        this.successful = successful;
    }
    /**
     * @return the data
     */
    public Object getData() {
        return data;
    }
    /**
     * @param data the data to set
     */
    public void setData(Object data) {
        this.data = data;
    }
    /**
     * @return the 结果消息
     */
    public String getResultMsg() {
        return resultMsg;
    }
    /**
     * @param resultMsg 结果消息
     * the resultMsg to set
     */
    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }
    /**
     * @return the 错误类型
     */
    public String getType() {
        return type;
    }
    /**
     * @param type 错误类型
     * the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

}
import java.util.HashMap;
import java.util.Set;

public class SDKTestSendTemplateSMS {
        
  public static ResponseObj sendRegCodeByYTX(String tel,String... verifyName){
        
    ResponseObj obj = new ResponseObj(true, "操作成功");
        
    HashMap<String, Object> result = null;

    //初始化SDK
    CCPRestSDK restAPI = new CCPRestSDK();
        
    restAPI.init(Constant.SMS_YTX_URL, Constant.SMS_YTX_PORT);// 初始化服务器地址和端口,格式如下,服务器地址不需要写https://
        
    restAPI.setAccount(Constant.SMS_YTX_SID, Constant.SMS_YTX_TOKEN);// 初始化主帐号和主帐号TOKEN
        
    restAPI.setAppId(Constant.SMS_YTX_APPID);// 初始化应用ID
        
    String smsValidMin=Constant.SMS_YTX_VALID_MIN;    //验证码过期时间
        
    result = restAPI.sendTemplateSMS(tel, Constant.SMS_YTX_TEMPID, verifyName);
//     result = restAPI.sendTemplateSMS("手机号码","模板id" ,new String[]{});

    System.out.println("SDKTestSendTemplateSMS result=" + result);
        
    if("000000".equals(result.get("statusCode"))){
      //正常返回输出data包体信息(map)
      HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
      Set<String> keySet = data.keySet();
      for(String key:keySet){
        Object object = data.get(key);
        System.out.println(key +" = "+object);
      }
    }else{
      //异常返回输出错误码和错误信息
      System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
      obj.setSuccessful(false);
      obj.setResultMsg(String.valueOf(result.get("statusMsg")));
    }
    return obj;
  }

}
public static void main(String[] args) {
        
  ResponseObj obj = SDKTestSendTemplateSMS.sendRegCodeByYTX("手机号", "呵呵");
  System.out.println(obj.getResultMsg());
        
} 
posted @ 2019-11-26 10:59  抟鹏  阅读(285)  评论(0编辑  收藏  举报