Skip to content

Commit

Permalink
🆕 #1983 【微信公众号】增加订阅通知相关接口
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Jan 31, 2021
1 parent fa179f6 commit 06a1726
Show file tree
Hide file tree
Showing 15 changed files with 336 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.binarywang.wx.miniapp.bean.subscribemsg;
package me.chanjar.weixin.common.bean.subscribemsg;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.binarywang.wx.miniapp.bean.subscribemsg;
package me.chanjar.weixin.common.bean.subscribemsg;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.binarywang.wx.miniapp.bean.template;
package me.chanjar.weixin.common.bean.subscribemsg;

import lombok.Data;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
Expand All @@ -10,15 +10,15 @@
* @author ArBing
*/
@Data
public class WxMaPubTemplateTitleListResult implements Serializable {
public class PubTemplateTitleListResult implements Serializable {
private static final long serialVersionUID = -7718911668757837527L;

private int count;

private List<TemplateItem> data;

public static WxMaPubTemplateTitleListResult fromJson(String json) {
return WxGsonBuilder.create().fromJson(json, WxMaPubTemplateTitleListResult.class);
public static PubTemplateTitleListResult fromJson(String json) {
return WxGsonBuilder.create().fromJson(json, PubTemplateTitleListResult.class);
}

@Data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.binarywang.wx.miniapp.bean.subscribemsg;
package me.chanjar.weixin.common.bean.subscribemsg;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cn.binarywang.wx.miniapp.api;

import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.bean.subscribemsg.CategoryData;
import cn.binarywang.wx.miniapp.bean.subscribemsg.PubTemplateKeyword;
import cn.binarywang.wx.miniapp.bean.subscribemsg.TemplateInfo;
import cn.binarywang.wx.miniapp.bean.template.WxMaPubTemplateTitleListResult;
import me.chanjar.weixin.common.bean.subscribemsg.CategoryData;
import me.chanjar.weixin.common.bean.subscribemsg.PubTemplateKeyword;
import me.chanjar.weixin.common.bean.subscribemsg.TemplateInfo;
import me.chanjar.weixin.common.bean.subscribemsg.PubTemplateTitleListResult;
import me.chanjar.weixin.common.error.WxErrorException;

import java.util.List;
Expand All @@ -31,7 +31,7 @@ public interface WxMaSubscribeService {
* @return .
* @throws WxErrorException .
*/
WxMaPubTemplateTitleListResult getPubTemplateTitleList(String[] ids, int start, int limit) throws WxErrorException;
PubTemplateTitleListResult getPubTemplateTitleList(String[] ids, int start, int limit) throws WxErrorException;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.bean.subscribemsg.CategoryData;
import cn.binarywang.wx.miniapp.bean.subscribemsg.PubTemplateKeyword;
import cn.binarywang.wx.miniapp.bean.subscribemsg.TemplateInfo;
import cn.binarywang.wx.miniapp.bean.template.WxMaPubTemplateTitleListResult;
import me.chanjar.weixin.common.bean.subscribemsg.CategoryData;
import me.chanjar.weixin.common.bean.subscribemsg.PubTemplateKeyword;
import me.chanjar.weixin.common.bean.subscribemsg.TemplateInfo;
import me.chanjar.weixin.common.bean.subscribemsg.PubTemplateTitleListResult;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
import com.google.common.base.Joiner;
Expand Down Expand Up @@ -34,12 +34,12 @@ public class WxMaSubscribeServiceImpl implements WxMaSubscribeService {
private final WxMaService service;

@Override
public WxMaPubTemplateTitleListResult getPubTemplateTitleList(String[] ids, int start, int limit) throws WxErrorException {
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 WxMaPubTemplateTitleListResult.fromJson(responseText);
return PubTemplateTitleListResult.fromJson(responseText);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cn.binarywang.wx.miniapp.api.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.subscribemsg.CategoryData;
import cn.binarywang.wx.miniapp.bean.subscribemsg.PubTemplateKeyword;
import cn.binarywang.wx.miniapp.bean.subscribemsg.TemplateInfo;
import cn.binarywang.wx.miniapp.bean.template.WxMaPubTemplateTitleListResult;
import me.chanjar.weixin.common.bean.subscribemsg.CategoryData;
import me.chanjar.weixin.common.bean.subscribemsg.PubTemplateKeyword;
import me.chanjar.weixin.common.bean.subscribemsg.TemplateInfo;
import me.chanjar.weixin.common.bean.subscribemsg.PubTemplateTitleListResult;
import cn.binarywang.wx.miniapp.test.ApiTestModule;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
Expand All @@ -30,7 +30,7 @@ public class WxMaSubscribeServiceImplTest {

@Test
public void testGetPubTemplateTitleList() throws WxErrorException {
WxMaPubTemplateTitleListResult result = this.wxService.getSubscribeService().getPubTemplateTitleList(new String[]{"2", "616"}, 0, 30);
PubTemplateTitleListResult result = this.wxService.getSubscribeService().getPubTemplateTitleList(new String[]{"2", "616"}, 0, 30);
System.out.println(result);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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请求.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand All @@ -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);

Expand All @@ -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 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 .
*/
boolean sendSubscribeMessage(WxMpSubscribeMessage message) throws WxErrorException;
void send(WxMpSubscribeMessage subscribeMessage) throws WxErrorException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ public String post(WxMpApiUrl url, String postData) throws WxErrorException {
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());
Expand Down
Loading

0 comments on commit 06a1726

Please sign in to comment.