Skip to content

Commit 6dc0481

Browse files
committed
添加获得模板列表的接口方法,并重构模板消息相关类包结构 for issue #63
1 parent d7298ab commit 6dc0481

File tree

10 files changed

+196
-36
lines changed

10 files changed

+196
-36
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpTemplateMsgService.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package me.chanjar.weixin.mp.api;
22

3+
import java.util.List;
4+
35
import me.chanjar.weixin.common.exception.WxErrorException;
4-
import me.chanjar.weixin.mp.bean.WxMpIndustry;
5-
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
6+
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
7+
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
8+
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
69

710
/**
811
* <pre>
@@ -24,7 +27,7 @@ public interface WxMpTemplateMsgService {
2427
*
2528
* @return 是否成功
2629
*/
27-
boolean setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException;
30+
boolean setIndustry(WxMpTemplateIndustry wxMpIndustry) throws WxErrorException;
2831

2932
/***
3033
* <pre>
@@ -34,7 +37,7 @@ public interface WxMpTemplateMsgService {
3437
*
3538
* @return wxMpIndustry
3639
*/
37-
WxMpIndustry getIndustry() throws WxErrorException;
40+
WxMpTemplateIndustry getIndustry() throws WxErrorException;
3841

3942
/**
4043
* <pre>
@@ -53,8 +56,20 @@ public interface WxMpTemplateMsgService {
5356
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
5457
* 接口地址格式:https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN
5558
* </pre>
56-
*@param shortTemplateId 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
59+
* @param shortTemplateId 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
5760
* @return templateId 模板Id
5861
*/
5962
String addTemplate(String shortTemplateId) throws WxErrorException;
63+
64+
/**
65+
* <pre>
66+
* 获取模板列表
67+
* 获取已添加至帐号下所有模板列表,可在MP中查看模板列表信息,为方便第三方开发者,提供通过接口调用的方式来获取帐号下所有模板信息
68+
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
69+
* 接口地址格式:https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN
70+
* </pre>
71+
*
72+
* @return templateId 模板Id
73+
*/
74+
List<WxMpTemplate> getAllPrivateTemplate() throws WxErrorException;
6075
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImpl.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3+
import java.util.List;
4+
35
import com.google.gson.JsonObject;
46
import com.google.gson.JsonParser;
57

68
import me.chanjar.weixin.common.bean.result.WxError;
79
import me.chanjar.weixin.common.exception.WxErrorException;
810
import me.chanjar.weixin.mp.api.WxMpService;
911
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
10-
import me.chanjar.weixin.mp.bean.WxMpIndustry;
11-
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
12+
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
13+
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
14+
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
1215

