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; } }
了解 工作生活心情记忆 的更多信息
Subscribe to get the latest posts sent to your email.