Merge pull request #189 from 1377382065/master

add SubscribeMessageApi
This commit is contained in:
jinyu 2020-01-08 12:13:08 +08:00 committed by GitHub
commit 15819c7a43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 253 additions and 0 deletions

View File

@ -15,6 +15,7 @@ import com.foxinmy.weixin4j.wxa.api.QrCodeApi;
import com.foxinmy.weixin4j.wxa.api.SecCheckApi; import com.foxinmy.weixin4j.wxa.api.SecCheckApi;
import com.foxinmy.weixin4j.wxa.api.TemplateApi; import com.foxinmy.weixin4j.wxa.api.TemplateApi;
import com.foxinmy.weixin4j.wxa.api.TemplateMessageApi; import com.foxinmy.weixin4j.wxa.api.TemplateMessageApi;
import com.foxinmy.weixin4j.wxa.api.SubscribeMessageApi;
/** /**
* The facade of WeChat Mini Program APIs. * The facade of WeChat Mini Program APIs.
@ -29,6 +30,7 @@ public class WeixinAppFacade {
private final TemplateMessageApi templateMessageApi; private final TemplateMessageApi templateMessageApi;
private final CustomMessageApi customMessageApi; private final CustomMessageApi customMessageApi;
private final SecCheckApi secCheckApi; private final SecCheckApi secCheckApi;
private final SubscribeMessageApi subscribeMessageApi;
/** /**
* Constructs {@link WeixinAppFacade} using {@link FileCacheStorager}. * Constructs {@link WeixinAppFacade} using {@link FileCacheStorager}.
@ -111,6 +113,7 @@ public class WeixinAppFacade {
this.templateMessageApi = new TemplateMessageApi(tokenManager, properties); this.templateMessageApi = new TemplateMessageApi(tokenManager, properties);
this.customMessageApi = new CustomMessageApi(tokenManager, properties); this.customMessageApi = new CustomMessageApi(tokenManager, properties);
this.secCheckApi = new SecCheckApi(tokenManager, properties); this.secCheckApi = new SecCheckApi(tokenManager, properties);
this.subscribeMessageApi = new SubscribeMessageApi(tokenManager, properties);
} }
/** /**
@ -149,6 +152,15 @@ public class WeixinAppFacade {
return templateMessageApi; return templateMessageApi;
} }
/**
* 获取订阅消息相关的 API
*
* @return 模板消息相关的 API
*/
public SubscribeMessageApi getSubscribeMessageApi() {
return subscribeMessageApi;
}
/** /**
* 获取客服消息相关的 API * 获取客服消息相关的 API
* *

View File

@ -0,0 +1,42 @@
package com.foxinmy.weixin4j.wxa.api;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.token.TokenManager;
import java.util.Map;
import java.util.Properties;
public class SubscribeMessageApi extends TokenManagerApi {
public SubscribeMessageApi(TokenManager tokenManager) {
super(tokenManager, null);
}
public SubscribeMessageApi(TokenManager tokenManager, Properties properties) {
super(tokenManager, properties);
}
/**
* 发送订阅消息
*
* @param toUser 接收者用户 openid
* @param templateId 所需下发的订阅模板id
* @param page 点击模板卡片后的跳转页面仅限本小程序内的页面支持带参数,示例index?foo=bar该字段不填则模板无跳转
* @param data 模板内容格式形如 { "key1": { "value": any }, "key2": { "value": any } }
* @throws WeixinException indicates getting access token failed, or sending template message failed.
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html">发送订阅消息</a>
*/
public void sendSubscribeMessage(
String toUser,
String templateId,
String page,
Map<String, String> data
) throws WeixinException {
final SubscribeMessageParameter message = new SubscribeMessageParameter(
toUser, templateId, page, data
);
WxaApiResult r = this.post("wxopen_subscribe_message_send", message, WxaApiResult.TYPE_REFERENCE);
r.checkErrCode();
}
}

View File

