Skip to content

Commit d7298ab

Browse files
committed
添加获得模板ID的接口方法实现 for issue #63
1 parent 77db3c2 commit d7298ab

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpTemplateMsgService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,15 @@ public interface WxMpTemplateMsgService {
4646
*/
4747
String sendTemplateMsg(WxMpTemplateMessage templateMessage) throws WxErrorException;
4848

49+
/**
50+
* <pre>
51+
* 获得模板ID
52+
* 从行业模板库选择模板到帐号后台,获得模板ID的过程可在MP中完成
53+
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
54+
* 接口地址格式:https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN
55+
* </pre>
56+
*@param shortTemplateId 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
57+
* @return templateId 模板Id
58+
*/
59+
String addTemplate(String shortTemplateId) throws WxErrorException;
4960
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.JsonObject;
44
import com.google.gson.JsonParser;
5+
56
import me.chanjar.weixin.common.bean.result.WxError;
67
import me.chanjar.weixin.common.exception.WxErrorException;
78
import me.chanjar.weixin.mp.api.WxMpService;
@@ -55,4 +56,18 @@ public WxMpIndustry getIndustry() throws WxErrorException {
5556
return WxMpIndustry.fromJson(responseContent);
5657
}
5758

59+
@Override
60+
public String addTemplate(String shortTemplateId) throws WxErrorException {
61+
String url = API_URL_PREFIX + "/api_add_template";
62+
JsonObject jsonObject = new JsonObject();
63+
jsonObject.addProperty("template_id_short", shortTemplateId);
64+
String responseContent = this.wxMpService.post(url, jsonObject.toString());
65+
final JsonObject result = JSON_PARSER.parse(responseContent).getAsJsonObject();
66+
if (result.get("errcode").getAsInt() == 0) {
67+
return result.get("template_id").getAsString();
68+
}
69+
70+
throw new WxErrorException(WxError.fromJson(responseContent));
71+
}
72+
5873
}

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImplTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3+
import java.text.SimpleDateFormat;
4+
import java.util.Date;
5+
6+
import org.testng.Assert;
7+
import org.testng.annotations.Guice;
8+
import org.testng.annotations.Test;
9+
310
import com.google.inject.Inject;
11+
412
import me.chanjar.weixin.common.exception.WxErrorException;
513
import me.chanjar.weixin.mp.api.ApiTestModule;
614
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage;
715
import me.chanjar.weixin.mp.bean.WxMpIndustry;
816
import me.chanjar.weixin.mp.bean.WxMpTemplateData;
917
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
10-
import org.testng.Assert;
11-
import org.testng.annotations.Guice;
12-
import org.testng.annotations.Test;
13-
14-
import java.text.SimpleDateFormat;
15-
import java.util.Date;
1618

1719
/**
1820
* <pre>
@@ -56,4 +58,11 @@ public void testSetIndustry() throws Exception {
5658
Assert.assertTrue(result);
5759
}
5860

61+
@Test
62+
public void testAddTemplate() throws Exception {
63+
String result = this.wxService.getTemplateMsgService().addTemplate("TM00015");
64+
Assert.assertNotNull(result);
65+
System.err.println(result);
66+
}
67+
5968
}

0 commit comments

Comments
 (0)