File tree Expand file tree Collapse file tree
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -66,6 +66,21 @@ public interface WxMpTemplateMsgService {
6666 */
6767 String addTemplate (String shortTemplateId ) throws WxErrorException ;
6868
69+ /**
70+ * <pre>
71+ * 获得模板ID
72+ * 从类目模板库选择模板到帐号后台,获得模板ID的过程可在MP中完成
73+ * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
74+ * 接口地址格式:https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN
75+ * </pre>
76+ *
77+ * @param shortTemplateId 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式,对于类目模板,为纯数字ID
78+ * @param keywordNameList 选用的类目模板的关键词,按顺序传入,如果为空,或者关键词不在模板库中,会返回40246错误码
79+ * @return templateId 模板Id
80+ * @throws WxErrorException .
81+ */
82+ String addTemplate (String shortTemplateId , List <String > keywordNameList ) throws WxErrorException ;
83+
6984 /**
7085 * <pre>
7186 * 获取模板列表
Original file line number Diff line number Diff line change 11package me .chanjar .weixin .mp .api .impl ;
22
3+ import com .google .gson .Gson ;
34import com .google .gson .JsonObject ;
45import lombok .RequiredArgsConstructor ;
56import me .chanjar .weixin .common .api .WxConsts ;
@@ -68,6 +69,21 @@ public String addTemplate(String shortTemplateId) throws WxErrorException {
6869 throw new WxErrorException (WxError .fromJson (responseContent , WxType .MP ));
6970 }
7071
72+ @ Override
73+ public String addTemplate (String shortTemplateId , List <String > keywordNameList ) throws WxErrorException {
74+ JsonObject jsonObject = new JsonObject ();
75+ Gson gson = new Gson ();
76+ jsonObject .addProperty ("template_id_short" , shortTemplateId );
77+ jsonObject .addProperty ("keyword_name_list" ,gson .toJson (keywordNameList ));
78+ String responseContent = this .wxMpService .post (TEMPLATE_API_ADD_TEMPLATE , jsonObject .toString ());
79+ final JsonObject result = GsonParser .parse (responseContent );
80+ if (result .get (WxConsts .ERR_CODE ).getAsInt () == 0 ) {
81+ return result .get ("template_id" ).getAsString ();
82+ }
83+
84+ throw new WxErrorException (WxError .fromJson (responseContent , WxType .MP ));
85+ }
86+
7187 @ Override
7288 public List <WxMpTemplate > getAllPrivateTemplate () throws WxErrorException {
7389 return WxMpTemplate .fromJson (this .wxMpService .get (TEMPLATE_GET_ALL_PRIVATE_TEMPLATE , null ));
You can’t perform that action at this time.
0 commit comments