短信发送接口-luosimao

0
(0)
public class LuosimaoPhoneCodeProvider {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    private String smsCodeApiKey;
    private String voiceCodeApiKey;

    public void setSmsCodeApiKey(String smsCodeApiKey) {
        this.smsCodeApiKey = smsCodeApiKey;
    }

    public void setVoiceCodeApiKey(String voiceCodeApiKey) {
        this.voiceCodeApiKey = voiceCodeApiKey;
    }

    public int sendSmsMsg(String phone, String msgTemplateId) {
        // just replace key here
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter(
                "api", smsCodeApiKey));
        WebResource webResource = client.resource(
                "http://sms-api.luosimao.com/v1/send.json");
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        formData.add("mobile", phone);
        formData.add("message", msgTemplateId);
        ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).
                post(ClientResponse.class, formData);
        String textEntity = response.getEntity(String.class);
        try {
            JsonNode jsonNode = JsonUtils.nonDefaultMapper().getMapper().readTree(textEntity);
            if (jsonNode.has("error") && jsonNode.get("error").intValue() == 0) {
                return 1;
            } else {
                if (jsonNode.has("error")) {
                    String error = jsonNode.get("error").asText();
                    String errorMsg = jsonNode.get("msg").textValue();
                    onSmsMsgFailed(phone, msgTemplateId, error, errorMsg);
                }
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("短信【内容】消息[" + phone + "," + msgTemplateId + "] 发送失败,接口返回:" + textEntity, e);

        }
        return -1;
    }


    public int sendVoiceNumber(String phone, String number) {
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter(
                "api", voiceCodeApiKey));
        WebResource webResource = client.resource(
                "http://voice-api.luosimao.com/v1/verify.json");
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        formData.add("mobile", phone);
        formData.add("code", number);
        ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).
                post(ClientResponse.class, formData);
        String textEntity = response.getEntity(String.class);
        try {
            JsonNode jsonNode = JsonUtils.nonDefaultMapper().getMapper().readTree(textEntity);
            if (jsonNode.has("error") && jsonNode.get("error").intValue() == 0) {
                return 1;
            } else {
                if (jsonNode.has("error")) {
                    String error = jsonNode.get("error").asText();
                    String errorMsg = jsonNode.get("msg").textValue();
                    onVoiceNumberFailed(phone, number, error, errorMsg);
                }
            }
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("语音【验证码】消息[" + phone + "," + number + "] 发送失败,接口返回:" + textEntity, e);
        }
        return -1;
    }

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

        }
    }

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

        }
    }

    /**
     * 查询第三方短信验证码接口的状态
     *
     * @return
     */
    private String smsApiStatus() {
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter(
                "api", smsCodeApiKey));
        WebResource webResource = client.resource("http://sms-api.luosimao.com/v1/status.json");
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        ClientResponse response = webResource.get(ClientResponse.class);
        String textEntity = response.getEntity(String.class);
        int status = response.getStatus();
        //System.out.print(status);
        //System.out.print(textEntity);
        return textEntity;
    }

    /**
     * 查询第三方语音验证码接口的状态
     *
     * @return
     */
    private String voiceApiStatus() {
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter(
                "api", voiceCodeApiKey));
        WebResource webResource = client.resource("http://voice-api.luosimao.com/v1/status.json");
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        ClientResponse response = webResource.get(ClientResponse.class);
        String textEntity = response.getEntity(String.class);
        int status = response.getStatus();
        //System.out.print(status);
        //System.out.print(textEntity);
        return textEntity;
    }
}

 

这篇文章有用吗?

平均评分 0 / 5. 投票数: 0

到目前为止还没有投票!成为第一位评论此文章。

很抱歉,这篇文章对您没有用!

让我们改善这篇文章!

告诉我们我们如何改善这篇文章?

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据