@ -0,0 +1,101 @@
package com.foxinmy.weixin4j.wxa.api;
import com.alibaba.fastjson.annotation.JSONField;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
class SubscribeMessageParameter {
private static final long serialVersionUID = 2018052601L;
private String toUser;
private String templateId;
private String page;
private Map<String, SubscribeMessageData> data;
public SubscribeMessageParameter() {
}
public SubscribeMessageParameter(
String toUser,
String templateId,
String page,
Map<String, String> data
) {
this.toUser = toUser;
this.templateId = templateId;
this.page = page;
if (data != null) {
this.data = new HashMap<String, SubscribeMessageData>(data.size());
for (Map.Entry<String, String> entry : data.entrySet()) {
this.data.put(entry.getKey(), new SubscribeMessageData(entry.getValue()));
}
}
}
@JSONField(name = "touser")
public String getToUser() {
return toUser;
}
public void setToUser(String toUser) {
this.toUser = toUser;
}
@JSONField(name = "template_id")
public String getTemplateId() {
return templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public Map<String, SubscribeMessageData> getData() {
return data;
}
public void setData(Map<String, SubscribeMessageData> data) {
this.data = data;
}
/**
* SubscribeMessageData
*/
public static class SubscribeMessageData implements Serializable {
private static final long serialVersionUID = 2018052601L;
private String value;
public SubscribeMessageData() {
}
public SubscribeMessageData(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}

View File

@ -0,0 +1,90 @@
package com.foxinmy.weixin4j.wxa.model.subscribemessage;
/**
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
*/
public enum ParameterType {
/**
* 20个以内字符 可汉字数字字母或符号组合
* 可汉字数字字母或符号组合
*/
THING("thing"),
/**
* 24小时制时间格式支持+年月日
* 例如15:012019年10月1日 15:01
*/
TIME("time"),
/**
* 年月日格式支持+24小时制时间
* 例如2019年10月1日2019年10月1日 15:01
*/
DATE("date"),
/**
* 10个以内纯汉字或20个以内纯字母或符号
* 中文名10个汉字内纯英文名20个字母内中文和字母混合按中文名算10个字内
*/
NAME("name"),
/**
* 32位以内数字
* 只能数字可带小数
*/
NUMBER("number"),
/**
* 32位以内字母
* 只能字母
*/
LETTER("letter"),
/**
* 5位以内符号
* 只能符号
*/
SYMBOL("symbol"),
/**
* 5个以内汉字
* 5个以内纯汉字例如配送中
*/
PHRASE("phrase"),
/**
* 1个币种符号+10位以内纯数字可带小数结尾可带
* 可带小数
*/
AMOUNT("amount"),
/**
* 8位以内第一位与最后一位可为汉字其余为字母或数字
* 车牌号码粤A8Z888挂
*/
CAR_NUMBER("car_number"),
/**
* 17位以内数字符号
* 电话号码+86-0766-66888866
*/
PHONE_NUMBER("phone_number"),
/**
* 32位以内数字字母或符号
* 可数字字母或符号组合
*/
CHARACTER_STRING("character_string");
private final String value;
ParameterType(final String value) {
this.value = value;
}
@Override
public String toString() {
return this.value;
}
}

View File

@ -0,0 +1,6 @@
/**
* Models for {@link com.foxinmy.weixin4j.wxa.api.TemplateApi}.
*
* @since 1.8
*/
package com.foxinmy.weixin4j.wxa.model.subscribemessage;

View File

@ -38,6 +38,8 @@ wxopen_template_del={api_cgi_url}/wxopen/template/del?access_token=%s
# \u53d1\u9001\u6a21\u7248\u6d88\u606f # \u53d1\u9001\u6a21\u7248\u6d88\u606f
wxopen_template_message_send={api_cgi_url}/message/wxopen/template/send?access_token=%s wxopen_template_message_send={api_cgi_url}/message/wxopen/template/send?access_token=%s
# \u53d1\u9001\u8ba2\u9605\u6d88\u606f
wxopen_subscribe_message_send={api_cgi_url}/message/subscribe/send?access_token=%s
# \u53d1\u9001\u5ba2\u670d\u6d88\u606f # \u53d1\u9001\u5ba2\u670d\u6d88\u606f
message_custom_send={api_cgi_url}/message/custom/send?access_token=%s message_custom_send={api_cgi_url}/message/custom/send?access_token=%s