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

This commit is contained in:
JCLee
2021-12-17 11:57:15 +08:00
committed by GitHub
parent c1d0b68d32
commit d70b907407
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));
}
}
}