🆕 #2451 【小程序】增加设备订阅消息相关接口

This commit is contained in:
JCLee 2021-12-17 11:57:15 +08:00 committed by GitHub
parent c1d0b68d32
commit d70b907407
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 287 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package cn.binarywang.wx.miniapp.api;
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceSubscribeMessageRequest;
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceTicketRequest;
import me.chanjar.weixin.common.error.WxErrorException;
/**
* 小程序设备订阅消息相关 API
* 文档
*
* @author <a href="https://github.com/leejuncheng">JCLee</a>
* @since 2021-12-16 17:13:35
*/
public interface WxMaDeviceSubscribeService {
/**
* <pre>
* 获取设备票据
* 应用场景
* 小程序前端界面拉起设备消息授权订阅弹框界面
* 注意
* 设备ticket有效时间为5分钟
* </pre>
* @param deviceTicketRequest
* @return
* @throws WxErrorException
*/
String getSnTicket(WxMaDeviceTicketRequest deviceTicketRequest) throws WxErrorException;
/**
* <pre>
* 发送设备订阅消息
* </pre>
*
* @param deviceSubscribeMessageRequest 订阅消息
* @throws WxErrorException .
*/
void sendDeviceSubscribeMsg(WxMaDeviceSubscribeMessageRequest deviceSubscribeMessageRequest) throws WxErrorException;
}

View File

@ -462,4 +462,11 @@ public interface WxMaService extends WxService {
* @return
*/
WxMaReimburseInvoiceService getReimburseInvoiceService();
/**
* 返回设备订阅消息相关接口服务对象
*
* @return WxMaDeviceSubscribeService plugin service
*/
WxMaDeviceSubscribeService getDeviceSubscribeService();
}

View File

@ -75,6 +75,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
private final WxMaShopDeliveryService shopDeliveryService = new WxMaShopDeliveryServiceImpl(this);
private final WxMaLinkService linkService = new WxMaLinkServiceImpl(this);
private final WxMaReimburseInvoiceService reimburseInvoiceService = new WxMaReimburseInvoiceServiceImpl(this);
private final WxMaDeviceSubscribeService deviceSubscribeService = new WxMaDeviceSubscribeServiceImpl(this);
private Map<String, WxMaConfig> configMap;
private int retrySleepMillis = 1000;
private int maxRetryTimes = 5;
@ -573,4 +574,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
public WxMaReimburseInvoiceService getReimburseInvoiceService() {
return this.reimburseInvoiceService;
}
@Override
public WxMaDeviceSubscribeService getDeviceSubscribeService(){ return this.deviceSubscribeService; }
}

View File

@ -0,0 +1,50 @@
package cn.binarywang.wx.miniapp.api.impl;
import cn.binarywang.wx.miniapp.api.WxMaDeviceSubscribeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceSubscribeMessageRequest;
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceTicketRequest;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
import com.google.gson.JsonObject;
import lombok.RequiredArgsConstructor;
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.json.GsonParser;
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.GET_SN_TICKET_URL;
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.SEND_DEVICE_SUBSCRIBE_MSG_URL;
/**
* 小程序设备订阅消息相关 API
* 文档
*
* @author <a href="https://github.com/leejuncheng">JCLee</a>
* @since 2021-12-16 17:13:35
*/
@RequiredArgsConstructor
public class WxMaDeviceSubscribeServiceImpl implements WxMaDeviceSubscribeService {
private final WxMaService service;
@Override
public String getSnTicket(WxMaDeviceTicketRequest deviceTicketRequest) throws WxErrorException {
String responseContent = this.service.post(GET_SN_TICKET_URL, deviceTicketRequest.toJson());
JsonObject jsonObject = GsonParser.parse(responseContent);
if (jsonObject.get(WxMaConstants.ERRCODE).getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
}
String snTicket = jsonObject.get("sn_ticket").getAsString();
return snTicket;
}
@Override
public void sendDeviceSubscribeMsg(WxMaDeviceSubscribeMessageRequest deviceSubscribeMessageRequest) throws WxErrorException {
String responseContent = this.service.post(SEND_DEVICE_SUBSCRIBE_MSG_URL, deviceSubscribeMessageRequest.toJson());
JsonObject jsonObject = GsonParser.parse(responseContent);
if (jsonObject.get(WxMaConstants.ERRCODE).getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
}
}
}

View File

