将模板消息相关接口代码移到单独的service,并作相应重构调整,增加单元测试 for issue #63

This commit is contained in:
Binary Wang
2016-10-14 19:33:34 +08:00
parent 6682f5ff28
commit 77db3c2dae
9 changed files with 274 additions and 165 deletions

View File

@@ -3,19 +3,8 @@ package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.mp.bean.WxMpIndustry;
import me.chanjar.weixin.mp.bean.WxMpMassTagMessage;
import me.chanjar.weixin.mp.bean.WxMpMassNews;
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage;
import me.chanjar.weixin.mp.bean.WxMpMassVideo;
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.*;
/**
* 微信API的Service
@@ -133,16 +122,6 @@ public interface WxMpService {
*/
String shortUrl(String long_url) throws WxErrorException;
/**
* <pre>
* 发送模板消息
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=模板消息接口
* </pre>
*
* @return msgid
*/
String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException;
/**
* <pre>
* 语义查询接口
@@ -266,27 +245,6 @@ public interface WxMpService {
*/
WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception;
/**
* <pre>
* 设置所属行业
* 官方文档中暂未告知响应内容
* 详情请见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
* </pre>
*
* @return JsonObject
*/
String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException;
/***
* <pre>
* 获取设置的行业信息
* 详情请见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
* </pre>
*
* @return wxMpIndustry
*/
WxMpIndustry getIndustry() throws WxErrorException;
/**
* 获取WxMpConfigStorage 对象
*
@@ -370,4 +328,11 @@ public interface WxMpService {
* @return WxMpStoreService
*/
WxMpStoreService getStoreService();
/**
* 返回模板消息相关接口方法的实现类对象,以方便调用其各种接口
*
* @return WxMpTemplateMsgService
*/
WxMpTemplateMsgService getTemplateMsgService();
}

View File

@@ -0,0 +1,49 @@
package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.WxMpIndustry;
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
/**
* <pre>
* 模板消息接口
* http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
* Created by Binary Wang on 2016-10-14.
* @author miller.lin
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
* </pre>
*/
public interface WxMpTemplateMsgService {
/**
* <pre>
* 设置所属行业
* 官方文档中暂未告知响应内容
* 详情请见http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
* </pre>
*
* @return 是否成功
*/
boolean setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException;
/***
* <pre>
* 获取设置的行业信息
* 详情请见http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
* </pre>
*
* @return wxMpIndustry
*/
WxMpIndustry getIndustry() throws WxErrorException;
/**
* <pre>
* 发送模板消息
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
* </pre>
*
* @return 消息Id
*/
String sendTemplateMsg(WxMpTemplateMessage templateMessage) throws WxErrorException;
}

View File

@@ -69,6 +69,8 @@ public class WxMpServiceImpl implements WxMpService {
private WxMpUserBlacklistService blackListService = new WxMpUserBlacklistServiceImpl(this);
private WxMpTemplateMsgService templateMsgService = new WxMpTemplateMsgServiceImpl(this);
private CloseableHttpClient httpClient;
private HttpHost httpProxy;
@@ -221,35 +223,6 @@ public class WxMpServiceImpl implements WxMpService {
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
}
@Override
public String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send";
String responseContent = this.post(url, templateMessage.toJson());
final JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
if (jsonObject.get("errcode").getAsInt() == 0){
return jsonObject.get("msgid").getAsString();
}
throw new WxErrorException(WxError.fromJson(responseContent));
}
@Override
public String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException {
if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId()
|| null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) {
throw new IllegalArgumentException("industry id is empty");
}
String url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry";
return this.post(url, wxMpIndustry.toJson());
}
@Override
public WxMpIndustry getIndustry() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry";
String responseContent = this.get(url, null);
return WxMpIndustry.fromJson(responseContent);
}
@Override
public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException {
String url = "https://api.weixin.qq.com/semantic/semproxy/search";
@@ -562,4 +535,9 @@ public class WxMpServiceImpl implements WxMpService {
return this.storeService;
}
@Override
public WxMpTemplateMsgService getTemplateMsgService() {
return this.templateMsgService;
}
}

View File

@@ -0,0 +1,58 @@
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;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.bean.WxMpIndustry;
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
/**
* <pre>
* Created by Binary Wang on 2016-10-14.
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
* </pre>
*/
public class WxMpTemplateMsgServiceImpl implements WxMpTemplateMsgService {
public static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/template";
private static final JsonParser JSON_PARSER = new JsonParser();
private WxMpService wxMpService;
public WxMpTemplateMsgServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}
@Override
public String sendTemplateMsg(WxMpTemplateMessage templateMessage) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send";
String responseContent = this.wxMpService.post(url, templateMessage.toJson());
final JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
if (jsonObject.get("errcode").getAsInt() == 0) {
return jsonObject.get("msgid").getAsString();
}
throw new WxErrorException(WxError.fromJson(responseContent));
}
@Override
public boolean setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException {
if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId()
|| null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) {
throw new IllegalArgumentException("行业Id不能为空请核实");
}
String url = API_URL_PREFIX + "/api_set_industry";
this.wxMpService.post(url, wxMpIndustry.toJson());
return true;
}
@Override
public WxMpIndustry getIndustry() throws WxErrorException {
String url = API_URL_PREFIX + "/get_industry";
String responseContent = this.wxMpService.get(url, null);
return WxMpIndustry.fromJson(responseContent);
}
}