Skip to content

Commit 77db3c2

Browse files
committed
将模板消息相关接口代码移到单独的service,并作相应重构调整,增加单元测试 for issue #63
1 parent 6682f5f commit 77db3c2

File tree

9 files changed

+273
-164
lines changed

9 files changed

+273
-164
lines changed

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

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,8 @@
33
import me.chanjar.weixin.common.bean.WxJsapiSignature;
44
import me.chanjar.weixin.common.exception.WxErrorException;
55
import me.chanjar.weixin.common.util.http.RequestExecutor;
6-
import me.chanjar.weixin.mp.bean.WxMpIndustry;
7-
import me.chanjar.weixin.mp.bean.WxMpMassTagMessage;
8-
import me.chanjar.weixin.mp.bean.WxMpMassNews;
9-
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
10-
import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage;
11-
import me.chanjar.weixin.mp.bean.WxMpMassVideo;
12-
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
13-
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
14-
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
15-
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
16-
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
17-
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
18-
import me.chanjar.weixin.mp.bean.result.WxMpUser;
6+
import me.chanjar.weixin.mp.bean.*;
7+
import me.chanjar.weixin.mp.bean.result.*;
198

209
/**
2110
* 微信API的Service
@@ -133,16 +122,6 @@ public interface WxMpService {
133122
*/
134123
String shortUrl(String long_url) throws WxErrorException;
135124

136-
/**
137-
* <pre>
138-
* 发送模板消息
139-
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=模板消息接口
140-
* </pre>
141-
*
142-
* @return msgid
143-
*/
144-
String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException;
145-
146125
/**
147126
* <pre>
148127
* 语义查询接口
@@ -266,27 +245,6 @@ public interface WxMpService {
266245
*/
267246
WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception;
268247

