From d7298ab790a3b36d4b8f39e93680a4cdd521a8a3 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Mon, 17 Oct 2016 23:43:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=BE=97=E6=A8=A1?= =?UTF-8?q?=E6=9D=BFID=E7=9A=84=E6=8E=A5=E5=8F=A3=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20for=20issue=20#63?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/mp/api/WxMpTemplateMsgService.java | 11 ++++++++++ .../api/impl/WxMpTemplateMsgServiceImpl.java | 15 +++++++++++++ .../impl/WxMpTemplateMsgServiceImplTest.java | 21 +++++++++++++------ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpTemplateMsgService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpTemplateMsgService.java index d468a89e..9f696597 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpTemplateMsgService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpTemplateMsgService.java @@ -46,4 +46,15 @@ public interface WxMpTemplateMsgService { */ String sendTemplateMsg(WxMpTemplateMessage templateMessage) throws WxErrorException; + /** + *
+   * 获得模板ID
+   * 从行业模板库选择模板到帐号后台,获得模板ID的过程可在MP中完成
+   * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
+   * 接口地址格式:https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN
+   * 
+ *@param shortTemplateId 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式 + * @return templateId 模板Id + */ + String addTemplate(String shortTemplateId) throws WxErrorException; } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImpl.java index d4f03ec9..a404cb02 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImpl.java @@ -2,6 +2,7 @@ package me.chanjar.weixin.mp.api.impl; import com.google.gson.JsonObject; import com.google.gson.JsonParser; + import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.mp.api.WxMpService; @@ -55,4 +56,18 @@ public class WxMpTemplateMsgServiceImpl implements WxMpTemplateMsgService { return WxMpIndustry.fromJson(responseContent); } + @Override + public String addTemplate(String shortTemplateId) throws WxErrorException { + String url = API_URL_PREFIX + "/api_add_template"; + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("template_id_short", shortTemplateId); + String responseContent = this.wxMpService.post(url, jsonObject.toString()); + final JsonObject result = JSON_PARSER.parse(responseContent).getAsJsonObject(); + if (result.get("errcode").getAsInt() == 0) { + return result.get("template_id").getAsString(); + } + + throw new WxErrorException(WxError.fromJson(responseContent)); + } + } diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImplTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImplTest.java index 5fb8061e..b967719f 100644 --- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImplTest.java +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImplTest.java @@ -1,18 +1,20 @@ package me.chanjar.weixin.mp.api.impl; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.testng.Assert; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + import com.google.inject.Inject; + import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.mp.api.ApiTestModule; import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage; import me.chanjar.weixin.mp.bean.WxMpIndustry; import me.chanjar.weixin.mp.bean.WxMpTemplateData; import me.chanjar.weixin.mp.bean.WxMpTemplateMessage; -import org.testng.Assert; -import org.testng.annotations.Guice; -import org.testng.annotations.Test; - -import java.text.SimpleDateFormat; -import java.util.Date; /** *
@@ -56,4 +58,11 @@ public class WxMpTemplateMsgServiceImplTest {
     Assert.assertTrue(result);
   }
 
+  @Test
+  public void testAddTemplate() throws Exception {
+    String result = this.wxService.getTemplateMsgService().addTemplate("TM00015");
+    Assert.assertNotNull(result);
+    System.err.println(result);
+  }
+
 }