提交 483f0976 authored 作者: zgy's avatar zgy

短信发送方法封装

上级 d839b542
...@@ -25,6 +25,11 @@ ...@@ -25,6 +25,11 @@
<dependencies> <dependencies>
<dependency>
<groupId>com.yunpian.sdk</groupId>
<artifactId>yunpian-java-sdk</artifactId>
<version>1.2.7</version>
</dependency>
<dependency> <dependency>
<groupId>io.netty</groupId> <groupId>io.netty</groupId>
<artifactId>netty-all</artifactId> <artifactId>netty-all</artifactId>
...@@ -190,11 +195,11 @@ ...@@ -190,11 +195,11 @@
</dependency> </dependency>
<!--net.sf.json-lib--> <!--net.sf.json-lib-->
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency> <!-- <dependency>-->
<groupId>org.apache.httpcomponents</groupId> <!-- <groupId>org.apache.httpcomponents</groupId>-->
<artifactId>httpclient</artifactId> <!-- <artifactId>httpclient</artifactId>-->
<!--<version>4.5.2</version>--> <!-- &lt;!&ndash;<version>4.5.2</version>&ndash;&gt;-->
</dependency> <!-- </dependency>-->
<!--apache httpclient--> <!--apache httpclient-->
......
...@@ -65,9 +65,9 @@ public class FlutterWaveServiceImpl implements FlutterWaveService { ...@@ -65,9 +65,9 @@ public class FlutterWaveServiceImpl implements FlutterWaveService {
private String FLUTTERWAVE_REFUND_URL = "https://api.ravepay.co/gpx/merchant/transactions/refund"; private String FLUTTERWAVE_REFUND_URL = "https://api.ravepay.co/gpx/merchant/transactions/refund";
//校验API //校验API
private String VERIFY_PAY_URL = "https://api.ravepay.co/flwv3-pug/getpaidx/api/v2/verify"; private String VERIFY_PAY_URL = "https://api.ravepay.co/flwv3-pug/getpaidx/api/v2/verify";
//测试API //测试API
// private String VERIFY_PAY_URL = "https://ravesandboxapi.flutterwave.com/flwv3-pug/getpaidx/api/v2/verify"; // private String VERIFY_PAY_URL = "https://ravesandboxapi.flutterwave.com/flwv3-pug/getpaidx/api/v2/verify";
/** /**
* 使用卡号支付(预支付) * 使用卡号支付(预支付)
...@@ -80,7 +80,7 @@ public class FlutterWaveServiceImpl implements FlutterWaveService { ...@@ -80,7 +80,7 @@ public class FlutterWaveServiceImpl implements FlutterWaveService {
public Result payForOrder(String orderId, FlutterWaveCard flutterWaveCard) { public Result payForOrder(String orderId, FlutterWaveCard flutterWaveCard) {
RaveConstant.PUBLIC_KEY = domainProperties.getProperty("flutterwave.public.key"); RaveConstant.PUBLIC_KEY = domainProperties.getProperty("flutterwave.public.key");
RaveConstant.SECRET_KEY = domainProperties.getProperty("flutterwave.secret.key"); RaveConstant.SECRET_KEY = domainProperties.getProperty("flutterwave.secret.key");
RaveConstant.ENVIRONMENT = Environment.STAGING; //or live RaveConstant.ENVIRONMENT = Environment.LIVE; //or live
Result result = new Result(); Result result = new Result();
TbCfOrderVo tbCfOrderVo = (TbCfOrderVo) orderRedisCache.get(KeyConstant.ORDER_DET + orderId); TbCfOrderVo tbCfOrderVo = (TbCfOrderVo) orderRedisCache.get(KeyConstant.ORDER_DET + orderId);
TbCfOrderEntity orderEntity = tbCfOrderDao.queryObject(orderId); TbCfOrderEntity orderEntity = tbCfOrderDao.queryObject(orderId);
......
package com.diaoyun.zion.master.util;
/**
* @Auther: wudepeng
* @Date: 2019/12/04
* @Description:
*/
public class MessageUtil {
}
...@@ -9,6 +9,9 @@ import java.util.List; ...@@ -9,6 +9,9 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.constant.YunpianConstant;
import com.yunpian.sdk.model.SmsBatchSend;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
...@@ -19,110 +22,113 @@ import org.apache.http.impl.client.CloseableHttpClient; ...@@ -19,110 +22,113 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair; import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.junit.Test;
/** /**
* 短信接口类 * 短信接口类
* @author lyl
* *
* @author lyl
* <p>
* 2018年4月18日 * 2018年4月18日
*/ */
public class SMSUtil { public class SMSUtil {
//查账户信息的http地址 //查账户信息的http地址
private static String URI_GET_USER_INFO = "https://sms.yunpian.com/v2/user/get.json"; private static String URI_GET_USER_INFO = "https://sms.yunpian.com/v2/user/get.json";
//模板发送接口的http地址 //模板发送接口的http地址
private static String URI_TPL_SEND_SMS = "https://sms.yunpian.com/v2/sms/tpl_single_send.json"; private static String URI_TPL_SEND_SMS = "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
//private static String URI_TPL_SEND_SMS ="https://sms.yunpian.com/v2/sms/single_send.json"; private static String URI_SINGLE_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json";
//编码格式。发送编码格式统一用UTF-8 //编码格式。发送编码格式统一用UTF-8
private static String ENCODING = "UTF-8"; private static String ENCODING = "UTF-8";
private String API_KEY="cb20310db0e7dcc76a827afbd2ba548f"; private static String API_KEY = "cb20310db0e7dcc76a827afbd2ba548f";
/**
* 查询用户信息
*
* @param apikey
* @return
* @throws IOException
* @throws URISyntaxException
*/
public static String getUserInfo(String apikey) throws IOException, URISyntaxException {
Map<String, String> params = new HashMap<String, String>();
params.put("apikey", apikey);
return post(URI_GET_USER_INFO, params);
}
/**
* 提交响应
*
* @param url
* @param paramsMap
* @return
*/
public static String post(String url, Map<String, String> paramsMap) {
CloseableHttpClient client = HttpClients.createDefault();
String responseText = "";
CloseableHttpResponse response = null;
try {
HttpPost method = new HttpPost(url);
if (paramsMap != null) {
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> param : paramsMap.entrySet()) {
NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
paramList.add(pair);
}
method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
}
response = client.execute(method);
HttpEntity entity = response.getEntity();
if (entity != null) {
responseText = EntityUtils.toString(entity, ENCODING);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return responseText;
}
/** /**
* 查询用户信息 * 发送验证码
* *
* @param apikey * @return
* @return * @throws IOException
* @throws IOException */
* @throws URISyntaxException public static String yzCode(String phone) throws IOException {
*/
public static String getUserInfo(String apikey) throws IOException, URISyntaxException { if (phone.equals(null)) {
Map<String, String> params = new HashMap<String, String>(); return "error";
params.put("apikey", apikey); }
return post(URI_GET_USER_INFO, params); //编码格式。发送编码格式统一用UTF-8
} String ENCODING = "UTF-8";
long tpl_id = 3253208;//模板ID
/** String code = RandomStringUtils.randomNumeric(6);// 生成6位随机验证码
* 提交响应 String tpl_value = URLEncoder.encode("#code#", ENCODING) + "="
* + URLEncoder.encode(code, ENCODING);
* @param url System.out.println(tpl_value);
* @param paramsMap
* @return
*/
public static String post(String url, Map<String, String> paramsMap) {
CloseableHttpClient client = HttpClients.createDefault();
String responseText = "";
CloseableHttpResponse response = null;
try {
HttpPost method = new HttpPost(url);
if (paramsMap != null) {
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> param : paramsMap.entrySet()) {
NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
paramList.add(pair);
}
method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
}
response = client.execute(method);
HttpEntity entity = response.getEntity();
if (entity != null) {
responseText = EntityUtils.toString(entity, ENCODING);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return responseText;
}
/**
* 发送验证码
*
* @return
* @throws IOException
*/
public String yzCode(String phone) throws IOException{
if(phone.equals(null)){
return "error";
}
//编码格式。发送编码格式统一用UTF-8
String ENCODING = "UTF-8";
long tpl_id = 3253208;//模板ID
String code = RandomStringUtils.randomNumeric(6);// 生成6位随机验证码
String tpl_value = URLEncoder.encode("#code#",ENCODING) +"="
+ URLEncoder.encode(code, ENCODING) ;
System.out.println(tpl_value);
System.out.println(SMSUtil.tplSendSms(API_KEY, tpl_id, tpl_value, phone)); System.out.println(SMSUtil.tplSendSms(API_KEY, tpl_id, tpl_value, phone));
return code; return code;
} }
/** /**
* 通过模板发送短信 * 通过模板发送短信
* *
* @param apikey * @param apikey
* @param tpl_id * @param tpl_id
* @param tpl_value * @param tpl_value
* @param mobile * @param mobile
* @return * @return
* @throws IOException * @throws IOException
*/ */
public static String tplSendSms(String apikey, long tpl_id, String tpl_value, String mobile) throws IOException { public static String tplSendSms(String apikey, long tpl_id, String tpl_value, String mobile) throws IOException {
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
params.put("apikey", apikey); params.put("apikey", apikey);
params.put("tpl_id", String.valueOf(tpl_id)); params.put("tpl_id", String.valueOf(tpl_id));
...@@ -130,18 +136,58 @@ public class SMSUtil { ...@@ -130,18 +136,58 @@ public class SMSUtil {
params.put("mobile", mobile); params.put("mobile", mobile);
return post(URI_TPL_SEND_SMS, params); return post(URI_TPL_SEND_SMS, params);
} }
public static String singleSend(String apikey, String text, String mobile) {
Map<String, String> params = new HashMap<String, String>();//请求参数集合
params.put("apikey", apikey); /**
params.put("text", text); * 通过模板群发(新版)
params.put("mobile", mobile); *
return post("https://sms.yunpian.com/v2/sms/single_send.json", params);//请自行使用post方式请求,可使用Apache HttpClient * @param phone
} * @param tplId
public static void main(String[] args) throws IOException { * @param tplVal
// SMSUtil.singleSend(URI_TPL_SEND_SMS,"code","+8613751400455"); */
SMSUtil s=new SMSUtil(); public static void sendMessageByTpl(String phone, String tplId, String tplVal) {
String s1 = s.yzCode("+260972134374"); YunpianClient clnt = new YunpianClient(API_KEY).init();
Map<String, String> param = clnt.newParam(5);
} param.put(YunpianConstant.MOBILE, phone);
param.put(YunpianConstant.TPL_ID, tplId);
String code = RandomStringUtils.randomNumeric(6);
param.put(YunpianConstant.TPL_VALUE, tplVal);
com.yunpian.sdk.model.Result<SmsBatchSend> r = clnt.sms().tpl_batch_send(param);
System.out.println(r);
}
/**
* 群发短信(新版)
*
* @param phone
* @param text
*/
public static void sendMessageByText(String phone, String text) {
YunpianClient clnt = new YunpianClient(API_KEY).init();
Map<String, String> param = clnt.newParam(5);
param.put(YunpianConstant.MOBILE, phone);
String code = RandomStringUtils.randomNumeric(6);
param.put(YunpianConstant.TEXT, text);
com.yunpian.sdk.model.Result<SmsBatchSend> result = clnt.sms().batch_send(param);
System.out.println(result);
}
/**
* 发送物流短信
*
* @param phone
*/
public static void sendLogisticsMessage(String phone) {
String message = "【Afrishop】Thank you for shopping with Afrishop! Your parcel has arrived in Zambia.";
sendMessageByText(phone, message);
}
public static void main(String[] args) throws IOException {
String phone = "13751400455,18607444177";
SMSUtil.sendLogisticsMessage(phone);
}
} }
...@@ -39,12 +39,12 @@ spring: ...@@ -39,12 +39,12 @@ spring:
type: com.alibaba.druid.pool.DruidDataSource type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
# 测试环境 # 测试环境
url: jdbc:mysql://47.106.242.175:3306/chinafrica?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&useSSL=false # url: jdbc:mysql://47.106.242.175:3306/chinafrica?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&useSSL=false
username: root
password: diaoyun666
# url: jdbc:mysql://159.138.48.71:3306/chinafrica?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&useSSL=false
# username: root # username: root
# password: Diaoyunnuli.8 # password: diaoyun666
url: jdbc:mysql://159.138.48.71:3306/chinafrica?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&useSSL=false
username: root
password: Diaoyunnuli.8
# 连接池配置 # 连接池配置
initial-size: 5 initial-size: 5
......
...@@ -14,9 +14,17 @@ import com.mashape.unirest.http.Headers; ...@@ -14,9 +14,17 @@ import com.mashape.unirest.http.Headers;
import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest; import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException; import com.mashape.unirest.http.exceptions.UnirestException;
import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.api.SmsApi;
import com.yunpian.sdk.api.TplApi;
import com.yunpian.sdk.api.YunpianApi;
import com.yunpian.sdk.constant.YunpianConstant;
import com.yunpian.sdk.model.SmsBatchSend;
import com.yunpian.sdk.model.SmsSingleSend;
import io.swagger.models.auth.In; import io.swagger.models.auth.In;
import net.sf.json.JSONArray; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.http.Consts; import org.apache.http.Consts;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
import org.junit.Test; import org.junit.Test;
...@@ -29,6 +37,7 @@ import java.io.UnsupportedEncodingException; ...@@ -29,6 +37,7 @@ import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*; import java.util.*;
import static com.github.pagehelper.page.PageMethod.startPage; import static com.github.pagehelper.page.PageMethod.startPage;
...@@ -123,7 +132,7 @@ public class PayTest { ...@@ -123,7 +132,7 @@ public class PayTest {
@Test @Test
public void testHeaders() throws UnirestException { public void testHeaders() throws UnirestException {
// String url = "https://7f0e048ac4016b9357bb1eb2217e4201:1c97a1222c5b40e0597c816c6dfecac0@mollykitty.myshopify.com/admin/api/2019-10/products.json"; // String url = "https://7f0e048ac4016b9357bb1eb2217e4201:1c97a1222c5b40e0597c816c6dfecac0@mollykitty.myshopify.com/admin/api/2019-10/products.json";
// Map<String, Object> map = new HashMap<>(); // Map<String, Object> map = new HashMap<>();
// map.put("product_type", "Men"); // map.put("product_type", "Men");
...@@ -141,9 +150,74 @@ public class PayTest { ...@@ -141,9 +150,74 @@ public class PayTest {
String str = link.get(0); String str = link.get(0);
String[] split = str.split(","); String[] split = str.split(",");
String s = split[0]; String s = split[0];
System.out.println(s.substring(s.lastIndexOf("page_info="),s.lastIndexOf(">;"))); System.out.println(s.substring(s.lastIndexOf("page_info="), s.lastIndexOf(">;")));
} }
/**
* 批量发送
*
* @throws UnsupportedEncodingException
*/
@Test
public void sendMessage() throws UnsupportedEncodingException {
// YunpianClient clnt = new YunpianClient("cb20310db0e7dcc76a827afbd2ba548f").init();
// Map<String, String> param = clnt.newParam(5);
// //param.put("tpl_id","3253208");
// param.put(YunpianConstant.MOBILE, "+260977598876");
String code = RandomStringUtils.randomNumeric(6);
// param.put(YunpianConstant.TEXT, "【Afrishop】you are logging in for verification, and the verification code is '" + code + "'.Do not disclose the verification code to others.");
// // param.put(EXTEND, "001");
// // param.put(UID, "10001");
// // param.put(CALLBACK_URL, "http://yourreceiveurl_address");
// com.yunpian.sdk.model.Result<SmsBatchSend> result = clnt.sms().batch_send(param);
// System.out.println(result);
String text = "【Afrishop】you are logging in for verification, and the verification code is '" + code + "'.Do not disclose the verification code to others.";
sendMessageByText("13751400455", text);
}
public void sendMessageByText(String phone, String text) {
YunpianClient clnt = new YunpianClient("cb20310db0e7dcc76a827afbd2ba548f").init();
Map<String, String> param = clnt.newParam(5);
param.put(YunpianConstant.MOBILE, phone);
String code = RandomStringUtils.randomNumeric(6);
param.put(YunpianConstant.TEXT, text);
com.yunpian.sdk.model.Result<SmsBatchSend> result = clnt.sms().batch_send(param);
System.out.println(result);
}
@Test
public void sendTpl() {
// YunpianClient clnt = new YunpianClient("cb20310db0e7dcc76a827afbd2ba548f").init();
// Map<String, String> param = clnt.newParam(5);
// param.put(YunpianConstant.MOBILE, "13751400455");
// param.put(YunpianConstant.TPL_ID, "3253208");
// param.put(YunpianConstant.TPL_VALUE, "#code#=" + code);
String code = RandomStringUtils.randomNumeric(6);
sendMessageByTpl("cb20310db0e7dcc76a827afbd2ba548f", "13751400455", "3253208", "#code#=" + code);
}
/**
* 通过模板群发
*
* @param apiKey
* @param phone
* @param tplId
* @param tplVal
*/
public void sendMessageByTpl(String apiKey, String phone, String tplId, String tplVal) {
YunpianClient clnt = new YunpianClient(apiKey).init();
Map<String, String> param = clnt.newParam(5);
param.put(YunpianConstant.MOBILE, phone);
param.put(YunpianConstant.TPL_ID, tplId);
String code = RandomStringUtils.randomNumeric(6);
param.put(YunpianConstant.TPL_VALUE, tplVal);
// param.put(EXTEND, "001");
// param.put(UID, "10001");
com.yunpian.sdk.model.Result<SmsBatchSend> r = clnt.sms().tpl_batch_send(param);
System.out.println(r);
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论