Skip to content

Commit 8a733d9

Browse files
IOManbinarywang
authored andcommitted
binarywang#470 增加小程序模板配置相关接口
1 parent 2629f63 commit 8a733d9

File tree

10 files changed

+380
-0
lines changed

10 files changed

+380
-0
lines changed

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ public interface WxMaService {
121121
*/
122122
WxMaQrcodeService getQrcodeService();
123123

124+
/**
125+
* 返回模板配置相关接口方法的实现类对象, 以方便调用其各个接口
126+
* @return WxMaTemplateService
127+
*/
128+
WxMaTemplateService getTemplateService();
129+
124130
/**
125131
* 初始化http请求对象
126132
*/
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package cn.binarywang.wx.miniapp.api;
2+
3+
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateAddResult;
4+
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryGetResult;
5+
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryListResult;
6+
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateListResult;
7+
import me.chanjar.weixin.common.exception.WxErrorException;
8+
9+
import java.util.List;
10+
11+
public interface WxMaTemplateService {
12+
13+
//获取小程序模板库标题列表
14+
String TEMPLATE_LIBRARY_LIST_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list";
15+
16+
//获取模板库某个模板标题下关键词库
17+
String TEMPLATE_LIBRARY_KEYWORD_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get";
18+
19+
//组合模板并添加至帐号下的个人模板库
20+
String TEMPLATE_ADD_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/add";
21+
22+
//获取帐号下已存在的模板列表
23+
String TEMPLATE_LIST_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/list";
24+
25+
//删除帐号下的某个模板
26+
String TEMPLATE_DEL_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/del";
27+
28+
/**
29+
* <pre>
30+
* 获取小程序模板库标题列表
31+
*
32+
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
33+
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list?access_token=ACCESS_TOKEN
34+
* </pre>
35+
* @param offset
36+
* @param count
37+
* @return
38+
*/
39+
WxMaTemplateLibraryListResult findTemplateLibraryList(int offset, int count) throws WxErrorException;
40+
41+
/**
42+
* <pre>
43+
* 获取模板库某个模板标题下关键词库
44+
*
45+
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
46+
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get?access_token=ACCESS_TOKEN
47+
* </pre>
48+
* @param id
49+
* @return
50+
*/
51+
WxMaTemplateLibraryGetResult findTemplateLibraryKeywordList(String id) throws WxErrorException;
52+
53+
/**
54+
* <pre>
55+
* 组合模板并添加至帐号下的个人模板库
56+
*
57+
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
58+
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/add?access_token=ACCESS_TOKEN
59+
* </pre>
60+
* @param id
61+
* @param keywordIdList
62+
* @return
63+
*/
64+
WxMaTemplateAddResult addTemplate(String id, List<Integer> keywordIdList) throws WxErrorException;
65+
66+
/**
67+
* <pre>
68+
* 获取帐号下已存在的模板列表
69+
*
70+
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
71+
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=ACCESS_TOKEN
72+
* </pre>
73+
* @param offset
74+
* @param count
75+
* @return
76+
*/
77+
WxMaTemplateListResult findTemplateList(int offset, int count) throws WxErrorException;
78+
79+
/**
80+
* <pre>
81+
* 删除帐号下的某个模板
82+
*
83+
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
84+
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=ACCESS_TOKEN
85+
* </pre>
86+
* @param templateId
87+
*/
88+
boolean delTemplate(String templateId) throws WxErrorException;
89+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
3838
private WxMaMediaService materialService = new WxMaMediaServiceImpl(this);
3939
private WxMaUserService userService = new WxMaUserServiceImpl(this);
4040
private WxMaQrcodeService qrCodeService = new WxMaQrcodeServiceImpl(this);
41+
private WxMaTemplateService templateService = new WxMaTemplateServiceImpl(this);
4142

4243
private int retrySleepMillis = 1000;
4344
private int maxRetryTimes = 5;
@@ -259,4 +260,9 @@ public WxMaUserService getUserService() {
259260
public WxMaQrcodeService getQrcodeService() {
260261
return this.qrCodeService;
261262
}
263+
264+
@Override
265+
public WxMaTemplateService getTemplateService() {
266+
return this.templateService;
267+
}
262268
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaService;
4+
import cn.binarywang.wx.miniapp.api.WxMaTemplateService;
5+
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateAddResult;
6+
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryGetResult;
7+
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryListResult;
8+
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateListResult;
9+
import me.chanjar.weixin.common.bean.result.WxError;
10+
import me.chanjar.weixin.common.exception.WxErrorException;
11+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
12+
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
public class WxMaTemplateServiceImpl implements WxMaTemplateService {
18+
19+
private WxMaService wxMaService;
20+
21+
public WxMaTemplateServiceImpl(WxMaService wxMaService){
22+
this.wxMaService = wxMaService;
23+
}
24+
25+
@Override
26+
public WxMaTemplateLibraryListResult findTemplateLibraryList(int offset, int count) throws WxErrorException {
27+
28+
Map<String, Integer> params = new HashMap<>();
29+
params.put("offset", offset);
30+
params.put("count", count);
31+
32+
String responseText = this.wxMaService.post(TEMPLATE_LIBRARY_LIST_URL, WxGsonBuilder.create().toJson(params));
33+
WxError wxError = WxError.fromJson(responseText);
34+
if(wxError.getErrorCode() == 0){
35+
return WxMaTemplateLibraryListResult.fromJson(responseText);
36+
}else {
37+
throw new WxErrorException(wxError);
38+
}
39+
}
40+
41+
@Override
42+
public WxMaTemplateLibraryGetResult findTemplateLibraryKeywordList(String id) throws WxErrorException {
43+
44+
Map<String, String> params = new HashMap<>();
45+
params.put("id", id);
46+
47+
String responseText = this.wxMaService.post(TEMPLATE_LIBRARY_KEYWORD_URL, WxGsonBuilder.create().toJson(params));
48+
WxError wxError = WxError.fromJson(responseText);
49+
if(wxError.getErrorCode() == 0){
50+
return WxMaTemplateLibraryGetResult.fromJson(responseText);
51+
}else {
52+
throw new WxErrorException(wxError);
53+
}
54+
}
55+
56+
@Override
57+
public WxMaTemplateAddResult addTemplate(String id, List<Integer> keywordIdList) throws WxErrorException {
58+
59+
Map<String, Object> params = new HashMap<>();
60+
params.put("id", id);
61+
params.put("keyword_id_list", keywordIdList.toArray());
62+
63+
String responseText = this.wxMaService.post(TEMPLATE_ADD_URL, WxGsonBuilder.create().toJson(params));
64+
WxError wxError = WxError.fromJson(responseText);
65+
if(wxError.getErrorCode() == 0){
66+
return WxMaTemplateAddResult.fromJson(responseText);
67+
}else {
68+
throw new WxErrorException(wxError);
69+
}
70+
}
71+
72+
@Override
73+
public WxMaTemplateListResult findTemplateList(int offset, int count) throws WxErrorException {
74+
75+
Map<String, Integer> params = new HashMap<>();
76+
params.put("offset", offset);
77+
params.put("count", count);
78+
79+
String responseText = this.wxMaService.post(TEMPLATE_LIST_URL, WxGsonBuilder.create().toJson(params));
80+
WxError wxError = WxError.fromJson(responseText);
81+
if(wxError.getErrorCode() == 0){
82+
return WxMaTemplateListResult.fromJson(responseText);
83+
}else {
84+
throw new WxErrorException(wxError);
85+
}
86+
}
87+
88+
@Override
89+
public boolean delTemplate(String templateId) throws WxErrorException {
90+
Map<String, String> params = new HashMap<>();
91+
params.put("template_id", templateId);
92+
93+
String responseText = this.wxMaService.post(TEMPLATE_DEL_URL, WxGsonBuilder.create().toJson(params));
94+
WxError wxError = WxError.fromJson(responseText);
95+
if(wxError.getErrorCode() == 0){
96+
return true;
97+
}else {
98+
throw new WxErrorException(wxError);
99+
}
100+
}
101+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.binarywang.wx.miniapp.bean.template;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
6+
7+
import java.io.Serializable;
8+
9+
@Data
10+
public class WxMaTemplateAddResult implements Serializable{
11+
12+
private static final long serialVersionUID = 872250961973834465L;
13+
14+
@SerializedName("template_id")
15+
private String templateId;
16+
17+
public static WxMaTemplateAddResult fromJson(String json){
18+
return WxGsonBuilder.create().fromJson(json, WxMaTemplateAddResult.class);
19+
}
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cn.binarywang.wx.miniapp.bean.template;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
6+
7+
import java.io.Serializable;
8+
import java.util.List;
9+
10+
@Data
11+
public class WxMaTemplateLibraryGetResult implements Serializable{
12+
13+
private static final long serialVersionUID = -190847592776636744L;
14+
private String id;
15+
private String title;
16+
@SerializedName("keyword_list")
17+
private List<KeywordInfo> keywordList;
18+
19+
@Data
20+
public static class KeywordInfo{
21+
22+
@SerializedName("keyword_id")
23+
private int keywordId;
24+
private String name;
25+
private String example;
26+
}
27+
28+
public static WxMaTemplateLibraryGetResult fromJson(String json){
29+
return WxGsonBuilder.create().fromJson(json, WxMaTemplateLibraryGetResult.class);
30+
}
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.binarywang.wx.miniapp.bean.template;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
6+
7+
import java.io.Serializable;
8+
import java.util.List;
9+
10+
@Data
11+
public class WxMaTemplateLibraryListResult implements Serializable{
12+
private static final long serialVersionUID = -2780782521447602209L;
13+
14+
@SerializedName("total_count")
15+
private int totalCount;
16+
private List<TemplateItem> list;
17+
18+
public static WxMaTemplateLibraryListResult fromJson(String json){
19+
return WxGsonBuilder.create().fromJson(json, WxMaTemplateLibraryListResult.class);
20+
}
21+
22+
@Data
23+
public static class TemplateItem{
24+
25+
private String id;
26+
private String title;
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.binarywang.wx.miniapp.bean.template;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
6+
7+
import java.io.Serializable;
8+
import java.util.List;
9+
10+
@Data
11+
public class WxMaTemplateListResult implements Serializable{
12+
13+
private static final long serialVersionUID = -7430535579782184537L;
14+
private List<TemplateInfo> list;
15+
16+
public static WxMaTemplateListResult fromJson(String json){
17+
return WxGsonBuilder.create().fromJson(json, WxMaTemplateListResult.class);
18+
}
19+
20+
@Data
21+
public static class TemplateInfo{
22+
23+
@SerializedName("template_id")
24+
private String templateId;
25+
private String title;
26+
private String content;
27+
private String example;
28+
}
29+
}

0 commit comments

Comments
 (0)