Browse Source

:art: 修复错误代码

allovine 1 year ago
parent
commit
f468653ac4

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

@@ -1,6 +1,7 @@
 package me.chanjar.weixin.mp.api.impl;
 
 import com.google.gson.Gson;
+import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
 import lombok.RequiredArgsConstructor;
 import me.chanjar.weixin.common.api.WxConsts;
@@ -78,6 +79,24 @@ public class WxMpTemplateMsgServiceImpl implements WxMpTemplateMsgService {
   }
 
   @Override
+  public String addTemplate(String shortTemplateId, List<String> keywordNameList) throws WxErrorException {
+    JsonObject jsonObject = new JsonObject();
+    JsonArray jsonArray = new JsonArray();
+    for(String val: keywordNameList) {
+      jsonArray.add(val);
+    }
+    jsonObject.addProperty("template_id_short", shortTemplateId);
+    jsonObject.add("keyword_name_list",jsonArray);
+    String responseContent = this.wxMpService.post(TEMPLATE_API_ADD_TEMPLATE, jsonObject.toString());
+    final JsonObject result = GsonParser.parse(responseContent);
+    if (result.get(WxConsts.ERR_CODE).getAsInt() == 0) {
+      return result.get("template_id").getAsString();
+    }
+
+    throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
+  }
+
+  @Override
   public List<WxMpTemplate> getAllPrivateTemplate() throws WxErrorException {
     return WxMpTemplate.fromJson(this.wxMpService.get(TEMPLATE_GET_ALL_PRIVATE_TEMPLATE, null));
   }