Skip to content

Commit

Permalink
Merge pull request #60 from rememberber/develop
Browse files Browse the repository at this point in the history
新增支持云片网短信
  • Loading branch information
rememberber authored Jul 15, 2018
2 parents 1037e1c + 0cf0db9 commit 140dd53
Show file tree
Hide file tree
Showing 15 changed files with 537 additions and 134 deletions.
24 changes: 16 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<sun-mail.version>1.5.6</sun-mail.version>
<aliyun-java-sdk-core.version>3.5.0</aliyun-java-sdk-core.version>
<poi.version>3.17</poi.version>
<AppleJavaExtensions.version>1.4</AppleJavaExtensions.version>
<qcloudsms.version>1.0.2</qcloudsms.version>
<yunpian-java-sdk.version>1.2.7</yunpian-java-sdk.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -103,7 +106,19 @@
<dependency>
<groupId>com.apple</groupId>
<artifactId>AppleJavaExtensions</artifactId>
<version>1.4</version>
<version>${AppleJavaExtensions.version}</version>
</dependency>

<dependency>
<groupId>com.github.qcloudsms</groupId>
<artifactId>qcloudsms</artifactId>
<version>${qcloudsms.version}</version>
</dependency>

<dependency>
<groupId>com.yunpian.sdk</groupId>
<artifactId>yunpian-java-sdk</artifactId>
<version>${yunpian-java-sdk.version}</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -135,13 +150,6 @@
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
</dependency>

