Browse Source

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

master
Binary Wang 8 years ago
parent
commit
d7298ab790
3 changed files with 41 additions and 6 deletions
  1. +11
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpTemplateMsgService.java
  2. +15
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImpl.java
  3. +15
    -6
      weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImplTest.java

+ 11
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpTemplateMsgService.java View File

@@ -46,4 +46,15 @@ public interface WxMpTemplateMsgService {
*/ */
String sendTemplateMsg(WxMpTemplateMessage templateMessage) throws WxErrorException; 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;
} }

+ 15
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImpl.java View File

@@ -2,6 +2,7 @@ package me.chanjar.weixin.mp.api.impl;


import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;

import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
@@ -55,4 +56,18 @@ public class WxMpTemplateMsgServiceImpl implements WxMpTemplateMsgService {
return WxMpIndustry.fromJson(responseContent); 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));
}

} }

+ 15
- 6
weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImplTest.java View File

@@ -1,18 +1,20 @@
package me.chanjar.weixin.mp.api.impl; 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 com.google.inject.Inject;

import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.ApiTestModule; import me.chanjar.weixin.mp.api.ApiTestModule;
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage; import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.bean.WxMpIndustry; import me.chanjar.weixin.mp.bean.WxMpIndustry;
import me.chanjar.weixin.mp.bean.WxMpTemplateData; import me.chanjar.weixin.mp.bean.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage; 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> * <pre>
@@ -56,4 +58,11 @@ public class WxMpTemplateMsgServiceImplTest {
Assert.assertTrue(result); Assert.assertTrue(result);
} }


@Test
public void testAddTemplate() throws Exception {
String result = this.wxService.getTemplateMsgService().addTemplate("TM00015");
Assert.assertNotNull(result);
System.err.println(result);
}

} }

Loading…
Cancel
Save