Skip to content

Commit aded340

Browse files
committed
#253 修改企业号发送消息的messageSend方法,增加返回值,方便客户端进行自行处理
1 parent c7ffff0 commit aded340

File tree

4 files changed

+142
-25
lines changed

4 files changed

+142
-25
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
import me.chanjar.weixin.common.session.WxSessionManager;
99
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
1010
import me.chanjar.weixin.common.util.http.RequestExecutor;
11-
import me.chanjar.weixin.cp.bean.WxCpDepart;
12-
import me.chanjar.weixin.cp.bean.WxCpMessage;
13-
import me.chanjar.weixin.cp.bean.WxCpTag;
14-
import me.chanjar.weixin.cp.bean.WxCpUser;
11+
import me.chanjar.weixin.cp.bean.*;
1512

1613
import java.io.File;
1714
import java.io.IOException;
@@ -141,7 +138,7 @@ WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream i
141138
*
142139
* @param message 要发送的消息对象
143140
*/
144-
void messageSend(WxCpMessage message) throws WxErrorException;
141+
WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException;
145142

146143
/**
147144
* <pre>

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/AbstractWxCpServiceImpl.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
import me.chanjar.weixin.common.util.json.GsonHelper;
1818
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
1919
import me.chanjar.weixin.cp.api.WxCpService;
20-
import me.chanjar.weixin.cp.bean.WxCpDepart;
21-
import me.chanjar.weixin.cp.bean.WxCpMessage;
22-
import me.chanjar.weixin.cp.bean.WxCpTag;
23-
import me.chanjar.weixin.cp.bean.WxCpUser;
20+
import me.chanjar.weixin.cp.bean.*;
2421
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
2522
import org.apache.commons.lang3.StringUtils;
2623
import org.slf4j.Logger;
@@ -131,9 +128,9 @@ public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException
131128
}
132129

133130
@Override
134-
public void messageSend(WxCpMessage message) throws WxErrorException {
131+
public WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException {
135132
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
136-
post(url, message.toJson());
133+
return WxCpMessageSendResult.fromJson(this.post(url, message.toJson()));
137134
}
138135

139136
@Override
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import com.google.common.base.Splitter;
4+
import com.google.gson.annotations.SerializedName;
5+
import me.chanjar.weixin.common.util.ToStringUtils;
6+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
7+
import org.apache.commons.lang3.StringUtils;
8+
9+
import java.util.Collections;
10+
import java.util.List;
11+
12+
/**
13+
* <pre>
14+
* 消息发送结果对象类
15+
* Created by Binary Wang on 2017-6-22.
16+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
17+
* </pre>
18+
*/
19+
public class WxCpMessageSendResult {
20+
@Override
21+
public String toString() {
22+
return ToStringUtils.toSimpleString(this);
23+
}
24+
25+
public static WxCpMessageSendResult fromJson(String json) {
26+
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpMessageSendResult.class);
27+
}
28+
29+
@SerializedName("errcode")
30+
private Integer errCode;
31+
32+
@SerializedName("errmsg")
33+
private String errMsg;
34+
35+
@SerializedName("invaliduser")
36+
private String invalidUser;
37+
38+
@SerializedName("invalidparty")
39+
private String invalidParty;
40+
41+
@SerializedName("invalidtag")
42+
private String invalidTag;
43+
44+
public Integer getErrCode() {
45+
return this.errCode;
46+
}
47+
48+
public void setErrCode(Integer errCode) {
49+
this.errCode = errCode;
50+
}
51+
52+
public String getErrMsg() {
53+
return this.errMsg;
54+
}
55+
56+
public void setErrMsg(String errMsg) {
57+
this.errMsg = errMsg;
58+
}
59+
60+
public String getInvalidUser() {
61+
return this.invalidUser;
62+
}
63+
64+
public void setInvalidUser(String invalidUser) {
65+
this.invalidUser = invalidUser;
66+
}
67+
68+
public String getInvalidParty() {
69+
return this.invalidParty;
70+
}
71+
72+
public void setInvalidParty(String invalidParty) {
73+
this.invalidParty = invalidParty;
74+
}
75+
76+
public String getInvalidTag() {
77+
return this.invalidTag;
78+
}
79+
80+
public void setInvalidTag(String invalidTag) {
81+
this.invalidTag = invalidTag;
82+
}
83+
84+
public List<String> getInvalidUserList() {
85+
return this.content2List(this.invalidUser);
86+
}
87+
88+
private List<String> content2List(String content) {
89+
if(StringUtils.isBlank(content)){
90+
return Collections.emptyList();
91+
}
92+
93+
return Splitter.on("|").splitToList(content);
94+
}
95+
96+
public List<String> getInvalidPartyList() {
97+
return this.content2List(this.invalidParty);
98+
}
99+
100+
public List<String> getInvalidTagList() {
101+
return this.content2List(this.invalidTag);
102+
}
103+
}

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMessageAPITest.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,58 @@
55
import me.chanjar.weixin.common.exception.WxErrorException;
66
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
77
import me.chanjar.weixin.cp.bean.WxCpMessage;
8-
import org.testng.annotations.Guice;
9-
import org.testng.annotations.Test;
8+
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
9+
import org.testng.annotations.*;
10+
11+
import static org.testng.Assert.*;
1012

