File tree Expand file tree Collapse file tree 3 files changed +41
-6
lines changed
main/java/me/chanjar/weixin/mp/api
test/java/me/chanjar/weixin/mp/api/impl Expand file tree Collapse file tree 3 files changed +41
-6
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 22
33import com .google .gson .JsonObject ;
44import com .google .gson .JsonParser ;
5+
56import me .chanjar .weixin .common .bean .result .WxError ;
67import me .chanjar .weixin .common .exception .WxErrorException ;
78import 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}
Original file line number Diff line number Diff line change 11package 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+
310import com .google .inject .Inject ;
11+
412import me .chanjar .weixin .common .exception .WxErrorException ;
513import me .chanjar .weixin .mp .api .ApiTestModule ;
614import me .chanjar .weixin .mp .api .WxXmlMpInMemoryConfigStorage ;
715import me .chanjar .weixin .mp .bean .WxMpIndustry ;
816import me .chanjar .weixin .mp .bean .WxMpTemplateData ;
917import 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}
You can’t perform that action at this time.
0 commit comments