Skip to content

Commit a69c972

Browse files
committed
issue binarywang#24 模板消息接口
1 parent 46e66d3 commit a69c972

7 files changed

Lines changed: 181 additions & 2 deletions

File tree

‎weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class WxConsts {
8888
public static final String EVT_PIC_PHOTO_OR_ALBUM = "pic_photo_or_album";
8989
public static final String EVT_PIC_WEIXIN = "pic_weixin";
9090
public static final String EVT_LOCATION_SELECT = "location_select";
91+
public static final String EVT_TEMPLATESENDJOBFINISH = "TEMPLATESENDJOBFINISH";
9192

9293
///////////////////////
9394
// 上传多媒体文件的类型

‎weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,17 @@ public interface WxMpService {
306306
* @throws WxErrorException
307307
*/
308308
public String shortUrl(String long_url) throws WxErrorException;
309-
309+
310+
/**
311+
* <pre>
312+
* 发送模板消息
313+
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=模板消息接口
314+
* </pre>
315+
* @param templateMessage
316+
* @throws WxErrorException
317+
*/
318+
public void templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException;
319+
310320
/**
311321
* 注入 {@link WxMpConfigStorage} 的实现
312322
* @param wxConfigProvider

‎weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.InputStream;
3232
import java.io.StringReader;
3333
import java.util.List;
34+
import java.util.Map;
3435
import java.util.UUID;
3536
import java.util.concurrent.atomic.AtomicBoolean;
3637

@@ -272,7 +273,12 @@ public String shortUrl(String long_url) throws WxErrorException {
272273
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
273274
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
274275
}
275-
276+
277+
public void templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException {
278+
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send";
279+
execute(new SimplePostRequestExecutor(), url, templateMessage.toJson());
280+
}
281+
276282
/**
277283
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
278284
* @param executor
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package me.chanjar.weixin.mp.bean;
2+
3+
/**
4+
* @author Daniel Qian
5+
*/
6+
public class WxMpTemplateData {
7+
8+
private String name;
9+
private String value;
10+
private String color;
11+
12+
public WxMpTemplateData() {
13+
}
14+
15+
public WxMpTemplateData(String name, String value) {
16+
this.name = name;
17+
this.value = value;
18+
}
19+
20+
public WxMpTemplateData(String name, String value, String color) {
21+
this.name = name;
22+
this.value = value;
23+
this.color = color;
24+
}
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public String getValue() {
35+
return value;
36+
}
37+
38+
public void setValue(String value) {
39+
this.value = value;
40+
}
41+
42+
public String getColor() {
43+
return color;
44+
}
45+
public void setColor(String color) {
46+
this.color = color;
47+
}
48+
49+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package me.chanjar.weixin.mp.bean;
2+
3+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* Created by qianjia on 14/11/1.
10+
*/
11+
public class WxMpTemplateMessage {
12+
13+
private String toUser;
14+
private String templateId;
15+
private String url;
16+
private String topColor;
17+
private List<WxMpTemplateData> datas = new ArrayList<WxMpTemplateData>();
18+
19+
public String getToUser() {
20+
return toUser;
21+
}
22+
23+
public void setToUser(String toUser) {
24+
this.toUser = toUser;
25+
}
26+
27+
public String getTemplateId() {
28+
return templateId;
29+
}
30+
31+
public void setTemplateId(String templateId) {
32+
this.templateId = templateId;
33+
}
34+
35+
public String getUrl() {
36+
return url;
37+
}
38+
39+
public void setUrl(String url) {
40+
this.url = url;
41+
}
42+
43+
public String getTopColor() {
44+
return topColor;
45+
}
46+
47+
public void setTopColor(String topColor) {
48+
this.topColor = topColor;
49+
}
50+
51+
public List<WxMpTemplateData> getDatas() {
52+
return datas;
53+
}
54+
55+
public void setDatas(List<WxMpTemplateData> datas) {
56+
this.datas = datas;
57+
}
58+
59+
public String toJson() {
60+
return WxMpGsonBuilder.INSTANCE.create().toJson(this);
61+
}
62+
}

‎weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class WxMpGsonBuilder {
2222
INSTANCE.registerTypeAdapter(WxMpMassSendResult.class, new WxMpMassSendResultAdapter());
2323
INSTANCE.registerTypeAdapter(WxMpMassUploadResult.class, new WxMpMassUploadResultAdapter());
2424
INSTANCE.registerTypeAdapter(WxMpQrCodeTicket.class, new WxQrCodeTicketAdapter());
25+
INSTANCE.registerTypeAdapter(WxMpTemplateMessage.class, new WxMpTemplateMessageGsonAdapter());
2526
}
2627

2728
public static Gson create() {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
3+
*
4+
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
5+
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
6+
* arose from modification of the original source, or other redistribution of this source
7+
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
8+
*/
9+
package me.chanjar.weixin.mp.util.json;
10+
11+
import com.google.gson.*;
12+
import me.chanjar.weixin.common.api.WxConsts;
13+
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
14+
import me.chanjar.weixin.mp.bean.WxMpTemplateData;
15+
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
16+
17+
import java.lang.reflect.Type;
18+
19+
/**
20+
* @author qianjia
21+
*/
22+
public class WxMpTemplateMessageGsonAdapter implements JsonSerializer<WxMpTemplateMessage> {
23+
24+
public JsonElement serialize(WxMpTemplateMessage message, Type typeOfSrc, JsonSerializationContext context) {
25+
JsonObject messageJson = new JsonObject();
26+
messageJson.addProperty("touser", message.getToUser());
27+
messageJson.addProperty("template_id", message.getTemplateId());
28+
if (message.getUrl() != null) {
29+
messageJson.addProperty("url", message.getUrl());
30+
}
31+
if (message.getTopColor() != null) {
32+
messageJson.addProperty("topcolor", message.getTopColor());
33+
}
34+
35+
JsonObject datas = new JsonObject();
36+
messageJson.add("data", datas);
37+
38+
for (WxMpTemplateData data : message.getDatas()) {
39+
JsonObject dataJson = new JsonObject();
40+
dataJson.addProperty("value", data.getValue());
41+
if (data.getColor() != null) {
42+
dataJson.addProperty("color", data.getColor());
43+
}
44+
datas.add(data.getName(), dataJson);
45+
}
46+
47+
return messageJson;
48+
}
49+
50+
}

0 commit comments

Comments
 (0)