mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-02-18 06:06:23 +08:00
#436 添加一次性订阅消息接口
This commit is contained in:
@@ -85,6 +85,8 @@ public interface WxMpConfigStorage {
|
||||
|
||||
String getAesKey();
|
||||
|
||||
String getTemplateId();
|
||||
|
||||
long getExpiresTime();
|
||||
|
||||
String getOauth2redirectUri();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -363,6 +363,13 @@ public interface WxMpService {
|
||||
*/
|
||||
WxMpTemplateMsgService getTemplateMsgService();
|
||||
|
||||
/**
|
||||
* 返回一次性订阅消息相关接口方法的实现类对象,以方便调用其各个接口
|
||||
*
|
||||
* @return WxMpSubscribeMsgService
|
||||
*/
|
||||
WxMpSubscribeMsgService getSubscribeMsgService();
|
||||
|
||||
/**
|
||||
* 返回硬件平台相关接口方法的实现类对象,以方便调用其各个接口
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package me.chanjar.weixin.mp.bean.subscribe;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
|
||||
/**
|
||||
* @author Mklaus
|
||||
* @date 2018-01-22 下午12:18
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
public class WxMpSubscribeMessage {
|
||||
|
||||
/**
|
||||
* 接收者openid.
|
||||
*/
|
||||
private String toUser;
|
||||
|
||||
/**
|
||||
* 模板ID.
|
||||
*/
|
||||
private String templateId;
|
||||
|
||||
/**
|
||||
* 模板跳转链接.
|
||||
* <pre>
|
||||
* url和miniprogram都是非必填字段,若都不传则模板无跳转;若都传,会优先跳转至小程序。
|
||||
* 开发者可根据实际需要选择其中一种跳转方式即可。当用户的微信客户端版本不支持跳小程序时,将会跳转至url。
|
||||
* </pre>
|
||||
*/
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* 跳小程序所需数据,不需跳小程序可不用传该数据.
|
||||
*
|
||||
* @see #url
|
||||
*/
|
||||
private WxMpTemplateMessage.MiniProgram miniProgram;
|
||||
|
||||
/**
|
||||
* 订阅场景值
|
||||
*/
|
||||
private String scene;
|
||||
|
||||
/**
|
||||
* 消息标题 (15字以内)
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 消息内容文本 (200字以内)
|
||||
*/
|
||||
private String contentValue;
|
||||
|
||||
/**
|
||||
* 消息内容文本颜色
|
||||
*/
|
||||
private String contentColor;
|
||||
|
||||
|
||||
public String toJson() {
|
||||
return WxMpGsonBuilder.INSTANCE.create().toJson(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import me.chanjar.weixin.mp.bean.membercard.WxMpMemberCardUpdateResult;
|
||||
import me.chanjar.weixin.mp.bean.membercard.WxMpMemberCardUserInfoResult;
|
||||
import me.chanjar.weixin.mp.bean.material.*;
|
||||
import me.chanjar.weixin.mp.bean.result.*;
|
||||
import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage;
|
||||
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
|
||||
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
|
||||
|
||||
@@ -30,6 +31,7 @@ public class WxMpGsonBuilder {
|
||||
INSTANCE.registerTypeAdapter(WxMpMassUploadResult.class, new WxMpMassUploadResultAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMpQrCodeTicket.class, new WxQrCodeTicketAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMpTemplateMessage.class, new WxMpTemplateMessageGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMpSubscribeMessage.class, new WxMpSubscribeMessageGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMpSemanticQueryResult.class, new WxMpSemanticQueryResultAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMpOAuth2AccessToken.class, new WxMpOAuth2AccessTokenAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxDataCubeUserSummary.class, new WxMpUserSummaryGsonAdapter());
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package me.chanjar.weixin.mp.util.json;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* @author Mklaus
|
||||
* @date 2018-01-22 下午12:31
|
||||
*/
|
||||
public class WxMpSubscribeMessageGsonAdapter implements JsonSerializer<WxMpSubscribeMessage> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(WxMpSubscribeMessage message, Type type, JsonSerializationContext jsonSerializationContext) {
|
||||
JsonObject messageJson = new JsonObject();
|
||||
messageJson.addProperty("touser", message.getToUser());
|
||||
messageJson.addProperty("template_id", message.getTemplateId());
|
||||
|
||||
if (message.getUrl() != null) {
|
||||
messageJson.addProperty("url", message.getUrl());
|
||||
}
|
||||
|
||||
if (message.getMiniProgram() != null) {
|
||||
JsonObject miniProgramJson = new JsonObject();
|
||||
miniProgramJson.addProperty("appid", message.getMiniProgram().getAppid());
|
||||
miniProgramJson.addProperty("pagepath", message.getMiniProgram().getPagePath());
|
||||
messageJson.add("miniprogram", miniProgramJson);
|
||||
}
|
||||
|
||||
messageJson.addProperty("scene", message.getScene());
|
||||
messageJson.addProperty("title", message.getTitle());
|
||||
|
||||
JsonObject data = new JsonObject();
|
||||
messageJson.add("data", data);
|
||||
|
||||
JsonObject content = new JsonObject();
|
||||
data.add("content", content);
|
||||
|
||||
if (message.getContentValue() != null) {
|
||||
content.addProperty("value", message.getContentValue());
|
||||
}
|
||||
|
||||
if (message.getContentColor() != null) {
|
||||
content.addProperty("color", message.getContentColor());
|
||||
}
|
||||
|
||||
return messageJson;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user