添加获得模板ID的接口方法实现 for issue #63

This commit is contained in:
Binary Wang 2016-10-17 23:43:31 +08:00
parent 77db3c2dae
commit d7298ab790
3 changed files with 41 additions and 6 deletions

View File

@ -46,4 +46,15 @@ public interface WxMpTemplateMsgService {
*/
String sendTemplateMsg(WxMpTemplateMessage templateMessage) throws WxErrorException;
/**
* <pre>
* 获得模板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
* </pre>
*@param shortTemplateId 模板库中模板的编号TM**OPENTMTM**等形式
* @return templateId 模板Id
*/
String addTemplate(String shortTemplateId) throws WxErrorException;
}

View File

@ -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));
}
}

View File

@ -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;
/**
* <pre>
@ -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);
}
}