Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,11 @@ public interface WxMaService extends WxService {
* @return
*/
WxMaReimburseInvoiceService getReimburseInvoiceService();

/**
* 返回设备订阅消息相关接口服务对象
*
* @return WxMaDeviceSubscribeService plugin service
*/
WxMaDeviceSubscribeService getDeviceSubscribeService();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -573,4 +574,7 @@ public WxMaLinkService getLinkService() {
public WxMaReimburseInvoiceService getReimburseInvoiceService() {
return this.reimburseInvoiceService;
}

@Override
public WxMaDeviceSubscribeService getDeviceSubscribeService(){ return this.deviceSubscribeService; }
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,18 @@ public interface Invoice{
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";
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}