1316
/**
1417
* <pre>
@@ -38,7 +41,7 @@ public String sendTemplateMsg(WxMpTemplateMessage templateMessage) throws WxErro
3841
}
3942

4043
@Override
41-
public boolean setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException {
44+
public boolean setIndustry(WxMpTemplateIndustry wxMpIndustry) throws WxErrorException {
4245
if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId()
4346
|| null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) {
4447
throw new IllegalArgumentException("行业Id不能为空,请核实");
@@ -50,10 +53,10 @@ public boolean setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException {
5053
}
5154

5255
@Override
53-
public WxMpIndustry getIndustry() throws WxErrorException {
56+
public WxMpTemplateIndustry getIndustry() throws WxErrorException {
5457
String url = API_URL_PREFIX + "/get_industry";
5558
String responseContent = this.wxMpService.get(url, null);
56-
return WxMpIndustry.fromJson(responseContent);
59+
return WxMpTemplateIndustry.fromJson(responseContent);
5760
}
5861

5962
@Override
@@ -70,4 +73,10 @@ public String addTemplate(String shortTemplateId) throws WxErrorException {
7073
throw new WxErrorException(WxError.fromJson(responseContent));
7174
}
7275

76+
@Override
77+
public List<WxMpTemplate> getAllPrivateTemplate() throws WxErrorException {
78+
String url = API_URL_PREFIX + "/get_all_private_template";
79+
return WxMpTemplate.fromJson(this.wxMpService.get(url, null));
80+
}
81+
7382
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package me.chanjar.weixin.mp.bean.template;
2+
3+
import java.util.List;
4+
5+
import org.apache.commons.lang3.builder.ToStringBuilder;
6+
import org.apache.commons.lang3.builder.ToStringStyle;
7+
8+
import com.google.gson.JsonParser;
9+
import com.google.gson.annotations.SerializedName;
10+
import com.google.gson.reflect.TypeToken;
11+
12+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
13+
14+
/**
15+
* <pre>
16+
* 模板列表信息
17+
* Created by Binary Wang on 2016-10-17.
18+
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
19+
* </pre>
20+
*/
21+
public class WxMpTemplate {
22+
23+
private static final JsonParser JSON_PARSER = new JsonParser();
24+
25+
public static List<WxMpTemplate> fromJson(String json) {
26+
return WxMpGsonBuilder.create().fromJson(JSON_PARSER.parse(json).getAsJsonObject().get("template_list"),
27+
new TypeToken<List<WxMpTemplate>>() {
28+
}.getType());
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
34+
}
35+
36+
/**
37+
* template_id
38+
* 模板ID
39+
*/
40+
@SerializedName("template_id")
41+
private String templateId;
42+
43+
/**
44+
* title
45+
* 模板标题
46+
*/
47+
@SerializedName("title")
48+
private String title;
49+
50+
/**
51+
* primary_industry
52+
* 模板所属行业的一级行业
53+
*/
54+
@SerializedName("primary_industry")
55+
private String primaryIndustry;
56+
57+
/**
58+
* deputy_industry
59+
* 模板所属行业的二级行业
60+
*/
61+
@SerializedName("deputy_industry")
62+
private String deputyIndustry;
63+
64+
/**
65+
* content
66+
* 模板内容
67+
*/
68+
@SerializedName("content")
69+
private String content;
70+
71+
/**
72+
* example
73+
* 模板示例
74+
*/
75+
@SerializedName("example")
76+
private String example;
77+
78+
public String getTemplateId() {
79+
return templateId;
80+
}
81+
82+
public void setTemplateId(String templateId) {
83+
this.templateId = templateId;
84+
}
85+
86+
public String getTitle() {
87+
return title;
88+
}
89+
90+
public void setTitle(String title) {
91+
this.title = title;
92+
}
93+
94+
public String getPrimaryIndustry() {
95+
return primaryIndustry;
96+
}
97+
98+
public void setPrimaryIndustry(String primaryIndustry) {
99+
this.primaryIndustry = primaryIndustry;
100+
}
101+
102+
public String getDeputyIndustry() {
103+
return deputyIndustry;
104+
}
105+
106+
public void setDeputyIndustry(String deputyIndustry) {
107+
this.deputyIndustry = deputyIndustry;
108+
}
109+
110+
public String getContent() {
111+
return content;
112+
}
113+
114+
public void setContent(String content) {
115+
this.content = content;
116+
}
117+
118+
public String getExample() {
119+
return example;
120+
}
121+
122+
public void setExample(String example) {
123+
this.example = example;
124+
}
125+
126+
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpTemplateData.java renamed to weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.chanjar.weixin.mp.bean;
1+
package me.chanjar.weixin.mp.bean.template;
22

33
import java.io.Serializable;
44

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpIndustry.java renamed to weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateIndustry.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.chanjar.weixin.mp.bean;
1+
package me.chanjar.weixin.mp.bean.template;
22

33

44
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
@@ -10,15 +10,15 @@
1010
/**
1111
* @author miller
1212
*/
13-
public class WxMpIndustry implements Serializable {
13+
public class WxMpTemplateIndustry implements Serializable {
1414
private static final long serialVersionUID = -7700398224795914722L;
1515
private Industry primaryIndustry;
1616
private Industry secondIndustry;
1717

18-
public WxMpIndustry() {
18+
public WxMpTemplateIndustry() {
1919
}
2020

21-
public WxMpIndustry(Industry primaryIndustry, Industry secondIndustry) {
21+
public WxMpTemplateIndustry(Industry primaryIndustry, Industry secondIndustry) {
2222
this.primaryIndustry = primaryIndustry;
2323
this.secondIndustry = secondIndustry;
2424
}
@@ -81,8 +81,8 @@ public String toString() {
8181
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
8282
}
8383

84-
public static WxMpIndustry fromJson(String json) {
85-
return WxMpGsonBuilder.create().fromJson(json, WxMpIndustry.class);
84+
public static WxMpTemplateIndustry fromJson(String json) {
85+
return WxMpGsonBuilder.create().fromJson(json, WxMpTemplateIndustry.class);
8686
}
8787

8888
public String toJson() {

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpTemplateMessage.java renamed to weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.chanjar.weixin.mp.bean;
1+
package me.chanjar.weixin.mp.bean.template;
22

33
import java.io.Serializable;
44
import java.util.ArrayList;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55

66
import me.chanjar.weixin.mp.bean.WxMpCard;
77
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
8-
import me.chanjar.weixin.mp.bean.WxMpIndustry;
98
import me.chanjar.weixin.mp.bean.WxMpMassNews;
109
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
1110
import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage;
1211
import me.chanjar.weixin.mp.bean.WxMpMassTagMessage;
1312
import me.chanjar.weixin.mp.bean.WxMpMassVideo;
1413
import me.chanjar.weixin.mp.bean.WxMpMaterialArticleUpdate;
1514
import me.chanjar.weixin.mp.bean.WxMpMaterialNews;
16-
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
1715
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserCumulate;
1816
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
1917
import me.chanjar.weixin.mp.bean.result.WxMediaImgUploadResult;
@@ -31,6 +29,8 @@
3129
import me.chanjar.weixin.mp.bean.result.WxMpUser;
3230
import me.chanjar.weixin.mp.bean.result.WxMpUserBlacklistGetResult;
3331
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
32+
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
33+
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
3434

3535
public class WxMpGsonBuilder {
3636

@@ -68,7 +68,7 @@ public class WxMpGsonBuilder {
6868
INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter());
6969
INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter());
7070
INSTANCE.registerTypeAdapter(WxMediaImgUploadResult.class, new WxMediaImgUploadResultGsonAdapter());
71-
INSTANCE.registerTypeAdapter(WxMpIndustry.class, new WxMpIndustryGsonAdapter());
71+
INSTANCE.registerTypeAdapter(WxMpTemplateIndustry.class, new WxMpIndustryGsonAdapter());
7272
INSTANCE.registerTypeAdapter(WxMpUserBlacklistGetResult.class, new WxUserBlacklistGetResultGsonAdapter());
7373
}
7474

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
import com.google.gson.*;
44
import me.chanjar.weixin.common.util.json.GsonHelper;
5-
import me.chanjar.weixin.mp.bean.WxMpIndustry;
5+
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
66

77
import java.lang.reflect.Type;
88

99
/**
1010
* @author miller
1111
*/
1212
public class WxMpIndustryGsonAdapter
13-
implements JsonSerializer<WxMpIndustry>, JsonDeserializer<WxMpIndustry> {
13+
implements JsonSerializer<WxMpTemplateIndustry>, JsonDeserializer<WxMpTemplateIndustry> {
1414
@Override
15-
public JsonElement serialize(WxMpIndustry wxMpIndustry, Type type,
15+
public JsonElement serialize(WxMpTemplateIndustry wxMpIndustry, Type type,
1616
JsonSerializationContext jsonSerializationContext) {
1717
JsonObject json = new JsonObject();
1818
json.addProperty("industry_id1", wxMpIndustry.getPrimaryIndustry().getId());
@@ -21,10 +21,10 @@ public JsonElement serialize(WxMpIndustry wxMpIndustry, Type type,
2121
}
2222

2323
@Override
24-
public WxMpIndustry deserialize(JsonElement jsonElement, Type type,
24+
public WxMpTemplateIndustry deserialize(JsonElement jsonElement, Type type,
2525
JsonDeserializationContext jsonDeserializationContext)
2626
throws JsonParseException {
27-
WxMpIndustry wxMpIndustry = new WxMpIndustry();
27+
WxMpTemplateIndustry wxMpIndustry = new WxMpTemplateIndustry();
2828
JsonObject primaryIndustry = jsonElement.getAsJsonObject()
2929
.get("primary_industry").getAsJsonObject();
3030
wxMpIndustry.setPrimaryIndustry(convertFromJson(primaryIndustry));
@@ -34,8 +34,8 @@ public WxMpIndustry deserialize(JsonElement jsonElement, Type type,
3434
return wxMpIndustry;
3535
}
3636

37-
private static WxMpIndustry.Industry convertFromJson(JsonObject json) {
38-
WxMpIndustry.Industry industry = new WxMpIndustry.Industry();
37+
private static WxMpTemplateIndustry.Industry convertFromJson(JsonObject json) {
38+
WxMpTemplateIndustry.Industry industry = new WxMpTemplateIndustry.Industry();
3939
industry.setFirstClass(GsonHelper.getString(json, "first_class"));
4040
industry.setSecondClass(GsonHelper.getString(json, "second_class"));
4141
return industry;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
import com.google.gson.JsonObject;
1313
import com.google.gson.JsonSerializationContext;
1414
import com.google.gson.JsonSerializer;
15-
import me.chanjar.weixin.mp.bean.WxMpTemplateData;
16-
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
15+
16+
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
17+
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
1718

1819
import java.lang.reflect.Type;
1920

0 commit comments

Comments
 (0)