Skip to content

Commit c2d0cf8

Browse files
authored
🆕 #3818 【小程序】 新增设备组相关的 API 接口
1 parent 8e760a9 commit c2d0cf8

File tree

8 files changed

+329
-9
lines changed

8 files changed

+329
-9
lines changed

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaDeviceSubscribeService.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package cn.binarywang.wx.miniapp.api;
22

3-
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceSubscribeMessageRequest;
4-
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceTicketRequest;
3+
import cn.binarywang.wx.miniapp.bean.device.*;
54
import me.chanjar.weixin.common.error.WxErrorException;
65

6+
import java.util.List;
7+
78
/**
89
* 小程序设备订阅消息相关 API
910
* 文档:
@@ -21,6 +22,7 @@ public interface WxMaDeviceSubscribeService {
2122
* 注意:
2223
* 设备ticket有效时间为5分钟
2324
* </pre>
25+
*
2426
* @param deviceTicketRequest
2527
* @return
2628
* @throws WxErrorException
@@ -37,4 +39,53 @@ public interface WxMaDeviceSubscribeService {
3739
*/
3840
void sendDeviceSubscribeMsg(WxMaDeviceSubscribeMessageRequest deviceSubscribeMessageRequest) throws WxErrorException;
3941

42+
/**
43+
* <pre>
44+
* 创建设备组
45+
* 详情请见:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/hardware-device/createIotGroupId.html
46+
* </pre>
47+
*
48+
* @param createIotGroupIdRequest 请求参数
49+
* @return 设备组的唯一标识
50+
* @throws WxErrorException
51+
*/
52+
String createIotGroupId(WxMaCreateIotGroupIdRequest createIotGroupIdRequest) throws WxErrorException;
53+
54+
/**
55+
* <pre>
56+
* 查询设备组信息
57+
* 详情请见:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/hardware-device/getIotGroupInfo.html
58+
* </pre>
59+
*
60+
* @param getIotGroupInfoRequest 请求参数
61+
* @return 设备组信息
62+
* @throws WxErrorException
63+
*/
64+
WxMaIotGroupDeviceInfoResponse getIotGroupInfo(WxMaGetIotGroupInfoRequest getIotGroupInfoRequest) throws WxErrorException;
65+
66+
/**
67+
* <pre>
68+
* 设备组添加设备
69+
* 一个设备组最多添加 50 个设备。 一个设备同一时间只能被添加到一个设备组中。
70+
* 详情请见:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/hardware-device/addIotGroupDevice.html
71+
* </pre>
72+
*
73+
* @param request 请求参数
74+
* @return 成功添加的设备信息
75+
* @throws WxErrorException
76+
*/
77+
List<WxMaDeviceTicketRequest> addIotGroupDevice(WxMaIotGroupDeviceRequest request) throws WxErrorException;
78+
79+
/**
80+
* <pre>
81+
* 设备组删除设备
82+
* 详情请见:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/hardware-device/removeIotGroupDevice.html
83+
* </pre>
84+
*
85+
* @param request 请求参数
86+
* @return 成功删除的设备信息
87+
* @throws WxErrorException
88+
*/
89+
List<WxMaDeviceTicketRequest> removeIotGroupDevice(WxMaIotGroupDeviceRequest request) throws WxErrorException;
90+
4091
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaDeviceSubscribeServiceImpl.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@
22

33
import cn.binarywang.wx.miniapp.api.WxMaDeviceSubscribeService;
44
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;
77
import com.google.gson.JsonObject;
8+
import com.google.gson.reflect.TypeToken;
89
import lombok.RequiredArgsConstructor;
910
import me.chanjar.weixin.common.api.WxConsts;
1011
import me.chanjar.weixin.common.enums.WxType;
1112
import me.chanjar.weixin.common.error.WxError;
1213
import me.chanjar.weixin.common.error.WxErrorException;
1314
import me.chanjar.weixin.common.util.json.GsonParser;
15+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
16+
17+
import java.util.List;
1418

1519
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.DeviceSubscribe.GET_SN_TICKET_URL;
1620
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;
1725