@ -0,0 +1,72 @@
package cn.binarywang.wx.miniapp.bean.device;
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.List;
/**
* 小程序设备订阅消息请求参数
*
* @author <a href="https://github.com/leejuncheng">JCLee</a>
* @since 2021-12-16 17:13:22
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class WxMaDeviceSubscribeMessageRequest implements Serializable {
private static final long serialVersionUID = -7973228178407991299L;
/**
* 接收者用户 openid列表.
*/
@SerializedName("to_openid_list")
private List<String> toOpenidList;
/**
* 下发通知的设备唯序列号
*/
@SerializedName("sn")
private String sn;
/**
* 所需下发的消息模板ID
*/
@SerializedName("template_id")
private String templateId;
/**
* 点击模板卡片后的跳转页面仅限本小程序内的页面支持带参数,示例index?foo=bar该字段不填则模板无跳转.
*/
@SerializedName("page")
private String page;
/**
* 跳转小程序类型developer为开发版trial为体验版formal为正式版默认为正式版.
*/
@SerializedName("miniprogram_state")
private String miniprogramState;
/**
* 进入小程序查看的语言类型支持zh_CN(简体中文)en_US(英文)zh_HK(繁体中文)zh_TW(繁体中文)默认为zh_CN.
*/
@SerializedName("lang")
private String lang;
/**
* 模板内容格式形如 { "key1": { "value": any }, "key2": { "value": any } }.
*/
@SerializedName("data")
private Object data;
public String toJson() {
return WxMaGsonBuilder.create().toJson(this);
}
}

View File

@ -0,0 +1,41 @@
package cn.binarywang.wx.miniapp.bean.device;
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 小程序设备ticket请求参数
*
* @author <a href="https://github.com/leejuncheng">JCLee</a>
* @since 2021-12-16 17:13:28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class WxMaDeviceTicketRequest implements Serializable {
private static final long serialVersionUID = -2152114813101871295L;
/**
* 设备型号id通过注册设备获得必填
*
*/
@SerializedName("model_id")
private String modelId;
/**
* 设备唯序列号商分配度不能超过128字节字符只接受数字与写字必填
*/
@SerializedName("sn")
private String sn;
public String toJson() {
return WxMaGsonBuilder.create().toJson(this);
}
}

View File

@ -396,4 +396,18 @@ public class WxMaApiUrlConstants {
public interface Internet{
String GET_USER_ENCRYPT_KEY = "https://api.weixin.qq.com/wxa/business/getuserencryptkey";
}
/**
* 设备订阅消息
*/
public interface DeviceSubscribe {
/**
* 获取设备票据
*/
String GET_SN_TICKET_URL = "https://api.weixin.qq.com/wxa/getsnticket";
/**
* 发送设备订阅消息
*/
String SEND_DEVICE_SUBSCRIBE_MSG_URL = "https://api.weixin.qq.com/cgi-bin/message/device/subscribe/send";
}
}

View File

@ -0,0 +1,59 @@
package cn.binarywang.wx.miniapp.api.impl;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceSubscribeMessageRequest;
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceTicketRequest;
import cn.binarywang.wx.miniapp.test.ApiTestModule;
import com.google.common.collect.Lists;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
/**
* 小程序设备订阅消息相关 测试类
*
* @author <a href="https://github.com/leejuncheng">JCLee</a>
* @since 2021-12-16 17:13:35
*/
@Test
@Guice(modules = ApiTestModule.class)
public class WxMaDeviceSubscribeServiceImplTest {
@Inject
protected WxMaService wxService;
@Test
public void testGetSnTicket() throws WxErrorException{
WxMaDeviceTicketRequest wxMaDeviceTicketRequest = new WxMaDeviceTicketRequest();
wxMaDeviceTicketRequest.setModelId("11111");
wxMaDeviceTicketRequest.setSn("11111");
String snTicket = this.wxService.getDeviceSubscribeService().getSnTicket(wxMaDeviceTicketRequest);
System.out.println(snTicket);
}
@Test
public void sendDeviceSubscribeMsg() throws WxErrorException{
WxMaDeviceSubscribeMessageRequest wxMaDeviceSubscribeMessageRequest = new WxMaDeviceSubscribeMessageRequest();
wxMaDeviceSubscribeMessageRequest.setToOpenidList(Lists.newArrayList("1", "2"));
wxMaDeviceSubscribeMessageRequest.setPage("pages/index/index");
wxMaDeviceSubscribeMessageRequest.setTemplateId("11111111");
wxMaDeviceSubscribeMessageRequest.setSn("111111");
JsonObject data = GsonParser.parse("{\n" +
"\t\t\"thing2\": {\n" +
"\t\t\t\"value\": \"阳台\"\n" +
"\t\t},\n" +
"\t\t\"time1\": {\n" +
"\t\t\t\"value\": \"2021-09-30 13:32:44\"\n" +
"\t\t},\n" +
"\t\t\"thing3\": {\n" +
"\t\t\t\"value\": \"洗衣已完成\"\n" +
"\t\t}\n" +
"\t}");
wxMaDeviceSubscribeMessageRequest.setData(data);
this.wxService.getDeviceSubscribeService().sendDeviceSubscribeMsg(wxMaDeviceSubscribeMessageRequest);
}
}