Skip to content

Commit 75c038d

Browse files
charmingohbinarywang
authored andcommitted
#560 微信开放平台:增加小程序代码模板库管理
* 微信开放平台:1. WxOpenInRedisConfigStorage 支持 JedisPool/JedisSentinelPool 等 Pool<Jedis> 的子类;2. WxOpenInRedisConfigStorage 增加 keyPrefix 以支持可配置的前缀; * 微信开放平台:增加小程序代码模板库管理
1 parent 3481cca commit 75c038d

File tree

3 files changed

+133
-3
lines changed

3 files changed

+133
-3
lines changed

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenComponentService.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
import me.chanjar.weixin.common.exception.WxErrorException;
66
import me.chanjar.weixin.mp.api.WxMpService;
77
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
8+
import me.chanjar.weixin.open.bean.WxOpenMaCodeTemplate;
89
import me.chanjar.weixin.open.bean.message.WxOpenXmlMessage;
910
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
1011
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerOptionResult;
1112
import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult;
1213

14+
import java.util.List;
15+
1316
/**
1417
* @author <a href="https://github.com/007gzs">007</a>
1518
*/
1619
public interface WxOpenComponentService {
17-
1820
String API_COMPONENT_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
1921
String API_CREATE_PREAUTHCODE_URL = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode";
2022
String API_QUERY_AUTH_URL = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth";
@@ -23,7 +25,6 @@ public interface WxOpenComponentService {
2325
String API_GET_AUTHORIZER_OPTION_URL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_option";
2426
String API_SET_AUTHORIZER_OPTION_URL = "https://api.weixin.qq.com/cgi-bin/component/api_set_authorizer_option";
2527

26-
2728
String COMPONENT_LOGIN_PAGE_URL = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s";
2829
String CONNECT_OAUTH2_AUTHORIZE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s&component_appid=%s#wechat_redirect";
2930

@@ -87,4 +88,48 @@ public interface WxOpenComponentService {
8788

8889
WxMaJscode2SessionResult miniappJscode2Session(String appId, String jsCode) throws WxErrorException;
8990

91+
/**
92+
* 代小程序实现业务
93+
* <p>
94+
* 小程序代码模版库管理:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1506504150_nMMh6&token=&lang=zh_CN
95+
* access_token 为 component_access_token
96+
*/
97+
String GET_TEMPLATE_DRAFT_LIST_URL = "https://api.weixin.qq.com/wxa/gettemplatedraftlist";
98+
String GET_TEMPLATE_LIST_URL = "https://api.weixin.qq.com/wxa/gettemplatelist";
99+
String ADD_TO_TEMPLATE_URL = "https://api.weixin.qq.com/wxa/addtotemplate";
100+
String DELETE_TEMPLATE_URL = "https://api.weixin.qq.com/wxa/deletetemplate";
101+
102+
/**
103+
* 获取草稿箱内的所有临时代码草稿
104+
*
105+
* @return 草稿箱代码模板列表(draftId)
106+
* @throws WxErrorException 获取失败时返回,具体错误码请看此接口的注释文档
107+
*/
108+
List<WxOpenMaCodeTemplate> getTemplateDraftList() throws WxErrorException;
109+
110+
/**
111+
* 获取代码模版库中的所有小程序代码模版
112+
*
113+
* @return 小程序代码模版列表(templateId)
114+
* @throws WxErrorException 获取失败时返回,具体错误码请看此接口的注释文档
115+
*/
116+
List<WxOpenMaCodeTemplate> getTemplateList() throws WxErrorException;
117+
118+
/**
119+
* 将草稿箱的草稿选为小程序代码模版
120+
*
121+
* @param draftId 草稿ID,本字段可通过“获取草稿箱内的所有临时代码草稿”接口获得
122+
* @throws WxErrorException 操作失败时抛出,具体错误码请看此接口的注释文档
123+
* @see #getTemplateDraftList
124+
*/
125+
void addToTemplate(long draftId) throws WxErrorException;
126+
127+
/**
128+
* 删除指定小程序代码模版
129+
*
130+
* @param templateId 要删除的模版ID
131+
* @throws WxErrorException 操作失败时抛出,具体错误码请看此接口的注释文档
132+
* @see #getTemplateList
133+
*/
134+
void deleteTemplate(long templateId) throws WxErrorException;
90135
}

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenComponentServiceImpl.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import cn.binarywang.wx.miniapp.api.WxMaService;
44
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
55
import com.google.gson.JsonObject;
6+
import com.google.gson.JsonParser;
7+
import com.google.gson.reflect.TypeToken;
68
import me.chanjar.weixin.common.bean.result.WxError;
79
import me.chanjar.weixin.common.exception.WxErrorException;
810
import me.chanjar.weixin.common.util.crypto.SHA1;
@@ -15,6 +17,7 @@
1517
import me.chanjar.weixin.open.api.WxOpenService;
1618
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
1719
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
20+
import me.chanjar.weixin.open.bean.WxOpenMaCodeTemplate;
1821
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizationInfo;
1922
import me.chanjar.weixin.open.bean.message.WxOpenXmlMessage;
2023
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
@@ -26,13 +29,14 @@
2629
import org.slf4j.LoggerFactory;
2730

2831
import java.util.Hashtable;
32+
import java.util.List;
2933
import java.util.Map;
3034

3135
/**
3236
* @author <a href="https://github.com/007gzs">007</a>
3337
*/
3438
public class WxOpenComponentServiceImpl implements WxOpenComponentService {
35-
39+
private static final JsonParser JSON_PARSER = new JsonParser();
3640
private static final Map<String, WxMaService> WX_OPEN_MA_SERVICE_MAP = new Hashtable<>();
3741
private static final Map<String, WxMpService> WX_OPEN_MP_SERVICE_MAP = new Hashtable<>();
3842

@@ -288,4 +292,45 @@ public WxMaJscode2SessionResult miniappJscode2Session(String appId, String jsCod
288292
return WxMaJscode2SessionResult.fromJson(responseContent);
289293
}
290294

295+
@Override
296+
public List<WxOpenMaCodeTemplate> getTemplateDraftList() throws WxErrorException {
297+
String responseContent = get(GET_TEMPLATE_DRAFT_LIST_URL);
298+
JsonObject response = JSON_PARSER.parse(StringUtils.defaultString(responseContent, "{}")).getAsJsonObject();
299+
boolean hasDraftList = response.has("draft_list");
300+
if (hasDraftList) {
301+
return WxOpenGsonBuilder.create().fromJson(response.getAsJsonArray("draft_list"),
302+
new TypeToken<List<WxOpenMaCodeTemplate>>() {
303+
}.getType());
304+
} else {
305+
return null;
306+
}
307+
}
308+
309+
@Override
310+
public List<WxOpenMaCodeTemplate> getTemplateList() throws WxErrorException {
311+
String responseContent = get(GET_TEMPLATE_LIST_URL);
312+
JsonObject response = JSON_PARSER.parse(StringUtils.defaultString(responseContent, "{}")).getAsJsonObject();
313+
boolean hasDraftList = response.has("template_list");
314+
if (hasDraftList) {
315+
return WxOpenGsonBuilder.create().fromJson(response.getAsJsonArray("template_list"),
316+
new TypeToken<List<WxOpenMaCodeTemplate>>() {
317+
}.getType());
318+
} else {
319+
return null;
320+
}
321+
}
322+
323+
@Override
324+
public void addToTemplate(long draftId) throws WxErrorException {
325+
JsonObject param = new JsonObject();
326+
param.addProperty("draft_id", draftId);
327+
post(ADD_TO_TEMPLATE_URL, param.toString());
328+
}
329+
330+
@Override
331+
public void deleteTemplate(long templateId) throws WxErrorException {
332+
JsonObject param = new JsonObject();
333+
param.addProperty("template_id", templateId);
334+
post(DELETE_TEMPLATE_URL, param.toString());
335+
}
291336
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package me.chanjar.weixin.open.bean;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
6+
import java.io.Serializable;
7+
8+
/**
9+
* @author <a href="https://github.com/charmingoh">Charming</a>
10+
* @since 2018-04-26 17:10
11+
*/
12+
@Data
13+
public class WxOpenMaCodeTemplate implements Serializable {
14+
private static final long serialVersionUID = -3278116984473619010L;
15+
/**
16+
* 草稿id
17+
*/
18+
@SerializedName(value = "draftId", alternate = "draft_id")
19+
private Long draftId;
20+
/**
21+
* 模版id
22+
*/
23+
@SerializedName(value = "templateId", alternate = "template_id")
24+
private Long templateId;
25+
/**
26+
* 模版版本号,开发者自定义字段
27+
*/
28+
@SerializedName(value = "userVersion", alternate = "user_version")
29+
private String userVersion;
30+
/**
31+
* 模版描述 开发者自定义字段
32+
*/
33+
@SerializedName(value = "userDesc", alternate = "user_desc")
34+
private String userDesc;
35+
/**
36+
* 开发者上传草稿时间 / 被添加为模版的时间
37+
*/
38+
@SerializedName(value = "createTime", alternate = "create_time")
39+
private Long createTime;
40+
}

0 commit comments

Comments
 (0)