1826
/**
1927
* 小程序设备订阅消息相关 API
@@ -47,4 +55,46 @@ public void sendDeviceSubscribeMsg(WxMaDeviceSubscribeMessageRequest deviceSubsc
4755
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
4856
}
4957
}
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+
}
50100
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cn.binarywang.wx.miniapp.bean.device;
2+
3+
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
4+
import com.google.gson.annotations.SerializedName;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
10+
import java.io.Serializable;
11+
12+
/**
13+
* @Author: yanglegetuo
14+
* @Date: 2025/12/22 8:51
15+
* @Description: 创建设备组 请求参数
16+
*/
17+
@Data
18+
@Builder
19+
@NoArgsConstructor
20+
@AllArgsConstructor
21+
public class WxMaCreateIotGroupIdRequest implements Serializable {
22+
private static final long serialVersionUID = 1827809470414413390L;
23+
/**
24+
* 设备型号id。通过注册设备获得(必填)
25+
*/
26+
@SerializedName("model_id")
27+
private String modelId;
28+
/**
29+
* 设备组的名称(创建时决定,无法修改)
30+
*/
31+
@SerializedName("group_name")
32+
private String groupName;
33+
34+
35+
public String toJson() {
36+
return WxMaGsonBuilder.create().toJson(this);
37+
}
38+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.binarywang.wx.miniapp.bean.device;
2+
3+
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
4+
import com.google.gson.annotations.SerializedName;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
10+
import java.io.Serializable;
11+
12+
/**
13+
* @Author: yanglegetuo
14+
* @Date: 2025/12/22 8:51
15+
* @Description: 查询设备组信息 请求参数
16+
*/
17+
@Data
18+
@Builder
19+
@NoArgsConstructor
20+
@AllArgsConstructor
21+
public class WxMaGetIotGroupInfoRequest implements Serializable {
22+
23+
private static final long serialVersionUID = 4913375114243384968L;
24+
/**
25+
* 设备组的唯一标识(必填)
26+
*/
27+
@SerializedName("group_id")
28+
private String groupId;
29+
30+
public String toJson() {
31+
return WxMaGsonBuilder.create().toJson(this);
32+
}
33+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cn.binarywang.wx.miniapp.bean.device;
2+
3+
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
4+
import com.google.gson.annotations.SerializedName;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
10+
import java.io.Serializable;
11+
import java.util.List;
12+
13+
/**
14+
* @Author: yanglegetuo
15+
* @Date: 2025/12/22 8:51
16+
* @Description: 设备组信息 响应参数
17+
*/
18+
@Data
19+
@Builder
20+
@NoArgsConstructor
21+
@AllArgsConstructor
22+
public class WxMaIotGroupDeviceInfoResponse implements Serializable {
23+
24+
private static final long serialVersionUID = 6615660801230308048L;
25+
/**
26+
* 设备组名称
27+
*/
28+
@SerializedName("group_name")
29+
private String groupName;
30+
/**
31+
* 设备列表
32+
*/
33+
@SerializedName("device_list")
34+
private List<WxMaDeviceTicketRequest> deviceList;
35+
36+
/**
37+
* 设备型号id。通过注册设备获得(必填)
38+
*/
39+
@SerializedName("model_id")
40+
private String modelId;
41+
/**
42+
* 设备类型
43+
*/
44+
@SerializedName("model_type")
45+
private String modelType;
46+
47+
48+
public String toJson() {
49+
return WxMaGsonBuilder.create().toJson(this);
50+
}
51+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package cn.binarywang.wx.miniapp.bean.device;
2+
3+
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
4+
import com.google.gson.annotations.SerializedName;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
10+
import java.io.Serializable;
11+
import java.util.List;
12+
13+
/**
14+
* @Author: yanglegetuo
15+
* @Date: 2025/12/22 8:51
16+
* @Description: 设备组 添加/移除 设备 请求参数
17+
*/
18+
@Data
19+
@Builder
20+
@NoArgsConstructor
21+
@AllArgsConstructor
22+
public class WxMaIotGroupDeviceRequest implements Serializable {
23+
private static final long serialVersionUID = -5648997758678588138L;
24+
25+
/**
26+
* 设备组的唯一标识(必填)
27+
*/
28+
@SerializedName("group_id")
29+
private String groupId;
30+
/**
31+
* 设备列表
32+
*/
33+
@SerializedName("device_list")
34+
private List<WxMaDeviceTicketRequest> deviceList;
35+
/**
36+
* 是否强制更新设备列表,等于 true 时将已存在其它设备组中的设备移除并添加到当前设备组,慎用。
37+
*/
38+
@SerializedName("force_add")
39+
private Boolean forceAdd;
40+
41+
public String toJson() {
42+
return WxMaGsonBuilder.create().toJson(this);
43+
}
44+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/constant/WxMaApiUrlConstants.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,14 @@ public interface DeviceSubscribe {
569569
/** 发送设备订阅消息 */
570570
String SEND_DEVICE_SUBSCRIBE_MSG_URL =
571571
"https://api.weixin.qq.com/cgi-bin/message/device/subscribe/send";
572+
/** 创建设备组 */
573+
String CREATE_IOT_GROUP_ID_URL = "https://api.weixin.qq.com/wxa/business/group/createid";
574+
/** 设备组添加设备 */
575+
String ADD_IOT_GROUP_DEVICE_URL = "https://api.weixin.qq.com/wxa/business/group/adddevice";
576+
/** 设备组删除设备 */
577+
String REMOVE_IOT_GROUP_DEVICE_URL = "https://api.weixin.qq.com/wxa/business/group/removedevice";
578+
/** 查询设备组信息 */
579+
String GET_IOT_GROUP_INFO_URL = "https://api.weixin.qq.com/wxa/business/group/getinfo";
572580
}
573581

574582
/**

0 commit comments

Comments
 (0)