From 75c038d464b142c6eadc68d5c72803bf24299d54 Mon Sep 17 00:00:00 2001
From: Charming <charmingoh@users.noreply.github.com>
Date: Thu, 26 Apr 2018 18:07:31 +0800
Subject: [PATCH] =?UTF-8?q?#560=20=E5=BE=AE=E4=BF=A1=E5=BC=80=E6=94=BE?=
 =?UTF-8?q?=E5=B9=B3=E5=8F=B0=EF=BC=9A=E5=A2=9E=E5=8A=A0=E5=B0=8F=E7=A8=8B?=
 =?UTF-8?q?=E5=BA=8F=E4=BB=A3=E7=A0=81=E6=A8=A1=E6=9D=BF=E5=BA=93=E7=AE=A1?=
 =?UTF-8?q?=E7=90=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* 微信开放平台:1. WxOpenInRedisConfigStorage 支持 JedisPool/JedisSentinelPool 等 Pool<Jedis> 的子类;2. WxOpenInRedisConfigStorage 增加 keyPrefix 以支持可配置的前缀;

* 微信开放平台:增加小程序代码模板库管理
---
 .../open/api/WxOpenComponentService.java      | 49 ++++++++++++++++++-
 .../api/impl/WxOpenComponentServiceImpl.java  | 47 +++++++++++++++++-
 .../open/bean/WxOpenMaCodeTemplate.java       | 40 +++++++++++++++
 3 files changed, 133 insertions(+), 3 deletions(-)
 create mode 100644 weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/WxOpenMaCodeTemplate.java

diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenComponentService.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenComponentService.java
index 9b0775905..35a438c0a 100644
--- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenComponentService.java
+++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenComponentService.java
@@ -5,16 +5,18 @@ import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
 import me.chanjar.weixin.common.exception.WxErrorException;
 import me.chanjar.weixin.mp.api.WxMpService;
 import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
+import me.chanjar.weixin.open.bean.WxOpenMaCodeTemplate;
 import me.chanjar.weixin.open.bean.message.WxOpenXmlMessage;
 import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
 import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerOptionResult;
 import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult;
 
