短信发送接口 – www.yuntongxun.com

2021-12-08 09:05:21 浏览数 (1)

代码语言:javascript复制
public class RonglianPhoneCodeProvider {


    Logger logger = LoggerFactory.getLogger(this.getClass());
    private String sId = ""; //用户id
    private String appId = ""; //应用id
    private String token = ""; //用户token
    private String respUrl = ""; //语音短信结果回调url
    private boolean sanbox = true; //是否是调试环境

    public void setSanbox(boolean sanbox) {
        this.sanbox = sanbox;
    }

    public void setRespUrl(String respUrl) {
        this.respUrl = respUrl;
    }

    public void setsId(String sId) {
        this.sId = sId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public int sendSmsMsg(String phone, String msgTemplateId, String[] data) {
        HashMap<String, Object> result = null;

        CCPRestSDK restAPI = new CCPRestSDK();
        if (sanbox) {
            restAPI.init("sandboxapp.cloopen.com", "8883");// 初始化服务器地址和端口,格式如下,服务器地址不需要写https://
        } else {
            restAPI.init("app.cloopen.com", "8883");// 初始化服务器地址和端口,格式如下,服务器地址不需要写https://
        }
        restAPI.setAccount(sId, token);// 初始化主帐号和主帐号TOKEN
        restAPI.setAppId(appId);// 初始化应用ID

//        TODO
        result = restAPI.sendTemplateSMS(phone, "1", data);
        if ("000000".equals(result.get("statusCode"))) { //成功
            return 1;
        } else {
            //异常返回输出错误码和错误信息
            String statusCode = result.get("statusCode").toString();
            String statusMsg = result.get("statusMsg").toString();
            this.onSmsMsgFailed(phone, msgTemplateId, statusCode, statusMsg);
            return -1;
        }
    }


    public int sendVoiceNumber(String phone, String number) {

        HashMap<String, Object> result = null;

        CCPRestSDK restAPI = new CCPRestSDK();
        if (sanbox) {
            restAPI.init("sandboxapp.cloopen.com", "8883");// 初始化服务器地址和端口,格式如下,服务器地址不需要写https://
        } else {
            restAPI.init("app.cloopen.com", "8883");// 初始化服务器地址和端口,格式如下,服务器地址不需要写https://
        }
        restAPI.setAccount(sId, token);// 初始化主帐号和主帐号TOKEN
        restAPI.setAppId(appId);// 初始化应用ID


        String times = String.valueOf(3);
        result = restAPI.voiceVerify(String.valueOf(number), phone, null, times, respUrl, "zh", "");
        if ("000000".equals(result.get("statusCode"))) { //成功
            return 1;
        } else {
            //异常返回输出错误码和错误信息
            String statusCode = result.get("statusCode").toString();
            String statusMsg = result.get("statusMsg").toString();
            this.onVoiceNumberFailed(phone, number, statusCode, statusMsg);
            return -1;
        }
    }

    public void onSmsMsgFailed(String phone, String msg, String errorCode, String errorMsg) {
        logger.error("短信【内容】消息["   phone   ","   msg   "] 发送失败("   errorCode   "):"   errorMsg);
        if (errorCode.equals("111142") || errorCode.equals("111158")
                || errorCode.equals("160014")) { //欠费了,需要通知管理员缴费

        }
    }

    public void onVoiceNumberFailed(String phone, String number, String errorCode, String errorMsg) {
        logger.error("语音【验证码】消息["   phone   ","   number   "] 发送失败("   errorCode   "):"   errorMsg);
        if (errorCode.equals("111142") || errorCode.equals("111158")
                || errorCode.equals("160014")) { //欠费了,需要通知管理员缴费

        }
    }
}
com

0 人点赞