mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
🆕 #1983 【微信公众号】增加订阅通知相关接口
This commit is contained in:
@@ -259,6 +259,16 @@ public interface WxMpService extends WxService {
|
||||
*/
|
||||
String post(WxMpApiUrl url, String postData) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求.
|
||||
*
|
||||
* @param url 请求接口地址
|
||||
* @param obj 请求参数
|
||||
* @return 接口响应字符串 string
|
||||
* @throws WxErrorException 异常
|
||||
*/
|
||||
String post(WxMpApiUrl url, Object obj) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求.
|
||||
*
|
||||
|
||||
@@ -1,16 +1,21 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import me.chanjar.weixin.common.bean.subscribemsg.CategoryData;
|
||||
import me.chanjar.weixin.common.bean.subscribemsg.PubTemplateKeyword;
|
||||
import me.chanjar.weixin.common.bean.subscribemsg.PubTemplateTitleListResult;
|
||||
import me.chanjar.weixin.common.bean.subscribemsg.TemplateInfo;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 一次性订阅消息接口
|
||||
* https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1500374289_66bvB
|
||||
* 订阅消息服务接口
|
||||
* </pre>
|
||||
*
|
||||
* @author Mklaus
|
||||
* @date 2018-01-22 上午11:07
|
||||
* @date 2018 -01-22 上午11:07
|
||||
*/
|
||||
public interface WxMpSubscribeMsgService {
|
||||
/**
|
||||
@@ -20,9 +25,9 @@ public interface WxMpSubscribeMsgService {
|
||||
* </pre>
|
||||
*
|
||||
* @param redirectURI 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode
|
||||
* @param scene 重定向后会带上scene参数,开发者可以填0-10000的整形值,用来标识订阅场景值
|
||||
* @param reserved 用于保持请求和回调的状态,授权请后原样带回给第三方 (最多128字节,要求做urlencode)
|
||||
* @return url
|
||||
* @param scene 重定向后会带上scene参数,开发者可以填0-10000的整形值,用来标识订阅场景值
|
||||
* @param reserved 用于保持请求和回调的状态,授权请后原样带回给第三方 (最多128字节,要求做urlencode)
|
||||
* @return url string
|
||||
*/
|
||||
String subscribeMsgAuthorizationUrl(String redirectURI, int scene, String reserved);
|
||||
|
||||
@@ -32,8 +37,106 @@ public interface WxMpSubscribeMsgService {
|
||||
* 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1500374289_66bvB
|
||||
* </pre>
|
||||
*
|
||||
* @return 消息Id
|
||||
* @param message the message
|
||||
* @return 消息Id boolean
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
boolean sendSubscribeMessage(WxMpSubscribeMessage message) throws WxErrorException;
|
||||
boolean sendOnce(WxMpSubscribeMessage message) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 获取帐号所属类目下的公共模板标题
|
||||
*
|
||||
* 详情请见: <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getPubTemplateTitleList.html">获取帐号所属类目下的公共模板标题</a>
|
||||
* 接口url格式: https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles?access_token=ACCESS_TOKEN
|
||||
* </pre>
|
||||
*
|
||||
* @param ids 类目 id,多个用逗号隔开
|
||||
* @param start 用于分页,表示从 start 开始。从 0 开始计数。
|
||||
* @param limit 用于分页,表示拉取 limit 条记录。最大为 30。
|
||||
* @return . pub template title list
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
PubTemplateTitleListResult getPubTemplateTitleList(String[] ids, int start, int limit) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 获取模板库某个模板标题下关键词库
|
||||
*
|
||||
* 详情请见: https://developers.weixin.qq.com/doc/offiaccount/Subscription_Messages/api.html
|
||||
* 接口url格式: GET https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords?access_token=ACCESS_TOKEN
|
||||
* </pre>
|
||||
*
|
||||
* @param id 模板标题 id,可通过接口获取
|
||||
* @return . pub template key words by id
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
List<PubTemplateKeyword> getPubTemplateKeyWordsById(String id) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 组合模板并添加至帐号下的个人模板库
|
||||
*
|
||||
* 详情请见: https://developers.weixin.qq.com/doc/offiaccount/Subscription_Messages/api.html
|
||||
* 接口url格式: POST https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token=ACCESS_TOKEN
|
||||
* </pre>
|
||||
*
|
||||
* @param id 模板标题 id,可通过接口获取,也可登录小程序后台查看获取
|
||||
* @param keywordIdList 模板关键词列表
|
||||
* @param sceneDesc 服务场景描述,15个字以内
|
||||
* @return 添加至帐号下的模板id ,发送小程序订阅消息时所需
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
String addTemplate(String id, List<Integer> keywordIdList, String sceneDesc) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 获取当前帐号下的个人模板列表
|
||||
*
|
||||
* 详情请见: https://developers.weixin.qq.com/doc/offiaccount/Subscription_Messages/api.html
|
||||
* 接口url格式: GET https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token=ACCESS_TOKEN
|
||||
* </pre>
|
||||
*
|
||||
* @return . template list
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
List<TemplateInfo> getTemplateList() throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 删除帐号下的某个模板
|
||||
*
|
||||
* 详情请见: https://developers.weixin.qq.com/doc/offiaccount/Subscription_Messages/api.html
|
||||
* 接口url格式: POST https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate?access_token=ACCESS_TOKEN
|
||||
* </pre>
|
||||
*
|
||||
* @param templateId 要删除的模板id
|
||||
* @return 删除是否成功 boolean
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
boolean delTemplate(String templateId) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 获取公众号类目
|
||||
* https://developers.weixin.qq.com/doc/offiaccount/Subscription_Messages/api.html
|
||||
* GET https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token=ACCESS_TOKEN
|
||||
* </pre>
|
||||
*
|
||||
* @return . category
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
List<CategoryData> getCategory() throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 发送订阅消息
|
||||
* https://developers.weixin.qq.com/doc/offiaccount/Subscription_Messages/api.html
|
||||
* </pre>
|
||||
*
|
||||
* @param subscribeMessage 订阅消息
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
void send(WxMpSubscribeMessage subscribeMessage) throws WxErrorException;
|
||||
|
||||
}
|
||||
|
||||
@@ -287,6 +287,11 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
return this.post(url.getUrl(this.getWxMpConfigStorage()), postData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String post(WxMpApiUrl url, Object obj) throws WxErrorException {
|
||||
return this.execute(SimplePostRequestExecutor.create(this), url, WxGsonBuilder.create().toJson(obj));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String post(WxMpApiUrl url, JsonObject jsonObject) throws WxErrorException {
|
||||
return this.post(url.getUrl(this.getWxMpConfigStorage()), jsonObject.toString());
|
||||
|
||||
@@ -1,40 +1,113 @@
|
||||
package me.chanjar.weixin.mp.api.impl;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.common.bean.subscribemsg.CategoryData;
|
||||
import me.chanjar.weixin.common.bean.subscribemsg.PubTemplateKeyword;
|
||||
import me.chanjar.weixin.common.bean.subscribemsg.PubTemplateTitleListResult;
|
||||
import me.chanjar.weixin.common.bean.subscribemsg.TemplateInfo;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.URIUtil;
|
||||
import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.api.WxMpSubscribeMsgService;
|
||||
import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.SubscribeMsg.SEND_MESSAGE_URL;
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.SubscribeMsg.SUBSCRIBE_MESSAGE_AUTHORIZE_URL;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.SubscribeMsg.*;
|
||||
|
||||
/**
|
||||
* 一次性订阅消息接口.
|
||||
* 订阅消息接口.
|
||||
*
|
||||
* @author Mklaus
|
||||
* @date 2018-01-22 上午11:19
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class WxMpSubscribeMsgServiceImpl implements WxMpSubscribeMsgService {
|
||||
private final WxMpService wxMpService;
|
||||
private static final String ERR_CODE = "errcode";
|
||||
private final WxMpService service;
|
||||
|
||||
@Override
|
||||
public String subscribeMsgAuthorizationUrl(String redirectURI, int scene, String reserved) {
|
||||
WxMpConfigStorage storage = this.wxMpService.getWxMpConfigStorage();
|
||||
public String subscribeMsgAuthorizationUrl(String redirectUri, int scene, String reserved) {
|
||||
WxMpConfigStorage storage = this.service.getWxMpConfigStorage();
|
||||
return String.format(SUBSCRIBE_MESSAGE_AUTHORIZE_URL.getUrl(storage), storage.getAppId(), scene, storage.getTemplateId(),
|
||||
URIUtil.encodeURIComponent(redirectURI), reserved);
|
||||
URIUtil.encodeURIComponent(redirectUri), reserved);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendSubscribeMessage(WxMpSubscribeMessage message) throws WxErrorException {
|
||||
public boolean sendOnce(WxMpSubscribeMessage message) throws WxErrorException {
|
||||
if (message.getTemplateId() == null) {
|
||||
message.setTemplateId(this.wxMpService.getWxMpConfigStorage().getTemplateId());
|
||||
message.setTemplateId(this.service.getWxMpConfigStorage().getTemplateId());
|
||||
}
|
||||
|
||||
String responseContent = this.wxMpService.post(SEND_MESSAGE_URL, message.toJson());
|
||||
String responseContent = this.service.post(SEND_MESSAGE_ONCE_URL, message.toJson());
|
||||
return responseContent != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PubTemplateTitleListResult getPubTemplateTitleList(String[] ids, int start, int limit) throws WxErrorException {
|
||||
ImmutableMap<String, ? extends Serializable> params = ImmutableMap.of("ids", StringUtils.join(ids, ","),
|
||||
"start", start, "limit", limit);
|
||||
String responseText = this.service.get(GET_PUB_TEMPLATE_TITLE_LIST_URL,
|
||||
Joiner.on("&").withKeyValueSeparator("=").join(params));
|
||||
return PubTemplateTitleListResult.fromJson(responseText);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PubTemplateKeyword> getPubTemplateKeyWordsById(String id) throws WxErrorException {
|
||||
String responseText = this.service.get(GET_PUB_TEMPLATE_KEY_WORDS_BY_ID_URL,
|
||||
Joiner.on("&").withKeyValueSeparator("=").join(ImmutableMap.of("tid", id)));
|
||||
return WxMpGsonBuilder.create().fromJson(GsonParser.parse(responseText)
|
||||
.getAsJsonArray("data"), new TypeToken<List<PubTemplateKeyword>>() {
|
||||
}.getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String addTemplate(String id, List<Integer> keywordIdList, String sceneDesc) throws WxErrorException {
|
||||
String responseText = this.service.post(TEMPLATE_ADD_URL, ImmutableMap.of("tid", id,
|
||||
"kidList", keywordIdList.toArray(),
|
||||
"sceneDesc", sceneDesc));
|
||||
return GsonParser.parse(responseText).get("priTmplId").getAsString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TemplateInfo> getTemplateList() throws WxErrorException {
|
||||
String responseText = this.service.get(TEMPLATE_LIST_URL, null);
|
||||
return WxMpGsonBuilder.create().fromJson(GsonParser.parse(responseText)
|
||||
.getAsJsonArray("data"), new TypeToken<List<TemplateInfo>>() {
|
||||
}.getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delTemplate(String templateId) throws WxErrorException {
|
||||
this.service.post(TEMPLATE_DEL_URL, ImmutableMap.of("priTmplId", templateId));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CategoryData> getCategory() throws WxErrorException {
|
||||
String responseText = this.service.get(GET_CATEGORY_URL, null);
|
||||
return WxMpGsonBuilder.create().fromJson(GsonParser.parse(responseText)
|
||||
.getAsJsonArray("data"), new TypeToken<List<CategoryData>>() {
|
||||
}.getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(WxMpSubscribeMessage subscribeMessage) throws WxErrorException {
|
||||
String responseContent = this.service.post(SEND_SUBSCRIBE_MESSAGE_URL, subscribeMessage.toJson());
|
||||
JsonObject jsonObject = GsonParser.parse(responseContent);
|
||||
if (jsonObject.get(ERR_CODE).getAsInt() != 0) {
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user