<dependency>
<groupId>com.github.qcloudsms</groupId>
<artifactId>qcloudsms</artifactId>
<version>1.0.2</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class MsgHisManage {
/**
* 历史消息保存的csv的列数
*/
public static final int ARRAY_LENGTH = 14;
public static final int ARRAY_LENGTH = 15;

public static MsgHisManage getInstance() {
return ourInstance;
Expand Down
69 changes: 59 additions & 10 deletions src/main/java/com/fangxuele/tool/wechat/push/logic/PushManage.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.model.Result;
import com.yunpian.sdk.model.SmsSingleSend;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
Expand Down Expand Up @@ -61,7 +64,7 @@ public class PushManage {
*
* @throws Exception
*/
public static void preview() throws Exception {
public static boolean preview() throws Exception {
List<String[]> msgDataList = new ArrayList<>();

for (String data : MainWindow.mainWindow.getPreviewUserField().getText().split(";")) {
Expand All @@ -73,7 +76,7 @@ public static void preview() throws Exception {
WxMpTemplateMessage wxMessageTemplate;
WxMpService wxMpService = getWxMpService();
if (wxMpService.getWxMpConfigStorage() == null) {
return;
return false;
}

for (String[] msgData : msgDataList) {
Expand All @@ -87,7 +90,7 @@ public static void preview() throws Exception {
WxMaTemplateMessage wxMaMessageTemplate;
WxMaService wxMaService = getWxMaService();
if (wxMaService.getWxMaConfig() == null) {
return;
return false;
}

for (String[] msgData : msgDataList) {
Expand All @@ -102,7 +105,7 @@ public static void preview() throws Exception {
wxMpService = getWxMpService();
WxMpKefuMessage wxMpKefuMessage;
if (wxMpService.getWxMpConfigStorage() == null) {
return;
return false;
}

for (String[] msgData : msgDataList) {
Expand All @@ -115,7 +118,7 @@ public static void preview() throws Exception {
case "客服消息优先":
wxMpService = getWxMpService();
if (wxMpService.getWxMpConfigStorage() == null) {
return;
return false;
}

for (String[] msgData : msgDataList) {
Expand All @@ -140,6 +143,7 @@ public static void preview() throws Exception {
JOptionPane.showMessageDialog(MainWindow.mainWindow.getSettingPanel(),
"请先在设置中填写并保存阿里云短信相关配置!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return false;
}

//初始化acsClient,暂不支持region化
Expand Down Expand Up @@ -170,6 +174,7 @@ public static void preview() throws Exception {
JOptionPane.showMessageDialog(MainWindow.mainWindow.getSettingPanel(),
"请先在设置中填写并保存腾讯云短信相关配置!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return false;
}

SmsSingleSender ssender = new SmsSingleSender(Integer.valueOf(txyunAppId), txyunAppKey);
Expand All @@ -194,6 +199,7 @@ public static void preview() throws Exception {
JOptionPane.showMessageDialog(MainWindow.mainWindow.getSettingPanel(),
"请先在设置中填写并保存阿里大于相关配置!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return false;
}

TaobaoClient client = new DefaultTaobaoClient(aliServerUrl, aliAppKey, aliAppSecret);
Expand All @@ -207,9 +213,32 @@ public static void preview() throws Exception {
}
}
break;
case "云片网短信":
String yunpianApiKey = Init.configer.getYunpianApiKey();

if (StringUtils.isEmpty(yunpianApiKey)) {
JOptionPane.showMessageDialog(MainWindow.mainWindow.getSettingPanel(),
"请先在设置中填写并保存云片网短信相关配置!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return false;
}

YunpianClient clnt = new YunpianClient(yunpianApiKey).init();

for (String[] msgData : msgDataList) {
Map<String, String> params = makeYunpianMessage(msgData);
params.put(YunpianClient.MOBILE, msgData[0]);
Result<SmsSingleSend> result = clnt.sms().single_send(params);
if (result.getCode() != 0) {
throw new Exception(result.toString());
}
}
clnt.close();
break;
default:
break;
}
return true;
}

/**
Expand Down Expand Up @@ -498,9 +527,6 @@ synchronized public static AlibabaAliqinFcSmsNumSendRequest makeAliTemplateMessa
* @return
*/
synchronized public static String[] makeTxyunMessage(String[] msgData) {
// 模板参数
Map<String, String> paramMap = new HashMap<>();

if (MainWindow.mainWindow.getTemplateMsgDataTable().getModel().getRowCount() == 0) {
Init.initTemplateDataTable();
}
Expand All @@ -509,7 +535,6 @@ synchronized public static String[] makeTxyunMessage(String[] msgData) {
int rowCount = tableModel.getRowCount();
String[] params = new String[rowCount];
for (int i = 0; i < rowCount; i++) {
String key = (String) tableModel.getValueAt(i, 0);
String value = ((String) tableModel.getValueAt(i, 1)).replaceAll("$ENTER$", "\n");
Pattern p = Pattern.compile("\\{([^{}]+)\\}");
Matcher matcher = p.matcher(value);
Expand All @@ -523,6 +548,27 @@ synchronized public static String[] makeTxyunMessage(String[] msgData) {
return params;
}

/**
* 组织云片网短信消息
*
* @param msgData
* @return
*/
synchronized static Map<String, String> makeYunpianMessage(String[] msgData) {
Map<String, String> params = new HashMap<>(2);

String text = MainWindow.mainWindow.getMsgYunpianMsgContentTextField().getText();
text = text.replaceAll("$ENTER$", "\n");
Pattern p = Pattern.compile("\\{([^{}]+)\\}");
Matcher matcher = p.matcher(text);
while (matcher.find()) {
text = text.replace(matcher.group(0), msgData[Integer.parseInt(matcher.group(1).trim())]);
}

params.put(YunpianClient.TEXT, text);
return params;
}

/**
* 微信公众号配置
*
Expand Down Expand Up @@ -571,7 +617,10 @@ private static WxMaInMemoryConfig wxMaConfigStorage() {
*/
public static WxMpService getWxMpService() {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
WxMpConfigStorage wxMpConfigStorage = wxMpConfigStorage();
if (wxMpConfigStorage != null) {
wxMpService.setWxMpConfigStorage(wxMpConfigStorage);
}
return wxMpService;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public void run() {
} else if ("腾讯云短信".equals(msgType)) {
thread = new TxYunSmsMsgServiceThread(i * pagePerThread,
i * pagePerThread + pagePerThread - 1, pageSize);
} else if ("云片网短信".equals(msgType)) {
thread = new YunpianSmsMsgServiceThread(i * pagePerThread,
i * pagePerThread + pagePerThread - 1, pageSize);
}

thread.setName(new StringBuffer().append("T-").append(i).toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.fangxuele.tool.wechat.push.logic;

import com.fangxuele.tool.wechat.push.ui.Init;
import com.fangxuele.tool.wechat.push.ui.MainWindow;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.model.Result;
import com.yunpian.sdk.model.SmsSingleSend;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.util.Map;

/**
* 云片网短信发送服务线程
* Created by rememberber(https://github.com/rememberber) on 2018/7/13.
*/
public class YunpianSmsMsgServiceThread extends BaseMsgServiceThread {

/**
* 构造函数
*
* @param pageFrom 起始页
* @param pageTo 截止页
* @param pageSize 页大小
*/
public YunpianSmsMsgServiceThread(int pageFrom, int pageTo, int pageSize) {
super(pageFrom, pageTo, pageSize);
}

@Override
public void run() {

// 初始化当前线程
initCurrentThread();

String yunpianApiKey = Init.configer.getYunpianApiKey();

if (StringUtils.isEmpty(yunpianApiKey)) {
JOptionPane.showMessageDialog(MainWindow.mainWindow.getSettingPanel(),
"请先在设置中填写并保存云片网短信相关配置!", "提示",
JOptionPane.INFORMATION_MESSAGE);
}

YunpianClient clnt = new YunpianClient(yunpianApiKey).init();

for (int i = 0; i < list.size(); i++) {
if (!PushData.running) {
// 停止
PushData.increaseStopedThread();
return;
}

// 本条消息所需的数据
String[] msgData = list.get(i);
String telNum = msgData[0];
try {
Map<String, String> params = PushManage.makeYunpianMessage(msgData);
params.put(YunpianClient.MOBILE, telNum);

// 空跑控制
if (!MainWindow.mainWindow.getDryRunCheckBox().isSelected()) {
Result<SmsSingleSend> result = clnt.sms().single_send(params);

if (result.getCode() == 0) {
// 总发送成功+1
PushData.increaseSuccess();
MainWindow.mainWindow.getPushSuccessCount().setText(String.valueOf(PushData.successRecords));

// 当前线程发送成功+1
currentThreadSuccessCount++;
tableModel.setValueAt(currentThreadSuccessCount, tableRow, 2);

// 保存发送成功
PushData.sendSuccessList.add(msgData);
} else {
// 总发送失败+1
PushData.increaseFail();
MainWindow.mainWindow.getPushFailCount().setText(String.valueOf(PushData.failRecords));

// 保存发送失败
PushData.sendFailList.add(msgData);

// 失败异常信息输出控制台
PushManage.console(new StringBuffer().append("发送失败:").append(result.toString())
.append(";telNum:").append(telNum).toString());

// 当前线程发送失败+1
currentThreadFailCount++;
tableModel.setValueAt(currentThreadFailCount, tableRow, 3);
}
} else {
// 总发送成功+1
PushData.increaseSuccess();
MainWindow.mainWindow.getPushSuccessCount().setText(String.valueOf(PushData.successRecords));

// 当前线程发送成功+1
currentThreadSuccessCount++;
tableModel.setValueAt(currentThreadSuccessCount, tableRow, 2);

// 保存发送成功
PushData.sendSuccessList.add(msgData);
}

} catch (Exception e) {
// 总发送失败+1
PushData.increaseFail();
MainWindow.mainWindow.getPushFailCount().setText(String.valueOf(PushData.failRecords));

// 保存发送失败
PushData.sendFailList.add(msgData);

// 失败异常信息输出控制台
PushManage.console(new StringBuffer().append("发送失败:").append(e.getMessage()).append(";telNum:").append(telNum).toString());

// 当前线程发送失败+1
currentThreadFailCount++;
tableModel.setValueAt(currentThreadFailCount, tableRow, 3);
}
// 当前线程进度条
tableModel.setValueAt((int) ((double) (i + 1) / list.size() * 100), tableRow, 5);

// 总进度条
MainWindow.mainWindow.getPushTotalProgressBar().setValue((int) (PushData.successRecords + PushData.failRecords));
}

// 当前线程结束
clnt.close();
currentThreadFinish();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ConstantsUI {
* 软件名称,版本
*/
public final static String APP_NAME = "WePush";
public final static String APP_VERSION = "v_2.2.1_180625";
public final static String APP_VERSION = "v_2.3.0_180714";

/**
* 主窗口图标
Expand Down
Loading

0 comments on commit 140dd53

Please sign in to comment.