Просмотр исходного кода

:art: 优化重构微信小程序订阅消息模板设置相关方法

Binary Wang 5 лет назад
Родитель
Сommit
9c58930719

+ 1 - 0
weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheSimplePostRequestExecutor.java

@@ -36,6 +36,7 @@ public class ApacheSimplePostRequestExecutor extends SimplePostRequestExecutor<C
 
     if (postEntity != null) {
       StringEntity entity = new StringEntity(postEntity, Consts.UTF_8);
+      entity.setContentType("application/json; charset=utf-8");
       httpPost.setEntity(entity);
     }
 

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

@@ -87,6 +87,11 @@ public interface WxMaService {
   String post(String url, String postData) throws WxErrorException;
 
   /**
+   * 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求.
+   */
+  String post(String url, Object obj) throws WxErrorException;
+
+  /**
    * <pre>
    * Service没有实现某个API的时候,可以用这个,
    * 比{@link #get}和{@link #post}方法更灵活,可以自己构造RequestExecutor用来处理不同的参数和不同的返回类型。

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

@@ -16,6 +16,7 @@ import me.chanjar.weixin.common.util.crypto.SHA1;
 import me.chanjar.weixin.common.util.http.*;
 import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
 import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
+import me.chanjar.weixin.common.util.json.WxGsonBuilder;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.http.HttpHost;
 import org.apache.http.client.config.RequestConfig;
@@ -206,6 +207,11 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
     return execute(SimplePostRequestExecutor.create(this), url, postData);
   }
 
+  @Override
+  public String post(String url, Object obj) throws WxErrorException {
+    return this.execute(SimplePostRequestExecutor.create(this), url, WxGsonBuilder.create().toJson(obj));
+  }
+
   /**
    * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
    */

+ 7 - 13
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaSubscribeServiceImpl.java

@@ -9,12 +9,9 @@ import com.google.gson.JsonParser;
 import com.google.gson.reflect.TypeToken;
 import lombok.AllArgsConstructor;
 import me.chanjar.weixin.common.error.WxErrorException;
-import me.chanjar.weixin.common.util.json.WxGsonBuilder;
 import org.apache.commons.lang3.StringUtils;
 
-import java.io.Serializable;
 import java.util.List;
-import java.util.Map;
 
 /**
  * @author <a href="https://github.com/binarywang">Binary Wang</a>
@@ -26,16 +23,14 @@ public class WxMaSubscribeServiceImpl implements WxMaSubscribeService {
 
   @Override
   public WxMaTemplateLibraryListResult getPubTemplateTitleList(Integer[] ids, int start, int limit) throws WxErrorException {
-    ImmutableMap<String, ? extends Serializable> params = ImmutableMap.of("ids", StringUtils.join(ids, ","),
-      "start", start, "limit", limit);
-    String responseText = this.wxMaService.post(GET_PUB_TEMPLATE_TITLE_LIST_URL, WxGsonBuilder.create().toJson(params));
-    return WxMaTemplateLibraryListResult.fromJson(responseText);
+    return WxMaTemplateLibraryListResult.fromJson(this.wxMaService.post(GET_PUB_TEMPLATE_TITLE_LIST_URL,
+      ImmutableMap.of("ids", StringUtils.join(ids, ","),
+        "start", start, "limit", limit)));
   }
 
   @Override
   public List<PubTemplateKeyword> getPubTemplateKeyWordsById(String id) throws WxErrorException {
-    String responseText = this.wxMaService.post(GET_PUB_TEMPLATE_KEY_WORDS_BY_ID_URL,
-      WxGsonBuilder.create().toJson(ImmutableMap.of("tid", id)));
+    String responseText = this.wxMaService.post(GET_PUB_TEMPLATE_KEY_WORDS_BY_ID_URL, ImmutableMap.of("tid", id));
     return WxMaGsonBuilder.create().fromJson(new JsonParser().parse(responseText).getAsJsonObject()
       .getAsJsonArray("data"), new TypeToken<List<PubTemplateKeyword>>() {
     }.getType());
@@ -43,9 +38,9 @@ public class WxMaSubscribeServiceImpl implements WxMaSubscribeService {
 
   @Override
   public String addTemplate(String id, List<Integer> keywordIdList, String sceneDesc) throws WxErrorException {
-    String responseText = this.wxMaService.post(TEMPLATE_ADD_URL, WxGsonBuilder.create().toJson(ImmutableMap.of("tid", id,
+    String responseText = this.wxMaService.post(TEMPLATE_ADD_URL, ImmutableMap.of("tid", id,
       "kidList", keywordIdList.toArray(),
-      "sceneDesc", sceneDesc)));
+      "sceneDesc", sceneDesc));
     return new JsonParser().parse(responseText).getAsJsonObject().get("priTmplId").getAsString();
   }
 
@@ -59,8 +54,7 @@ public class WxMaSubscribeServiceImpl implements WxMaSubscribeService {
 
   @Override
   public boolean delTemplate(String templateId) throws WxErrorException {
-    Map<String, String> params = ImmutableMap.of("priTmplId", templateId);
-    this.wxMaService.post(TEMPLATE_DEL_URL, WxGsonBuilder.create().toJson(params));
+    this.wxMaService.post(TEMPLATE_DEL_URL, ImmutableMap.of("priTmplId", templateId));
     return true;
   }
 

+ 1 - 1
weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaSubscribeServiceImplTest.java

@@ -38,7 +38,7 @@ public class WxMaSubscribeServiceImplTest {
 
   @Test
   public void testAddTemplate() throws WxErrorException {
-    final String templateId = this.wxService.getSubscribeService().addTemplate("1", Lists.newArrayList(1), "");
+    final String templateId = this.wxService.getSubscribeService().addTemplate("1", Lists.newArrayList(1), "hhaa");
     System.out.println(templateId);
   }