mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-23 13:06:54 +08:00
添加获得模板ID的接口方法实现 for issue #63
This commit is contained in:
parent
77db3c2dae
commit
d7298ab790
@ -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;
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user