use of com.xnx3.BaseVO in project xnx3 by xnx3.
the class AliyunSMSUtil method send.
/**
* 发送短信,如
* <pre>sms.send("网市场","SMS_40000000","{\"code\":\"123456\"}","18711111111");</pre>
* @param signName 控制台创建的签名名称(状态必须是验证通过)
* <br/> https://sms.console.aliyun.com/?spm=#/sms/Sign
* @param templateCode 控制台创建的模板CODE(状态必须是验证通过)
* <br/> https://sms.console.aliyun.com/?spm=#/sms/Template
* @param paramString 短信模板中的变量;数字需要转换为字符串;个人用户每个变量长度必须小于15个字符。 例如:短信模板为:“接受短信验证码${no}”,此参数传递{“no”:”123456”},用户将接收到[短信签名]接受短信验证码123456,传入的字符串为JSON格式
* @param phone 接收短信的手机号
* @return {@link BaseVO}
* <ul>
* <li>若成功,返回 {@link BaseVO#SUCCESS},此时可以用 {@link BaseVO#getInfo()} 拿到其requestId</li>
* <li>若失败,返回 {@link BaseVO#FAILURE},此时可以用 {@link BaseVO#getInfo()} 拿到其错误原因(catch的抛出的异常名字)</li>
* </ul>
*/
public BaseVO send(String signName, String templateCode, String templateParamString, String phone) {
BaseVO vo = new BaseVO();
// 可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
// 初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
try {
DefaultProfile.addEndpoint(regionId, regionId, product, domain);
} catch (ClientException e1) {
e1.printStackTrace();
vo.setBaseVO(BaseVO.FAILURE, e1.getMessage());
return vo;
}
IAcsClient acsClient = new DefaultAcsClient(profile);
// 组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
// 必填:待发送手机号
request.setPhoneNumbers(phone);
// 必填:短信签名-可在短信控制台中找到
request.setSignName(signName);
// 必填:短信模板-可在短信控制台中找到
request.setTemplateCode(templateCode);
// 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam(templateParamString);
// hint 此处可能会抛出异常,注意catch
try {
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
vo.setInfo(sendSmsResponse.getRequestId());
} catch (ServerException e) {
vo.setBaseVO(BaseVO.FAILURE, e.getMessage());
e.printStackTrace();
} catch (ClientException e) {
vo.setBaseVO(BaseVO.FAILURE, e.getMessage());
e.printStackTrace();
}
return vo;
}
use of com.xnx3.BaseVO in project xnx3 by xnx3.
the class MNSUtil method createSubScription.
/**
* 创建订阅
* @param topicName 要在哪个主题下创建订阅,这个是主题的名字
* @param subMeta 要创建的订阅的对象
* @return
*/
public BaseVO createSubScription(String topicName, SubscriptionMeta subMeta) {
BaseVO vo = new BaseVO();
CloudTopic topic = getMNSClient().getTopicRef(topicName);
try {
// SubscriptionMeta subMeta = new SubscriptionMeta();
// subMeta.setSubscriptionName("TestSub");
// subMeta.setEndpoint(HttpEndpoint.GenEndpointLocal());
// subMeta.setNotifyContentFormat(SubscriptionMeta.NotifyContentFormat.XML);
// //subMeta.setFilterTag("filterTag"); //设置订阅的filterTag
String subUrl = topic.subscribe(subMeta);
vo.setBaseVO(BaseVO.SUCCESS, subUrl);
} catch (Exception e) {
vo.setBaseVO(BaseVO.FAILURE, e.getMessage());
System.out.println("subscribe/unsubribe error");
e.printStackTrace();
}
return vo;
}
use of com.xnx3.BaseVO in project xnx3 by xnx3.
the class HuaweiSMSUtil method send.
/**
* 发送短信
* @param phone 要发送的手机号。可以传入 +8618788888888 ,也可以不带+86,接口里面会自动加上
* @param templateParas 模板变量。
* 选填,使用无变量模板时请赋空值 String templateParas = "";
* 单变量模板示例:模板内容为"您的验证码是${1}"时,templateParas可填写为"[\"369751\"]"
* 双变量模板示例:模板内容为"您有${1}件快递请到${2}领取"时,templateParas可填写为"[\"3\",\"人民公园正门\"]"
* 模板中的每个变量都必须赋值,且取值不能为空
* 查看更多模板和变量规范:产品介绍>模板和变量规范
* @param templateId 模板ID
* @return {@link BaseVO} result = 1 则是发送成功, =0则是失败,用getInfo() 获取失败原因
*/
public BaseVO send(String phone, String templateId, String templateParas) {
if (phone.indexOf("+") != 0) {
phone = "+86" + phone;
}
// 选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告
String statusCallBack = "";
// 请求Body,不携带签名名称时,signature请填null
String body = buildRequestBody(sender, phone, templateId, templateParas, statusCallBack, signature);
if (null == body || body.isEmpty()) {
return BaseVO.failure("body is null.");
}
// 请求Headers中的X-WSSE参数值
String wsseHeader = buildWsseHeader(appKey, appSecret);
if (null == wsseHeader || wsseHeader.isEmpty()) {
return BaseVO.failure("wsse header is null.");
}
// 如果JDK版本低于1.8,可使用如下代码
// 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
// CloseableHttpClient client = HttpClients.custom()
// .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
// @Override
// public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
// return true;
// }
// }).build()).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
// 如果JDK版本是1.8,可使用如下代码
// 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
CloseableHttpClient client = null;
try {
client = HttpClients.custom().setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, (x509CertChain, authType) -> true).build()).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
} catch (KeyManagementException e) {
e.printStackTrace();
return BaseVO.failure(e.getMessage());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return BaseVO.failure(e.getMessage());
} catch (KeyStoreException e) {
e.printStackTrace();
return BaseVO.failure(e.getMessage());
}
HttpResponse response;
try {
response = client.execute(// 请求方法POST
RequestBuilder.create("POST").setUri(url).addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded").addHeader(HttpHeaders.AUTHORIZATION, AUTH_HEADER_VALUE).addHeader("X-WSSE", wsseHeader).setEntity(new StringEntity(body)).build());
String responseContent = EntityUtils.toString(response.getEntity());
if (responseContent != null) {
JSONObject json = JSONObject.fromObject(responseContent);
if (json.get("code") != null && json.getString("code").equals("000000")) {
return BaseVO.success("success");
}
}
return BaseVO.failure(responseContent);
} catch (ClientProtocolException e) {
e.printStackTrace();
return BaseVO.failure(e.getMessage());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return BaseVO.failure(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
return BaseVO.failure(e.getMessage());
}
}
Aggregations