#436 添加一次性订阅消息接口

This commit is contained in:
Mklaus
2018-01-23 10:23:29 +08:00
committed by Binary Wang
parent be50ea009c
commit f2b05480b5
14 changed files with 336 additions and 2 deletions

View File

@@ -85,6 +85,8 @@ public interface WxMpConfigStorage {
String getAesKey();
String getTemplateId();
long getExpiresTime();
String getOauth2redirectUri();

View File

@@ -18,6 +18,7 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
protected volatile String appId;
protected volatile String secret;
protected volatile String token;
protected volatile String templateId;
protected volatile String accessToken;
protected volatile String aesKey;
protected volatile long expiresTime;
@@ -173,6 +174,15 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
this.token = token;
}
@Override
public String getTemplateId() {
return this.templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
@Override
public long getExpiresTime() {
return this.expiresTime;

View File

@@ -363,6 +363,13 @@ public interface WxMpService {
*/
WxMpTemplateMsgService getTemplateMsgService();
/**
* 返回一次性订阅消息相关接口方法的实现类对象,以方便调用其各个接口
*
* @return WxMpSubscribeMsgService
*/
WxMpSubscribeMsgService getSubscribeMsgService();
/**
* 返回硬件平台相关接口方法的实现类对象,以方便调用其各个接口
*

View File

@@ -0,0 +1,40 @@
package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage;
/**
* <pre>
* 一次性订阅消息接口
* https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1500374289_66bvB
* </pre>
*
* @author Mklaus
* @date 2018-01-22 上午11:07
*/
public interface WxMpSubscribeMsgService {
/**
* <pre>
* 构造用户订阅一条模板消息授权的url连接
* 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1500374289_66bvB
* </pre>
*
* @param redirectURI 用户授权完成后的重定向链接无需urlencode, 方法内会进行encode
* @param scene 重定向后会带上scene参数开发者可以填0-10000的整形值用来标识订阅场景值
* @param reserved 用于保持请求和回调的状态,授权请后原样带回给第三方 (最多128字节要求做urlencode)
* @return url
*/
String subscribeMsgAuthorizationUrl(String redirectURI, int scene, String reserved);
/**
* <pre>
* 发送一次性订阅消息
* 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1500374289_66bvB
* </pre>
*
* @return 消息Id
*/
boolean sendSubscribeMessage(WxMpSubscribeMessage message) throws WxErrorException;
}

View File

@@ -40,6 +40,7 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
private WxMpDataCubeService dataCubeService = new WxMpDataCubeServiceImpl(this);
private WxMpUserBlacklistService blackListService = new WxMpUserBlacklistServiceImpl(this);
private WxMpTemplateMsgService templateMsgService = new WxMpTemplateMsgServiceImpl(this);
private WxMpSubscribeMsgService subscribeMsgService = new WxMpSubscribeMsgServiceImpl(this);
private WxMpDeviceService deviceService = new WxMpDeviceServiceImpl(this);
private WxMpShakeService shakeService = new WxMpShakeServiceImpl(this);
private WxMpMemberCardService memberCardService = new WxMpMemberCardServiceImpl(this);
@@ -375,6 +376,11 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
return this.templateMsgService;
}
@Override
public WxMpSubscribeMsgService getSubscribeMsgService() {
return this.subscribeMsgService;
}
@Override
public WxMpDeviceService getDeviceService() {
return this.deviceService;

View File

@@ -0,0 +1,42 @@
package me.chanjar.weixin.mp.api.impl;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.URIUtil;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpSubscribeMsgService;
import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage;
/**
* @author Mklaus
* @date 2018-01-22 上午11:19
*/
public class WxMpSubscribeMsgServiceImpl implements WxMpSubscribeMsgService {
private static final String SUBSCRIBE_MESSAGE_AUTHORIZE_URL =
"https://mp.weixin.qq.com/mp/subscribemsg?action=get_confirm&appid=%s&scene=%d&template_id=%s&redirect_url=%s&reserved=%s#wechat_redirect";
private static final String SEND_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/subscribe";
private WxMpService wxMpService;
public WxMpSubscribeMsgServiceImpl(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}
@Override
public String subscribeMsgAuthorizationUrl(String redirectURI, int scene, String reserved) {
WxMpConfigStorage storage = this.wxMpService.getWxMpConfigStorage();
return String.format(SUBSCRIBE_MESSAGE_AUTHORIZE_URL,
storage.getAppId(), scene, storage.getTemplateId(), URIUtil.encodeURIComponent(redirectURI), reserved);
}
@Override
public boolean sendSubscribeMessage(WxMpSubscribeMessage message) throws WxErrorException {
if (message.getTemplateId() == null) {
message.setTemplateId(this.wxMpService.getWxMpConfigStorage().getTemplateId());
}
String responseContent = this.wxMpService.post(SEND_MESSAGE_URL, message.toJson());
return responseContent != null;
}
}