|
2 | 2 |
|
3 | 3 | import cn.binarywang.wx.miniapp.api.WxMaDeviceSubscribeService; |
4 | 4 | import cn.binarywang.wx.miniapp.api.WxMaService; |
5 | | -import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceSubscribeMessageRequest; |
6 | | -import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceTicketRequest; |
| 5 | +import cn.binarywang.wx.miniapp.bean.device.*; |
| 6 | +import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder; |
7 | 7 | import com.google.gson.JsonObject; |
| 8 | +import com.google.gson.reflect.TypeToken; |
8 | 9 | import lombok.RequiredArgsConstructor; |
9 | 10 | import me.chanjar.weixin.common.api.WxConsts; |
10 | 11 | import me.chanjar.weixin.common.enums.WxType; |
11 | 12 | import me.chanjar.weixin.common.error.WxError; |
12 | 13 | import me.chanjar.weixin.common.error.WxErrorException; |
13 | 14 | import me.chanjar.weixin.common.util.json.GsonParser; |
| 15 | +import me.chanjar.weixin.common.util.json.WxGsonBuilder; |
| 16 | + |
| 17 | +import java.util.List; |
14 | 18 |
|
15 | 19 | import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.GET_SN_TICKET_URL; |
16 | 20 | import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.SEND_DEVICE_SUBSCRIBE_MSG_URL; |
| 21 | +import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.CREATE_IOT_GROUP_ID_URL; |
| 22 | +import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.GET_IOT_GROUP_INFO_URL; |
| 23 | +import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.ADD_IOT_GROUP_DEVICE_URL; |
| 24 | +import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.REMOVE_IOT_GROUP_DEVICE_URL; |
17 | 25 |
|
18 | 26 | /** |
19 | 27 | * 小程序设备订阅消息相关 API |
@@ -47,4 +55,46 @@ public void sendDeviceSubscribeMsg(WxMaDeviceSubscribeMessageRequest deviceSubsc |
47 | 55 | throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); |
48 | 56 | } |
49 | 57 | } |
| 58 | + |
| 59 | + @Override |
| 60 | + public String createIotGroupId(WxMaCreateIotGroupIdRequest createIotGroupIdRequest) throws WxErrorException { |
| 61 | + String responseContent = this.service.post(CREATE_IOT_GROUP_ID_URL, createIotGroupIdRequest.toJson()); |
| 62 | + JsonObject jsonObject = GsonParser.parse(responseContent); |
| 63 | + if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) { |
| 64 | + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); |
| 65 | + } |
| 66 | + return jsonObject.get("group_id").getAsString(); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public WxMaIotGroupDeviceInfoResponse getIotGroupInfo(WxMaGetIotGroupInfoRequest getIotGroupInfoRequest) throws WxErrorException { |
| 71 | + String responseContent = this.service.post(GET_IOT_GROUP_INFO_URL, getIotGroupInfoRequest.toJson()); |
| 72 | + JsonObject jsonObject = GsonParser.parse(responseContent); |
| 73 | + if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) { |
| 74 | + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); |
| 75 | + } |
| 76 | + return WxGsonBuilder.create().fromJson(responseContent, WxMaIotGroupDeviceInfoResponse.class); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public List<WxMaDeviceTicketRequest> addIotGroupDevice(WxMaIotGroupDeviceRequest request) throws WxErrorException { |
| 81 | + String responseContent = this.service.post(ADD_IOT_GROUP_DEVICE_URL, request.toJson()); |
| 82 | + JsonObject jsonObject = GsonParser.parse(responseContent); |
| 83 | + if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) { |
| 84 | + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); |
| 85 | + } |
| 86 | + return WxMaGsonBuilder.create().fromJson(jsonObject.getAsJsonArray("device_list"), new TypeToken<List<WxMaDeviceTicketRequest>>() { |
| 87 | + }.getType()); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public List<WxMaDeviceTicketRequest> removeIotGroupDevice(WxMaIotGroupDeviceRequest request) throws WxErrorException { |
| 92 | + String responseContent = this.service.post(REMOVE_IOT_GROUP_DEVICE_URL, request.toJson()); |
| 93 | + JsonObject jsonObject = GsonParser.parse(responseContent); |
| 94 | + if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) { |
| 95 | + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); |
| 96 | + } |
| 97 | + return WxMaGsonBuilder.create().fromJson(jsonObject.getAsJsonArray("device_list"), new TypeToken<List<WxMaDeviceTicketRequest>>() { |
| 98 | + }.getType()); |
| 99 | + } |
50 | 100 | } |
0 commit comments