269-
/**
270-
* <pre>
271-
* 设置所属行业
272-
* 官方文档中暂未告知响应内容
273-
* 详情请见:http://mp.weixin.qq.com/wiki/5/6dde9eaa909f83354e0094dc3ad99e05.html#.E8.AE.BE.E7.BD.AE.E6.89.80.E5.B1.9E.E8.A1.8C.E4.B8.9A
274-
* </pre>
275-
*
276-
* @return JsonObject
277-
*/
278-
String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException;
279-
280-
/***
281-
* <pre>
282-
* 获取设置的行业信息
283-
* 详情请见:http://mp.weixin.qq.com/wiki/5/6dde9eaa909f83354e0094dc3ad99e05.html#.E8.AE.BE.E7.BD.AE.E6.89.80.E5.B1.9E.E8.A1.8C.E4.B8.9A
284-
* </pre>
285-
*
286-
* @return wxMpIndustry
287-
*/
288-
WxMpIndustry getIndustry() throws WxErrorException;
289-
290248
/**
291249
* 获取WxMpConfigStorage 对象
292250
*
@@ -370,4 +328,11 @@ public interface WxMpService {
370328
* @return WxMpStoreService
371329
*/
372330
WxMpStoreService getStoreService();
331+
332+
/**
333+
* 返回模板消息相关接口方法的实现类对象,以方便调用其各种接口
334+
*
335+
* @return WxMpTemplateMsgService
336+
*/
337+
WxMpTemplateMsgService getTemplateMsgService();
373338
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package me.chanjar.weixin.mp.api;
2+
3+
import me.chanjar.weixin.common.exception.WxErrorException;
4+
import me.chanjar.weixin.mp.bean.WxMpIndustry;
5+
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
6+
7+
/**
8+
* <pre>
9+
* 模板消息接口
10+
* http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
11+
* Created by Binary Wang on 2016-10-14.
12+
* @author miller.lin
13+
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
14+
* </pre>
15+
*/
16+
public interface WxMpTemplateMsgService {
17+
18+
/**
19+
* <pre>
20+
* 设置所属行业
21+
* 官方文档中暂未告知响应内容
22+
* 详情请见:http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
23+
* </pre>
24+
*
25+
* @return 是否成功
26+
*/
27+
boolean setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException;
28+
29+
/***
30+
* <pre>
31+
* 获取设置的行业信息
32+
* 详情请见:http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
33+
* </pre>
34+
*
35+
* @return wxMpIndustry
36+
*/
37+
WxMpIndustry getIndustry() throws WxErrorException;
38+
39+
/**
40+
* <pre>
41+
* 发送模板消息
42+
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
43+
* </pre>
44+
*
45+
* @return 消息Id
46+
*/
47+
String sendTemplateMsg(WxMpTemplateMessage templateMessage) throws WxErrorException;
48+
49+
}

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

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public class WxMpServiceImpl implements WxMpService {
6969

7070
private WxMpUserBlacklistService blackListService = new WxMpUserBlacklistServiceImpl(this);
7171

72+
private WxMpTemplateMsgService templateMsgService = new WxMpTemplateMsgServiceImpl(this);
73+
7274
private CloseableHttpClient httpClient;
7375

7476
private HttpHost httpProxy;
@@ -221,35 +223,6 @@ public String shortUrl(String long_url) throws WxErrorException {
221223
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
222224
}
223225

224-
@Override
225-
public String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException {
226-
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send";
227-
String responseContent = this.post(url, templateMessage.toJson());
228-
final JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
229-
if (jsonObject.get("errcode").getAsInt() == 0){
230-
return jsonObject.get("msgid").getAsString();
231-
}
232-
233-
throw new WxErrorException(WxError.fromJson(responseContent));
234-
}
235-
236-
@Override
237-
public String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException {
238-
if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId()
239-
|| null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) {
240-
throw new IllegalArgumentException("industry id is empty");
241-
}
242-
String url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry";
243-
return this.post(url, wxMpIndustry.toJson());
244-
}
245-
246-
@Override
247-
public WxMpIndustry getIndustry() throws WxErrorException {
248-
String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry";
249-
String responseContent = this.get(url, null);
250-
return WxMpIndustry.fromJson(responseContent);
251-
}
252-
253226
@Override
254227
public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException {
255228
String url = "https://api.weixin.qq.com/semantic/semproxy/search";
@@ -562,4 +535,9 @@ public WxMpStoreService getStoreService() {
562535
return this.storeService;
563536
}
564537

538+
@Override
539+
public WxMpTemplateMsgService getTemplateMsgService() {
540+
return this.templateMsgService;
541+
}
542+
565543
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package me.chanjar.weixin.mp.api.impl;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
5+
import me.chanjar.weixin.common.bean.result.WxError;
6+
import me.chanjar.weixin.common.exception.WxErrorException;
7+
import me.chanjar.weixin.mp.api.WxMpService;
8+
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
9+
import me.chanjar.weixin.mp.bean.WxMpIndustry;
10+
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
11+
12+
/**
13+
* <pre>
14+
* Created by Binary Wang on 2016-10-14.
15+
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
16+
* </pre>
17+
*/
18+
public class WxMpTemplateMsgServiceImpl implements WxMpTemplateMsgService {
19+
public static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/template";
20+
private static final JsonParser JSON_PARSER = new JsonParser();
21+
22+
private WxMpService wxMpService;
23+
24+
public WxMpTemplateMsgServiceImpl(WxMpService wxMpService) {
25+
this.wxMpService = wxMpService;
26+
}
27+
28+
@Override
29+
public String sendTemplateMsg(WxMpTemplateMessage templateMessage) throws WxErrorException {
30+
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send";
31+
String responseContent = this.wxMpService.post(url, templateMessage.toJson());
32+
final JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
33+
if (jsonObject.get("errcode").getAsInt() == 0) {
34+
return jsonObject.get("msgid").getAsString();
35+
}
36+
throw new WxErrorException(WxError.fromJson(responseContent));
37+
}
38+
39+
@Override
40+
public boolean setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException {
41+
if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId()
42+
|| null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) {
43+
throw new IllegalArgumentException("行业Id不能为空,请核实");
44+
}
45+
46+
String url = API_URL_PREFIX + "/api_set_industry";
47+
this.wxMpService.post(url, wxMpIndustry.toJson());
48+
return true;
49+
}
50+
51+
@Override
52+
public WxMpIndustry getIndustry() throws WxErrorException {
53+
String url = API_URL_PREFIX + "/get_industry";
54+
String responseContent = this.wxMpService.get(url, null);
55+
return WxMpIndustry.fromJson(responseContent);
56+
}
57+
58+
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/Industry.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)