1113
/***
1214
* 测试发送消息
1315
* @author Daniel Qian
1416
*
1517
*/
16-
@Test(groups = "customMessageAPI", dependsOnGroups = "baseAPI")
18+
@Test(groups = "customMessageAPI")
1719
@Guice(modules = ApiTestModule.class)
1820
public class WxCpMessageAPITest {
1921

2022
@Inject
2123
protected WxCpServiceImpl wxService;
24+
private ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage;
25+
26+
@BeforeTest
27+
public void setup() {
28+
configStorage = (ApiTestModule.WxXmlCpInMemoryConfigStorage) this.wxService.getWxCpConfigStorage();
29+
}
2230

23-
public void testSendCustomMessage() throws WxErrorException {
24-
ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlCpInMemoryConfigStorage) this.wxService.getWxCpConfigStorage();
25-
WxCpMessage message1 = new WxCpMessage();
26-
message1.setAgentId(configStorage.getAgentId());
27-
message1.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
28-
message1.setToUser(configStorage.getUserId());
29-
message1.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
30-
this.wxService.messageSend(message1);
31+
public void testSendMessage() throws WxErrorException {
32+
WxCpMessage message = new WxCpMessage();
33+
message.setAgentId(configStorage.getAgentId());
34+
message.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
35+
message.setToUser(configStorage.getUserId());
36+
message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
3137

32-
WxCpMessage message2 = WxCpMessage
38+
WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
39+
assertNotNull(messageSendResult);
40+
System.out.println(messageSendResult);
41+
System.out.println(messageSendResult.getInvalidPartyList());
42+
System.out.println(messageSendResult.getInvalidUserList());
43+
System.out.println(messageSendResult.getInvalidTagList());
44+
}
45+
46+
public void testSendMessage1() throws WxErrorException {
47+
WxCpMessage message = WxCpMessage
3348
.TEXT()
3449
.agentId(configStorage.getAgentId())
3550
.toUser(configStorage.getUserId())
3651
.content("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>")
3752
.build();
38-
this.wxService.messageSend(message2);
3953

40-
}
54+
WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
55+
assertNotNull(messageSendResult);
56+
System.out.println(messageSendResult);
57+
System.out.println(messageSendResult.getInvalidPartyList());
58+
System.out.println(messageSendResult.getInvalidUserList());
59+
System.out.println(messageSendResult.getInvalidTagList());
4160

61+
}
4262
}

0 commit comments

Comments
 (0)