Skip to content

Commit acc1291

Browse files
committed
添加删除模板的接口方法实现 for issue #63
1 parent 550bbe3 commit acc1291

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpTemplateMsgService.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package me.chanjar.weixin.mp.api;
22

3-
import java.util.List;
4-
53
import me.chanjar.weixin.common.exception.WxErrorException;
64
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
75
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
86
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
97

8+
import java.util.List;
9+
1010
/**
1111
* <pre>
1212
* 模板消息接口
@@ -68,8 +68,20 @@ public interface WxMpTemplateMsgService {
6868
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
6969
* 接口地址格式:https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN
7070
* </pre>
71-
*
71+
*
7272
* @return templateId 模板Id
7373
*/
7474
List<WxMpTemplate> getAllPrivateTemplate() throws WxErrorException;
75+
76+
/**
77+
* <pre>
78+
* 删除模板
79+
* 删除模板可在MP中完成,为方便第三方开发者,提供通过接口调用的方式来删除某帐号下的模板
80+
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
81+
* 接口地址格式:https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=ACCESS_TOKEN
82+
* </pre>
83+
*
84+
* @param templateId 模板Id
85+
*/
86+
boolean delPrivateTemplate(String templateId) throws WxErrorException;
7587
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3-
import java.util.List;
4-
53
import com.google.gson.JsonObject;
64
import com.google.gson.JsonParser;
7-
85
import me.chanjar.weixin.common.bean.result.WxError;
96
import me.chanjar.weixin.common.exception.WxErrorException;
107
import me.chanjar.weixin.mp.api.WxMpService;
@@ -13,6 +10,8 @@
1310
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
1411
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
1512

13+
import java.util.List;
14+
1615
/**
1716
* <pre>
1817
* Created by Binary Wang on 2016-10-14.
@@ -79,4 +78,18 @@ public List<WxMpTemplate> getAllPrivateTemplate() throws WxErrorException {
7978
return WxMpTemplate.fromJson(this.wxMpService.get(url, null));
8079
}
8180

81+
@Override
82+
public boolean delPrivateTemplate(String templateId) throws WxErrorException {
83+
String url = API_URL_PREFIX + "/del_private_template";
84+
JsonObject jsonObject = new JsonObject();
85+
jsonObject.addProperty("template_id", templateId);
86+
String responseContent = this.wxMpService.post(url, jsonObject.toString());
87+
WxError error = WxError.fromJson(responseContent);
88+
if (error.getErrorCode() == 0) {
89+
return true;
90+
}
91+
92+
throw new WxErrorException(error);
93+
}
94+
8295
}

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImplTest.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3-
import java.text.SimpleDateFormat;
4-
import java.util.Date;
5-
import java.util.List;
6-
7-
import org.testng.Assert;
8-
import org.testng.annotations.Guice;
9-
import org.testng.annotations.Test;
10-
113
import com.google.inject.Inject;
12-
134
import me.chanjar.weixin.common.exception.WxErrorException;
145
import me.chanjar.weixin.mp.api.ApiTestModule;
156
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage;
167
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
178
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
189
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
1910
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
11+
import org.testng.Assert;
12+
import org.testng.annotations.Guice;
13+
import org.testng.annotations.Test;
14+
15+
import java.text.SimpleDateFormat;
16+
import java.util.Date;
17+
import java.util.List;
2018

2119
/**
2220
* <pre>
@@ -74,4 +72,11 @@ public void testGetAllPrivateTemplate() throws Exception {
7472
System.err.println(result);
7573
}
7674

75+
@Test
76+
public void testDelPrivateTemplate() throws Exception {
77+
String templateId = "RPcTe7-4BkU5A2J3imC6W0b4JbjEERcJg0whOMKJKIc";
78+
boolean result = this.wxService.getTemplateMsgService().delPrivateTemplate(templateId);
79+
Assert.assertTrue(result);
80+
}
81+
7782
}

0 commit comments

Comments
 (0)