Skip to content

Commit 258f35a

Browse files
committed
直接发送消息的支持
1 parent b69aa77 commit 258f35a

23 files changed

+291
-190
lines changed

weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxConsts.java

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public class WxConsts {
1717
public static final String XML_MSG_LOCATION = "location";
1818
public static final String XML_MSG_LINK = "link";
1919
public static final String XML_MSG_EVENT = "event";
20-
public static final String XML_TRANSFER_CUSTOMER_SERVICE = "transfer_customer_service";
2120

2221
///////////////////////
2322
// 客服消息的消息类型
@@ -28,49 +27,8 @@ public class WxConsts {
2827
public static final String CUSTOM_MSG_VIDEO = "video";
2928
public static final String CUSTOM_MSG_MUSIC = "music";
3029
public static final String CUSTOM_MSG_NEWS = "news";
31-
32-
///////////////////////
33-
// 群发消息的消息类型
34-
///////////////////////
35-
public static final String MASS_MSG_NEWS = "mpnews";
36-
public static final String MASS_MSG_TEXT = "text";
37-
public static final String MASS_MSG_VOICE = "voice";
38-
public static final String MASS_MSG_IMAGE = "image";
39-
public static final String MASS_MSG_VIDEO = "mpvideo";
40-
41-
///////////////////////
42-
// 群发消息后微信端推送给服务器的反馈消息
43-
///////////////////////
44-
public static final String MASS_ST_SUCCESS = "send success";
45-
public static final String MASS_ST_FAIL = "send fail";
46-
public static final String MASS_ST_涉嫌广告 = "err(10001)";
47-
public static final String MASS_ST_涉嫌政治 = "err(20001)";
48-
public static final String MASS_ST_涉嫌社会 = "err(20004)";
49-
public static final String MASS_ST_涉嫌色情 = "err(20002)";
50-
public static final String MASS_ST_涉嫌违法犯罪 = "err(20006)";
51-
public static final String MASS_ST_涉嫌欺诈 = "err(20008)";
52-
public static final String MASS_ST_涉嫌版权 = "err(20013)";
53-
public static final String MASS_ST_涉嫌互推_互相宣传 = "err(22000)";
54-
public static final String MASS_ST_涉嫌其他 = "err(21000)";
55-
56-
/**
57-
* 群发反馈消息代码所对应的文字描述
58-
*/
59-
public static final Map<String, String> MASS_ST_2_DESC = new HashMap<String, String>();
60-
static {
61-
MASS_ST_2_DESC.put(MASS_ST_SUCCESS, "发送成功");
62-
MASS_ST_2_DESC.put(MASS_ST_FAIL, "发送失败");
63-
MASS_ST_2_DESC.put(MASS_ST_涉嫌广告, "涉嫌广告");
64-
MASS_ST_2_DESC.put(MASS_ST_涉嫌政治, "涉嫌政治");
65-
MASS_ST_2_DESC.put(MASS_ST_涉嫌社会, "涉嫌社会");
66-
MASS_ST_2_DESC.put(MASS_ST_涉嫌色情, "涉嫌色情");
67-
MASS_ST_2_DESC.put(MASS_ST_涉嫌违法犯罪, "涉嫌违法犯罪");
68-
MASS_ST_2_DESC.put(MASS_ST_涉嫌欺诈, "涉嫌欺诈");
69-
MASS_ST_2_DESC.put(MASS_ST_涉嫌版权, "涉嫌版权");
70-
MASS_ST_2_DESC.put(MASS_ST_涉嫌互推_互相宣传, "涉嫌互推_互相宣传");
71-
MASS_ST_2_DESC.put(MASS_ST_涉嫌其他, "涉嫌其他");
72-
}
73-
30+
public static final String CUSTOM_MSG_FILE = "file";
31+
7432
///////////////////////
7533
// 微信端推送过来的事件类型
7634
///////////////////////

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.List;
77

88
import me.chanjar.weixin.enterprise.bean.*;
9-
import me.chanjar.weixin.enterprise.bean.WxDepartment;
9+
import me.chanjar.weixin.enterprise.bean.WxCpDepartment;
1010
import me.chanjar.weixin.enterprise.bean.result.WxMediaUploadResult;
1111
import me.chanjar.weixin.enterprise.bean.result.WxUser;
1212
import me.chanjar.weixin.enterprise.exception.WxErrorException;
@@ -91,7 +91,7 @@ public interface WxCpService {
9191
* @param message
9292
* @throws WxErrorException
9393
*/
94-
public void messageSend(WxCustomMessage message) throws WxErrorException;
94+
public void messageSend(WxCpMessage message) throws WxErrorException;
9595

9696
/**
9797
* <pre>
@@ -131,7 +131,7 @@ public interface WxCpService {
131131
* @param name 分组名字(30个字符以内)
132132
* @throws WxErrorException
133133
*/
134-
public WxDepartment departmentCreate(String name) throws WxErrorException;
134+
public WxCpDepartment departmentCreate(String name) throws WxErrorException;
135135

136136
/**
137137
* <pre>
@@ -141,7 +141,7 @@ public interface WxCpService {
141141
* @return
142142
* @throws WxErrorException
143143
*/
144-
public List<WxDepartment> departmentGet() throws WxErrorException;
144+
public List<WxCpDepartment> departmentGet() throws WxErrorException;
145145

146146
/**
147147
* <pre>
@@ -153,7 +153,7 @@ public interface WxCpService {
153153
* @param group 要更新的group,group的id,name必须设置
154154
* @throws WxErrorException
155155
*/
156-
public void departmentUpdate(WxDepartment group) throws WxErrorException;
156+
public void departmentUpdate(WxCpDepartment group) throws WxErrorException;
157157

158158
/**
159159
* <pre>
@@ -163,7 +163,7 @@ public interface WxCpService {
163163
* @param department
164164
* @throws WxErrorException
165165
*/
166-
public void departmentDelete(WxDepartment department) throws WxErrorException;
166+
public void departmentDelete(WxCpDepartment department) throws WxErrorException;
167167

168168
public void userCreate(WxUser user) throws WxErrorException;
169169

weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpServiceImpl.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.concurrent.atomic.AtomicBoolean;
1010

1111
import me.chanjar.weixin.enterprise.bean.*;
12-
import me.chanjar.weixin.enterprise.util.crypto.WxCryptUtil;
1312
import me.chanjar.weixin.enterprise.util.http.SimpleGetRequestExecutor;
1413
import me.chanjar.weixin.enterprise.util.crypto.SHA1;
1514
import org.apache.commons.lang3.StringUtils;
@@ -20,7 +19,7 @@
2019
import org.apache.http.impl.client.CloseableHttpClient;
2120
import org.apache.http.impl.client.HttpClients;
2221

23-
import me.chanjar.weixin.enterprise.bean.WxDepartment;
22+
import me.chanjar.weixin.enterprise.bean.WxCpDepartment;
2423
import me.chanjar.weixin.enterprise.bean.result.WxError;
2524
import me.chanjar.weixin.enterprise.bean.result.WxMediaUploadResult;
2625
import me.chanjar.weixin.enterprise.bean.result.WxUser;
@@ -98,8 +97,8 @@ public void accessTokenRefresh() throws WxErrorException {
9897
}
9998
}
10099

101-
public void messageSend(WxCustomMessage message) throws WxErrorException {
102-
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
100+
public void messageSend(WxCpMessage message) throws WxErrorException {
101+
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
103102
execute(new SimplePostRequestExecutor(), url, message.toJson());
104103
}
105104

@@ -143,7 +142,7 @@ public File mediaDownload(String media_id) throws WxErrorException {
143142
}
144143

145144

146-
public WxDepartment departmentCreate(String name) throws WxErrorException {
145+
public WxCpDepartment departmentCreate(String name) throws WxErrorException {
147146
// TODO
148147
String url = "https://api.weixin.qq.com/cgi-bin/groups/create";
149148
JsonObject json = new JsonObject();
@@ -155,16 +154,16 @@ public WxDepartment departmentCreate(String name) throws WxErrorException {
155154
new SimplePostRequestExecutor(),
156155
url,
157156
json.toString());
158-
return WxDepartment.fromJson(responseContent);
157+
return WxCpDepartment.fromJson(responseContent);
159158
}
160159

161-
public void departmentUpdate(WxDepartment group) throws WxErrorException {
160+
public void departmentUpdate(WxCpDepartment group) throws WxErrorException {
162161
// TODO
163162
String url = "https://api.weixin.qq.com/cgi-bin/groups/update";
164163
execute(new SimplePostRequestExecutor(), url, group.toJson());
165164
}
166165

167-
public List<WxDepartment> departmentGet() throws WxErrorException {
166+
public List<WxCpDepartment> departmentGet() throws WxErrorException {
168167
// TODO
169168
String url = "https://api.weixin.qq.com/cgi-bin/groups/get";
170169
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
@@ -174,11 +173,11 @@ public List<WxDepartment> departmentGet() throws WxErrorException {
174173
*/
175174
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
176175
return WxGsonBuilder.INSTANCE.create()
177-
.fromJson(tmpJsonElement.getAsJsonObject().get("groups"), new TypeToken<List<WxDepartment>>() {
176+
.fromJson(tmpJsonElement.getAsJsonObject().get("groups"), new TypeToken<List<WxCpDepartment>>() {
178177
}.getType());
179178
}
180179

181-
public void departmentDelete(WxDepartment department) throws WxErrorException {
180+
public void departmentDelete(WxCpDepartment department) throws WxErrorException {
182181
// TODO
183182

184183
}

weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxInMemoryCpConfigStorage.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,17 @@ public String getAgentId() {
8282
public void setAgentId(String agentId) {
8383
this.agentId = agentId;
8484
}
85+
86+
@Override
87+
public String toString() {
88+
return "WxInMemoryCpConfigStorage{" +
89+
"corpId='" + corpId + '\'' +
90+
", corpSecret='" + corpSecret + '\'' +
91+
", token='" + token + '\'' +
92+
", accessToken='" + accessToken + '\'' +
93+
", aesKey='" + aesKey + '\'' +
94+
", agentId='" + agentId + '\'' +
95+
", expiresIn=" + expiresIn +
96+
'}';
97+
}
8598
}

weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/WxDepartment.java renamed to weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/WxCpDepartment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Daniel Qian
88
*
99
*/
10-
public class WxDepartment {
10+
public class WxCpDepartment {
1111

1212
private long id = -1;
1313
private String name;
@@ -31,8 +31,8 @@ public void setCount(long count) {
3131
this.count = count;
3232
}
3333

34-
public static WxDepartment fromJson(String json) {
35-
return WxGsonBuilder.create().fromJson(json, WxDepartment.class);
34+
public static WxCpDepartment fromJson(String json) {
35+
return WxGsonBuilder.create().fromJson(json, WxCpDepartment.class);
3636
}
3737

3838
public String toJson() {

weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/WxCustomMessage.java renamed to weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/WxCpMessage.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@
44
import java.util.List;
55

66
import me.chanjar.weixin.enterprise.api.WxConsts;
7-
import me.chanjar.weixin.enterprise.bean.custombuilder.ImageBuilder;
8-
import me.chanjar.weixin.enterprise.bean.custombuilder.MusicBuilder;
9-
import me.chanjar.weixin.enterprise.bean.custombuilder.NewsBuilder;
10-
import me.chanjar.weixin.enterprise.bean.custombuilder.TextBuilder;
11-
import me.chanjar.weixin.enterprise.bean.custombuilder.VideoBuilder;
12-
import me.chanjar.weixin.enterprise.bean.custombuilder.VoiceBuilder;
7+
import me.chanjar.weixin.enterprise.bean.messagebuilder.*;
138
import me.chanjar.weixin.enterprise.util.json.WxGsonBuilder;
149

1510
/**
1611
* 客服消息
1712
* @author Daniel Qian
1813
*
1914
*/
20-
public class WxCustomMessage {
15+
public class WxCpMessage {
2116

2217
private String toUser;
18+
private String toParty;
19+
private String toTag;
20+
private String agentId;
2321
private String msgType;
2422
private String content;
2523
private String mediaId;
@@ -36,6 +34,31 @@ public String getToUser() {
3634
public void setToUser(String toUser) {
3735
this.toUser = toUser;
3836
}
37+
38+
public String getToParty() {
39+
return toParty;
40+
}
41+
42+
public void setToParty(String toParty) {
43+
this.toParty = toParty;
44+
}
45+
46+
public String getToTag() {
47+
return toTag;
48+
}
49+
50+
public void setToTag(String toTag) {
51+
this.toTag = toTag;
52+
}
53+
54+
public String getAgentId() {
55+
return agentId;
56+
}
57+
58+
public void setAgentId(String agentId) {
59+
this.agentId = agentId;
60+
}
61+
3962
public String getMsgType() {
4063
return msgType;
4164
}
@@ -189,5 +212,13 @@ public static MusicBuilder MUSIC() {
189212
public static NewsBuilder NEWS() {
190213
return new NewsBuilder();
191214
}
215+
216+
/**
217+
* 获得文件消息builder
218+
* @return
219+
*/
220+
public static FileBuilder FILE() {
221+
return new FileBuilder();
222+
}
192223

193224
}

weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/custombuilder/BaseBuilder.java

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package me.chanjar.weixin.enterprise.bean.messagebuilder;
2+
3+
import me.chanjar.weixin.enterprise.bean.WxCpMessage;
4+
5+
public class BaseBuilder<T> {
6+
protected String msgType;
7+
protected String agentId;
8+
protected String toUser;
9+
protected String toParty;
10+
protected String toTag;
11+
12+
public T agentId(String agentId) {
13+
this.agentId = agentId;
14+
return (T) this;
15+
}
16+
17+
public T toUser(String toUser) {
18+
this.toUser = toUser;
19+
return (T) this;
20+
}
21+
22+
public T toParty(String toParty) {
23+
this.toParty = toParty;
24+
return (T) this;
25+
}
26+
27+
public T toTag(String toTag) {
28+
this.toTag = toTag;
29+
return (T) this;
30+
}
31+
32+
public WxCpMessage build() {
33+
WxCpMessage m = new WxCpMessage();
34+
m.setAgentId(this.agentId);
35+
m.setMsgType(this.msgType);
36+
m.setToUser(this.toUser);
37+
m.setToParty(this.toParty);
38+
m.setToTag(this.toTag);
39+
return m;
40+
}
41+
42+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package me.chanjar.weixin.enterprise.bean.messagebuilder;
2+
3+
import me.chanjar.weixin.enterprise.api.WxConsts;
4+
import me.chanjar.weixin.enterprise.bean.WxCpMessage;
5+
6+
/**
7+
* 获得消息builder
8+
* <pre>
9+
* 用法: WxCustomMessage m = WxCustomMessage.FILE().mediaId(...).toUser(...).build();
10+
* </pre>
11+
* @author Daniel Qian
12+
*
13+
*/
14+
public final class FileBuilder extends BaseBuilder<FileBuilder> {
15+
private String mediaId;
16+
17+
public FileBuilder() {
18+
this.msgType = WxConsts.CUSTOM_MSG_FILE;
19+
}
20+
21+
public FileBuilder mediaId(String media_id) {
22+
this.mediaId = media_id;
23+
return this;
24+
}
25+
26+
public WxCpMessage build() {
27+
WxCpMessage m = super.build();
28+
m.setMediaId(this.mediaId);
29+
return m;
30+
}
31+
}

0 commit comments

Comments
 (0)