Skip to content

Commit

Permalink
Merge pull request #45 from rememberber/develop
Browse files Browse the repository at this point in the history
新增对阿里云短信的支持
  • Loading branch information
rememberber authored Mar 16, 2018
2 parents f78dd3d + efafce3 commit 0d00274
Show file tree
Hide file tree
Showing 15 changed files with 467 additions and 16 deletions.
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<forms_rt.version>7.0.3</forms_rt.version>
<fastjson.version>1.2.46</fastjson.version>
<sun-mail.version>1.5.6</sun-mail.version>
<aliyun-java-sdk-core.version>3.5.0</aliyun-java-sdk-core.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -79,6 +80,12 @@
<version>${sun-mail.version}</version>
</dependency>

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

<dependency>
<groupId>com.apple</groupId>
<artifactId>AppleJavaExtensions</artifactId>
Expand Down Expand Up @@ -109,6 +116,12 @@
<version>1.0</version>
</dependency>

<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -225,6 +238,22 @@
<goal>install-file</goal>
</goals>
</execution>
<execution>
<id>install-alysms-sdk</id>
<phase>clean</phase>
<configuration>
<file>${basedir}/src/main/lib/aliyun-java-sdk-dysmsapi-1.0.0.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

### 功能
自定义消息并批量推送
>目前支持的消息类型:微信模板消息、微信客服消息、阿里大于模板短信
>目前支持的消息类型:微信模板消息、微信客服消息、阿里云短信、阿里大于模板短信
>支持消息编辑、预览
>支持文件导入用户
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* 阿里模板短信发送服务线程
* Created by rememberber(https://github.com/rememberber) on 2017/3/29.
*/
public class AliTemplateMsgServiceThread extends BaseMsgServiceThread {
public class AliDayuTemplateSmsMsgServiceThread extends BaseMsgServiceThread {

/**
* 构造函数
Expand All @@ -23,7 +23,7 @@ public class AliTemplateMsgServiceThread extends BaseMsgServiceThread {
* @param pageTo 截止页
* @param pageSize 页大小
*/
public AliTemplateMsgServiceThread(int pageFrom, int pageTo, int pageSize) {
public AliDayuTemplateSmsMsgServiceThread(int pageFrom, int pageTo, int pageSize) {
super(pageFrom, pageTo, pageSize);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.fangxuele.tool.wechat.push.logic;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.fangxuele.tool.wechat.push.ui.Init;
import com.fangxuele.tool.wechat.push.ui.MainWindow;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;

/**
* 阿里云短信发送服务线程
* Created by rememberber(https://github.com/rememberber) on 2018/3/16.
*/
public class AliYunSmsMsgServiceThread extends BaseMsgServiceThread {

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

@Override
public void run() {

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

String aliyunAccessKeyId = Init.configer.getAliyunAccessKeyId();
String aliyunAccessKeySecret = Init.configer.getAliyunAccessKeySecret();

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

//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunAccessKeyId, aliyunAccessKeySecret);
try {
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
} catch (ClientException e) {
logger.error(e);
PushManage.console(e.getMessage());
}
IAcsClient acsClient = new DefaultAcsClient(profile);

// 组织模板消息
SendSmsRequest sendSmsRequest;

SendSmsResponse response;

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

// 本条消息所需的数据
String[] msgData = list.get(i);
String telNum = msgData[0];
try {
sendSmsRequest = PushManage.makeAliyunMessage(msgData);
sendSmsRequest.setPhoneNumbers(telNum);

// 空跑控制
if (!MainWindow.mainWindow.getDryRunCheckBox().isSelected()) {
response = acsClient.getAcsResponse(sendSmsRequest);
if (response.getCode() != null && "OK".equals(response.getCode())) {
// 总发送成功+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(response.getMessage()).append(";ErrorCode:")
.append(response.getCode()).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));
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
public class BaseMsgServiceThread extends Thread {

private static final Log logger = LogFactory.get();
public static final Log logger = LogFactory.get();

/**
* 起始页号
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/com/fangxuele/tool/wechat/push/logic/PushManage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package com.fangxuele.tool.wechat.push.logic;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.fangxuele.tool.wechat.push.ui.Init;
import com.fangxuele.tool.wechat.push.ui.MainWindow;
import com.fangxuele.tool.wechat.push.util.SystemUtil;
Expand Down Expand Up @@ -103,6 +111,36 @@ public static void preview() throws Exception {
}
}
break;
case "阿里云短信":
String aliyunAccessKeyId = Init.configer.getAliyunAccessKeyId();
String aliyunAccessKeySecret = Init.configer.getAliyunAccessKeySecret();

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

//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunAccessKeyId, aliyunAccessKeySecret);
try {
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
} catch (ClientException e) {
logger.error(e.toString());
}

IAcsClient acsClient = new DefaultAcsClient(profile);
for (String[] msgData : msgDataList) {
SendSmsRequest request = makeAliyunMessage(msgData);
request.setPhoneNumbers(msgData[0]);
SendSmsResponse response = acsClient.getAcsResponse(request);

if (response.getCode() == null || !"OK".equals(response.getCode())) {
throw new Exception(new StringBuffer().append(response.getMessage()).append(";\n\nErrorCode:")
.append(response.getCode()).append(";\n\ntelNum:").append(msgData[0]).toString());
}
}
break;
case "阿里大于模板短信":
String aliServerUrl = Init.configer.getAliServerUrl();
String aliAppKey = Init.configer.getAliAppKey();
Expand Down Expand Up @@ -270,6 +308,48 @@ synchronized public static WxMpKefuMessage makeKefuMessage(String[] msgData) {
return kefuMessage;
}

/**
* 组织阿里云短信消息
*
* @param msgData
* @return
*/
synchronized public static SendSmsRequest makeAliyunMessage(String[] msgData) {
SendSmsRequest request = new SendSmsRequest();
//使用post提交
request.setMethod(MethodType.POST);
//必填:短信签名-可在短信控制台中找到
request.setSignName(Init.configer.getAliyunSign());

// 模板参数
Map<String, String> paramMap = new HashMap<>();

if (MainWindow.mainWindow.getTemplateMsgDataTable().getModel().getRowCount() == 0) {
Init.initTemplateDataTable();
}

DefaultTableModel tableModel = (DefaultTableModel) MainWindow.mainWindow.getTemplateMsgDataTable().getModel();
int rowCount = tableModel.getRowCount();
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);
while (matcher.find()) {
value = value.replace(matcher.group(0), msgData[Integer.parseInt(matcher.group(1).trim())]);
}

paramMap.put(key, value);
}

request.setTemplateParam(JSONUtil.parseFromMap(paramMap).toJSONString(0));

// 短信模板ID,传入的模板必须是在阿里阿里云短信中的可用模板。示例:SMS_585014
request.setTemplateCode(MainWindow.mainWindow.getMsgTemplateIdTextField().getText());

return request;
}

/**
* 组织阿里大于模板短信消息
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ public void run() {
}
thread.setWxMpService(wxMpService);
} else if ("阿里大于模板短信".equals(msgType)) {
thread = new AliTemplateMsgServiceThread(i * pagePerThread,
thread = new AliDayuTemplateSmsMsgServiceThread(i * pagePerThread,
i * pagePerThread + pagePerThread - 1, pageSize);
} else if ("阿里云短信".equals(msgType)) {
thread = new AliYunSmsMsgServiceThread(i * pagePerThread,
i * pagePerThread + pagePerThread - 1, pageSize);
}

Expand Down
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_1.9.0_180228";
public final static String APP_VERSION = "v_1.10.0_180316";

/**
* 主窗口图标
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/fangxuele/tool/wechat/push/ui/Init.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public static void switchMsgType(String msgType) {
MainWindow.mainWindow.getTemplateDataColorTextField().setVisible(true);
MainWindow.mainWindow.getPreviewMemberLabel().setText("预览消息用户openid(以半角分号分隔)");
break;
case "阿里云短信":
case "阿里大于模板短信":
MainWindow.mainWindow.getKefuMsgPanel().setVisible(false);
MainWindow.mainWindow.getTemplateMsgPanel().setVisible(true);
Expand Down Expand Up @@ -420,6 +421,11 @@ public static void initSettingTab() {
MainWindow.mainWindow.setWechatTokenPasswordField(configer.getWechatToken());
MainWindow.mainWindow.setWechatAesKeyPasswordField(configer.getWechatAesKey());

// 阿里云短信
MainWindow.mainWindow.setAliyunAccessKeyIdTextField(configer.getAliyunAccessKeyId());
MainWindow.mainWindow.setAliyunAccessKeySecretTextField(configer.getAliyunAccessKeySecret());
MainWindow.mainWindow.setAliyunSignTextField(configer.getAliyunSign());

// 阿里大于
MainWindow.mainWindow.setAliServerUrlTextField(configer.getAliServerUrl());
MainWindow.mainWindow.setAliAppKeyPasswordField(configer.getAliAppKey());
Expand Down
Loading

0 comments on commit 0d00274

Please sign in to comment.