mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-05 17:38:05 +08:00
🆕 #1983 【微信公众号】增加订阅通知相关接口
This commit is contained in:
parent
fa179f6cba
commit
06a1726665
@ -1,4 +1,4 @@
|
||||
package cn.binarywang.wx.miniapp.bean.subscribemsg;
|
||||
package me.chanjar.weixin.common.bean.subscribemsg;
|
||||
|
||||
import lombok.Data;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.binarywang.wx.miniapp.bean.subscribemsg;
|
||||
package me.chanjar.weixin.common.bean.subscribemsg;
|
||||
|
||||
import lombok.Data;
|
||||
|
@ -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;
|
||||
@ -10,15 +10,15 @@ import java.util.List;
|
||||
* @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
|
@ -1,4 +1,4 @@
|
||||
package cn.binarywang.wx.miniapp.bean.subscribemsg;
|
||||
package me.chanjar.weixin.common.bean.subscribemsg;
|
||||
|
||||
import lombok.Data;
|
||||
|
@ -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;
|
||||
@ -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>
|
||||
|
@ -3,10 +3,10 @@ package cn.binarywang.wx.miniapp.api.impl;
|
||||
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;
|
||||
@ -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
|
||||
|
@ -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;
|
||||
@ -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);
|
||||
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,14 @@
|
||||
package me.chanjar.weixin.mp.bean.subscribe;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Mklaus
|
||||
* @date 2018-01-22 下午12:18
|
||||
@ -64,6 +65,15 @@ public class WxMpSubscribeMessage {
|
||||
*/
|
||||
private String contentColor;
|
||||
|
||||
/**
|
||||
* 跳转网页时填写.
|
||||
*/
|
||||
private String page;
|
||||
|
||||
/**
|
||||
* 订阅通知消息专用
|
||||
*/
|
||||
private Map<String, String> dataMap;
|
||||
|
||||
public String toJson() {
|
||||
return WxMpGsonBuilder.create().toJson(this);
|
||||
|
@ -280,9 +280,40 @@ public interface WxMpApiUrl {
|
||||
*/
|
||||
SUBSCRIBE_MESSAGE_AUTHORIZE_URL(MP_DEFAULT_HOST_URL, "/mp/subscribemsg?action=get_confirm&appid=%s&scene=%d&template_id=%s&redirect_url=%s&reserved=%s#wechat_redirect"),
|
||||
/**
|
||||
* subscribe.
|
||||
* subscribe once.
|
||||
*/
|
||||
SEND_MESSAGE_URL(API_DEFAULT_HOST_URL, "/cgi-bin/message/template/subscribe");
|
||||
SEND_MESSAGE_ONCE_URL(API_DEFAULT_HOST_URL, "/cgi-bin/message/template/subscribe"),
|
||||
/**
|
||||
* 订阅通知消息发送.
|
||||
*/
|
||||
SEND_SUBSCRIBE_MESSAGE_URL(API_DEFAULT_HOST_URL, "/cgi-bin/message/subscribe/bizsend"),
|
||||
/**
|
||||
* 获取模板标题下的关键词列表.
|
||||
*/
|
||||
GET_PUB_TEMPLATE_TITLE_LIST_URL (API_DEFAULT_HOST_URL, "/wxaapi/newtmpl/getpubtemplatetitles"),
|
||||
/**
|
||||
* 获取模板标题下的关键词列表.
|
||||
*/
|
||||
GET_PUB_TEMPLATE_KEY_WORDS_BY_ID_URL (API_DEFAULT_HOST_URL, "/wxaapi/newtmpl/getpubtemplatekeywords"),
|
||||
/**
|
||||
* 组合模板并添加至帐号下的个人模板库.
|
||||
*/
|
||||
TEMPLATE_ADD_URL(API_DEFAULT_HOST_URL, "/wxaapi/newtmpl/addtemplate"),
|
||||
/**
|
||||
* 获取当前帐号下的个人模板列表.
|
||||
*/
|
||||
TEMPLATE_LIST_URL(API_DEFAULT_HOST_URL, "/wxaapi/newtmpl/gettemplate"),
|
||||
/**
|
||||
* 删除帐号下的某个模板.
|
||||
*/
|
||||
TEMPLATE_DEL_URL(API_DEFAULT_HOST_URL, "/wxaapi/newtmpl/deltemplate"),
|
||||
/**
|
||||
* 获取小程序账号的类目
|
||||
*/
|
||||
GET_CATEGORY_URL (API_DEFAULT_HOST_URL, "/wxaapi/newtmpl/getcategory"),
|
||||
UNIFORM_MSG_SEND_URL(API_DEFAULT_HOST_URL, "/cgi-bin/message/wxopen/template/uniform_send"),
|
||||
ACTIVITY_ID_CREATE_URL(API_DEFAULT_HOST_URL, "/cgi-bin/message/wxopen/activityid/create"),
|
||||
UPDATABLE_MSG_SEND_URL(API_DEFAULT_HOST_URL, "/cgi-bin/message/wxopen/updatablemsg/send");
|
||||
|
||||
private final String prefix;
|
||||
private final String path;
|
||||
|
@ -1,13 +1,13 @@
|
||||
package me.chanjar.weixin.mp.util.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
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
|
||||
@ -24,6 +24,10 @@ public class WxMpSubscribeMessageGsonAdapter implements JsonSerializer<WxMpSubsc
|
||||
messageJson.addProperty("url", message.getUrl());
|
||||
}
|
||||
|
||||
if (message.getPage() != null) {
|
||||
messageJson.addProperty("page", message.getPage());
|
||||
}
|
||||
|
||||
final WxMpSubscribeMessage.MiniProgram miniProgram = message.getMiniProgram();
|
||||
if (miniProgram != null) {
|
||||
JsonObject miniProgramJson = new JsonObject();
|
||||
@ -39,18 +43,29 @@ public class WxMpSubscribeMessageGsonAdapter implements JsonSerializer<WxMpSubsc
|
||||
messageJson.addProperty("scene", message.getScene());
|
||||
messageJson.addProperty("title", message.getTitle());
|
||||
|
||||
JsonObject data = new JsonObject();
|
||||
messageJson.add("data", data);
|
||||
if (message.getDataMap() == null) {
|
||||
JsonObject data = new JsonObject();
|
||||
messageJson.add("data", data);
|
||||
|
||||
JsonObject content = new JsonObject();
|
||||
data.add("content", content);
|
||||
JsonObject content = new JsonObject();
|
||||
data.add("content", content);
|
||||
|
||||
if (message.getContentValue() != null) {
|
||||
content.addProperty("value", message.getContentValue());
|
||||
}
|
||||
if (message.getContentValue() != null) {
|
||||
content.addProperty("value", message.getContentValue());
|
||||
}
|
||||
|
||||
if (message.getContentColor() != null) {
|
||||
content.addProperty("color", message.getContentColor());
|
||||
}
|
||||
} else {
|
||||
JsonObject data = new JsonObject();
|
||||
messageJson.add("data", data);
|
||||
message.getDataMap().forEach((key, value) -> {
|
||||
JsonObject content = new JsonObject();
|
||||
content.addProperty("value", value);
|
||||
data.add(key, content);
|
||||
});
|
||||
|
||||
if (message.getContentColor() != null) {
|
||||
content.addProperty("color", message.getContentColor());
|
||||
}
|
||||
|
||||
return messageJson;
|
||||
|
@ -21,7 +21,7 @@ public class WxMpSubscribeMsgServiceImplTest {
|
||||
protected WxMpService wxService;
|
||||
|
||||
@Test
|
||||
public void testSendSubscribeMessage() throws WxErrorException {
|
||||
public void testSendSubscribeOnceMessage() throws WxErrorException {
|
||||
TestConfigStorage configStorage = (TestConfigStorage) this.wxService
|
||||
.getWxMpConfigStorage();
|
||||
|
||||
@ -34,7 +34,7 @@ public class WxMpSubscribeMsgServiceImplTest {
|
||||
.build();
|
||||
|
||||
try {
|
||||
boolean send = this.wxService.getSubscribeMsgService().sendSubscribeMessage(message);
|
||||
boolean send = this.wxService.getSubscribeMsgService().sendOnce(message);
|
||||
Assert.assertTrue(send);
|
||||
} catch (WxErrorException e) {
|
||||
// 当用户没有授权,获取之前的授权已使用。微信会返回错误代码 {"errcode":43101,"errmsg":"user refuse to accept the msg hint: [xxxxxxxxxxx]"}
|
||||
@ -45,4 +45,35 @@ public class WxMpSubscribeMsgServiceImplTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubscribeMsgAuthorizationUrl() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPubTemplateTitleList() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPubTemplateKeyWordsById() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddTemplate() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTemplateList() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelTemplate() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCategory() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSend() {
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user