+import java.util.List;
+
 /**
  * @author <a href="https://github.com/007gzs">007</a>
  */
 public interface WxOpenComponentService {
-
   String API_COMPONENT_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
   String API_CREATE_PREAUTHCODE_URL = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode";
   String API_QUERY_AUTH_URL = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth";
@@ -23,7 +25,6 @@ public interface WxOpenComponentService {
   String API_GET_AUTHORIZER_OPTION_URL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_option";
   String API_SET_AUTHORIZER_OPTION_URL = "https://api.weixin.qq.com/cgi-bin/component/api_set_authorizer_option";
 
-
   String COMPONENT_LOGIN_PAGE_URL = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s";
   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";
 
@@ -87,4 +88,48 @@ public interface WxOpenComponentService {
 
   WxMaJscode2SessionResult miniappJscode2Session(String appId, String jsCode) throws WxErrorException;
 
+  /**
+   * 代小程序实现业务
+   * <p>
+   * 小程序代码模版库管理:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1506504150_nMMh6&token=&lang=zh_CN
+   * access_token 为 component_access_token
+   */
+  String GET_TEMPLATE_DRAFT_LIST_URL = "https://api.weixin.qq.com/wxa/gettemplatedraftlist";
+  String GET_TEMPLATE_LIST_URL = "https://api.weixin.qq.com/wxa/gettemplatelist";
+  String ADD_TO_TEMPLATE_URL = "https://api.weixin.qq.com/wxa/addtotemplate";
+  String DELETE_TEMPLATE_URL = "https://api.weixin.qq.com/wxa/deletetemplate";
+
+  /**
+   * 获取草稿箱内的所有临时代码草稿
+   *
+   * @return 草稿箱代码模板列表(draftId)
+   * @throws WxErrorException 获取失败时返回,具体错误码请看此接口的注释文档
+   */
+  List<WxOpenMaCodeTemplate> getTemplateDraftList() throws WxErrorException;
+
+  /**
+   * 获取代码模版库中的所有小程序代码模版
+   *
+   * @return 小程序代码模版列表(templateId)
+   * @throws WxErrorException 获取失败时返回,具体错误码请看此接口的注释文档
+   */
+  List<WxOpenMaCodeTemplate> getTemplateList() throws WxErrorException;
+
+  /**
+   * 将草稿箱的草稿选为小程序代码模版
+   *
+   * @param draftId 草稿ID,本字段可通过“获取草稿箱内的所有临时代码草稿”接口获得
+   * @throws WxErrorException 操作失败时抛出,具体错误码请看此接口的注释文档
+   * @see #getTemplateDraftList
+   */
+  void addToTemplate(long draftId) throws WxErrorException;
+
+  /**
+   * 删除指定小程序代码模版
+   *
+   * @param templateId 要删除的模版ID
+   * @throws WxErrorException 操作失败时抛出,具体错误码请看此接口的注释文档
+   * @see #getTemplateList
+   */
+  void deleteTemplate(long templateId) throws WxErrorException;
 }
diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenComponentServiceImpl.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenComponentServiceImpl.java
index 36dd28650..cb9062c7e 100644
--- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenComponentServiceImpl.java
+++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenComponentServiceImpl.java
@@ -3,6 +3,8 @@ package me.chanjar.weixin.open.api.impl;
 import cn.binarywang.wx.miniapp.api.WxMaService;
 import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
 import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.reflect.TypeToken;
 import me.chanjar.weixin.common.bean.result.WxError;
 import me.chanjar.weixin.common.exception.WxErrorException;
 import me.chanjar.weixin.common.util.crypto.SHA1;
@@ -15,6 +17,7 @@ import me.chanjar.weixin.open.api.WxOpenConfigStorage;
 import me.chanjar.weixin.open.api.WxOpenService;
 import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
 import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
+import me.chanjar.weixin.open.bean.WxOpenMaCodeTemplate;
 import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizationInfo;
 import me.chanjar.weixin.open.bean.message.WxOpenXmlMessage;
 import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
@@ -26,13 +29,14 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.Hashtable;
+import java.util.List;
 import java.util.Map;
 
 /**
  * @author <a href="https://github.com/007gzs">007</a>
  */
 public class WxOpenComponentServiceImpl implements WxOpenComponentService {
-
+  private static final JsonParser JSON_PARSER = new JsonParser();
   private static final Map<String, WxMaService> WX_OPEN_MA_SERVICE_MAP = new Hashtable<>();
   private static final Map<String, WxMpService> WX_OPEN_MP_SERVICE_MAP = new Hashtable<>();
 
@@ -288,4 +292,45 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService {
     return WxMaJscode2SessionResult.fromJson(responseContent);
   }
 
+  @Override
+  public List<WxOpenMaCodeTemplate> getTemplateDraftList() throws WxErrorException {
+    String responseContent = get(GET_TEMPLATE_DRAFT_LIST_URL);
+    JsonObject response = JSON_PARSER.parse(StringUtils.defaultString(responseContent, "{}")).getAsJsonObject();
+    boolean hasDraftList = response.has("draft_list");
+    if (hasDraftList) {
+      return WxOpenGsonBuilder.create().fromJson(response.getAsJsonArray("draft_list"),
+        new TypeToken<List<WxOpenMaCodeTemplate>>() {
+        }.getType());
+    } else {
+      return null;
+    }
+  }
+
+  @Override
+  public List<WxOpenMaCodeTemplate> getTemplateList() throws WxErrorException {
+    String responseContent = get(GET_TEMPLATE_LIST_URL);
+    JsonObject response = JSON_PARSER.parse(StringUtils.defaultString(responseContent, "{}")).getAsJsonObject();
+    boolean hasDraftList = response.has("template_list");
+    if (hasDraftList) {
+      return WxOpenGsonBuilder.create().fromJson(response.getAsJsonArray("template_list"),
+        new TypeToken<List<WxOpenMaCodeTemplate>>() {
+        }.getType());
+    } else {
+      return null;
+    }
+  }
+
+  @Override
+  public void addToTemplate(long draftId) throws WxErrorException {
+    JsonObject param = new JsonObject();
+    param.addProperty("draft_id", draftId);
+    post(ADD_TO_TEMPLATE_URL, param.toString());
+  }
+
+  @Override
+  public void deleteTemplate(long templateId) throws WxErrorException {
+    JsonObject param = new JsonObject();
+    param.addProperty("template_id", templateId);
+    post(DELETE_TEMPLATE_URL, param.toString());
+  }
 }
diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/WxOpenMaCodeTemplate.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/WxOpenMaCodeTemplate.java
new file mode 100644
index 000000000..be1bb9b13
--- /dev/null
+++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/WxOpenMaCodeTemplate.java
@@ -0,0 +1,40 @@
+package me.chanjar.weixin.open.bean;
+
+import com.google.gson.annotations.SerializedName;
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * @author <a href="https://github.com/charmingoh">Charming</a>
+ * @since 2018-04-26 17:10
+ */
+@Data
+public class WxOpenMaCodeTemplate implements Serializable {
+  private static final long serialVersionUID = -3278116984473619010L;
+  /**
+   * 草稿id
+   */
+  @SerializedName(value = "draftId", alternate = "draft_id")
+  private Long draftId;
+  /**
+   * 模版id
+   */
+  @SerializedName(value = "templateId", alternate = "template_id")
+  private Long templateId;
+  /**
+   * 模版版本号,开发者自定义字段
+   */
+  @SerializedName(value = "userVersion", alternate = "user_version")
+  private String userVersion;
+  /**
+   * 模版描述 开发者自定义字段
+   */
+  @SerializedName(value = "userDesc", alternate = "user_desc")
+  private String userDesc;
+  /**
+   * 开发者上传草稿时间 / 被添加为模版的时间
+   */
+  @SerializedName(value = "createTime", alternate = "create_time")
+  private Long createTime;
+}