前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >短信发送接口-luosimao

短信发送接口-luosimao

作者头像
前Thoughtworks-杨焱
发布2021-12-08 09:05:42
6.6K0
发布2021-12-08 09:05:42
举报
文章被收录于专栏:杨焱的专栏
代码语言:javascript
复制
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;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-10-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档