Skip to content

Commit 08a6540

Browse files
Copilotbinarywang
andauthored
feat(miniapp): 新增微信小程序服务卡片消息接口
实现以下三个服务卡片消息 API: - 激活与更新服务卡片(setUserNotify) - 更新服务卡片扩展信息(setUserNotifyExt) - 查询服务卡片状态(getUserNotify) Agent-Logs-Url: https://github.com/binarywang/WxJava/sessions/63aeccb8-2684-4ea7-8b6b-9db7d362044a Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
1 parent 2392cee commit 08a6540

8 files changed

Lines changed: 427 additions & 0 deletions

File tree

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

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

3+
import cn.binarywang.wx.miniapp.bean.WxMaGetUserNotifyRequest;
4+
import cn.binarywang.wx.miniapp.bean.WxMaGetUserNotifyResult;
5+
import cn.binarywang.wx.miniapp.bean.WxMaServiceNotifyExtRequest;
6+
import cn.binarywang.wx.miniapp.bean.WxMaServiceNotifyRequest;
37
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
48
import me.chanjar.weixin.common.bean.subscribemsg.CategoryData;
59
import me.chanjar.weixin.common.bean.subscribemsg.PubTemplateKeyword;
@@ -113,4 +117,44 @@ public interface WxMaSubscribeService {
113117
*/
114118
void sendSubscribeMsg(WxMaSubscribeMessage subscribeMessage) throws WxErrorException;
115119

120+
/**
121+
* <pre>
122+
* 激活与更新服务卡片
123+
*
124+
* 详情请见: <a href="https://developers.weixin.qq.com/miniprogram/dev/server/API/mp-message-management/subscribe-message/api_setusernotify.html">激活与更新服务卡片</a>
125+
* 接口url格式: POST https://api.weixin.qq.com/wxa/setusernotify?access_token=ACCESS_TOKEN
126+
* </pre>
127+
*
128+
* @param request 请求参数
129+
* @throws WxErrorException .
130+
*/
131+
void setUserNotify(WxMaServiceNotifyRequest request) throws WxErrorException;
132+
133+
/**
134+
* <pre>
135+
* 更新服务卡片扩展信息
136+
*
137+
* 详情请见: <a href="https://developers.weixin.qq.com/miniprogram/dev/server/API/mp-message-management/subscribe-message/api_setusernotifyext.html">更新服务卡片扩展信息</a>
138+
* 接口url格式: POST https://api.weixin.qq.com/wxa/setusernotifyext?access_token=ACCESS_TOKEN
139+
* </pre>
140+
*
141+
* @param request 请求参数
142+
* @throws WxErrorException .
143+
*/
144+
void setUserNotifyExt(WxMaServiceNotifyExtRequest request) throws WxErrorException;
145+
146+
/**
147+
* <pre>
148+
* 查询服务卡片状态
149+
*
150+
* 详情请见: <a href="https://developers.weixin.qq.com/miniprogram/dev/server/API/mp-message-management/subscribe-message/api_getusernotify.html">查询服务卡片状态</a>
151+
* 接口url格式: POST https://api.weixin.qq.com/wxa/getusernotify?access_token=ACCESS_TOKEN
152+
* </pre>
153+
*
154+
* @param request 请求参数
155+
* @return 服务卡片状态
156+
* @throws WxErrorException .
157+
*/
158+
WxMaGetUserNotifyResult getUserNotify(WxMaGetUserNotifyRequest request) throws WxErrorException;
159+
116160
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import cn.binarywang.wx.miniapp.api.WxMaService;
44
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
5+
import cn.binarywang.wx.miniapp.bean.WxMaGetUserNotifyRequest;
6+
import cn.binarywang.wx.miniapp.bean.WxMaGetUserNotifyResult;
7+
import cn.binarywang.wx.miniapp.bean.WxMaServiceNotifyExtRequest;
8+
import cn.binarywang.wx.miniapp.bean.WxMaServiceNotifyRequest;
59
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
610
import me.chanjar.weixin.common.api.WxConsts;
711
import me.chanjar.weixin.common.bean.subscribemsg.CategoryData;
@@ -89,4 +93,32 @@ public void sendSubscribeMsg(WxMaSubscribeMessage subscribeMessage) throws WxErr
8993
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
9094
}
9195
}
96+
97+
@Override
98+
public void setUserNotify(WxMaServiceNotifyRequest request) throws WxErrorException {
99+
String responseContent = this.service.post(SERVICE_NOTIFY_SET_URL, request.toJson());
100+
JsonObject jsonObject = GsonParser.parse(responseContent);
101+
if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) {
102+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
103+
}
104+
}
105+
106+
@Override
107+
public void setUserNotifyExt(WxMaServiceNotifyExtRequest request) throws WxErrorException {
108+
String responseContent = this.service.post(SERVICE_NOTIFY_SET_EXT_URL, request.toJson());
109+
JsonObject jsonObject = GsonParser.parse(responseContent);
110+
if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) {
111+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
112+
}
113+
}
114+
115+
@Override
116+
public WxMaGetUserNotifyResult getUserNotify(WxMaGetUserNotifyRequest request) throws WxErrorException {
117+
String responseContent = this.service.post(SERVICE_NOTIFY_GET_URL, request.toJson());
118+
JsonObject jsonObject = GsonParser.parse(responseContent);
119+
if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) {
120+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
121+
}
122+
return WxMaGsonBuilder.create().fromJson(responseContent, WxMaGetUserNotifyResult.class);
123+
}
92124
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package cn.binarywang.wx.miniapp.bean;
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+
* 查询服务卡片状态请求.
14+
*
15+
* <p>接口文档:
16+
* <a href="https://developers.weixin.qq.com/miniprogram/dev/server/API/mp-message-management/subscribe-message/api_getusernotify.html">
17+
* 查询服务卡片状态</a>
18+
*
19+
* @author GitHub Copilot
20+
*/
21+
@Data
22+
@Builder
23+
@NoArgsConstructor
24+
@AllArgsConstructor
25+
public class WxMaGetUserNotifyRequest implements Serializable {
26+
private static final long serialVersionUID = 1L;
27+
28+
/**
29+
* 用户身份标识符.
30+
* <pre>
31+
* 参数:openid
32+
* 是否必填:是
33+
* </pre>
34+
*/
35+
@SerializedName("openid")
36+
private String openid;
37+
38+
/**
39+
* 动态更新令牌.
40+
* <pre>
41+
* 参数:notify_code
42+
* 是否必填:是
43+
* </pre>
44+
*/
45+
@SerializedName("notify_code")
46+
private String notifyCode;
47+
48+
/**
49+
* 卡片ID.
50+
* <pre>
51+
* 参数:notify_type
52+
* 是否必填:是
53+
* </pre>
54+
*/
55+
@SerializedName("notify_type")
56+
private Integer notifyType;
57+
58+
/**
59+
* 转为 JSON 字符串.
60+
*
61+
* @return JSON 字符串
62+
*/
63+
public String toJson() {
64+
return WxMaGsonBuilder.create().toJson(this);
65+
}
66+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package cn.binarywang.wx.miniapp.bean;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import lombok.EqualsAndHashCode;
6+
7+
import java.io.Serializable;
8+
9+
/**
10+
* 查询服务卡片状态响应.
11+
*
12+
* <p>接口文档:
13+
* <a href="https://developers.weixin.qq.com/miniprogram/dev/server/API/mp-message-management/subscribe-message/api_getusernotify.html">
14+
* 查询服务卡片状态</a>
15+
*
16+
* @author GitHub Copilot
17+
*/
18+
@Data
19+
@EqualsAndHashCode(callSuper = true)
20+
public class WxMaGetUserNotifyResult extends WxMaBaseResponse {
21+
private static final long serialVersionUID = 1L;
22+
23+
/**
24+
* 卡片状态信息.
25+
*/
26+
@SerializedName("notify_info")
27+
private NotifyInfo notifyInfo;
28+
29+
/**
30+
* 卡片状态详情.
31+
*/
32+
@Data
33+
public static class NotifyInfo implements Serializable {
34+
private static final long serialVersionUID = 1L;
35+
36+
/**
37+
* 卡片ID.
38+
*/
39+
@SerializedName("notify_type")
40+
private Integer notifyType;
41+
42+
/**
43+
* 上次有效推送的卡片状态与状态相关字段,没推送过为空字符串.
44+
*/
45+
@SerializedName("content_json")
46+
private String contentJson;
47+
48+
/**
49+
* code 状态:0 正常;1 有风险;2 异常;10 用户拒收本次code.
50+
*/
51+
@SerializedName("code_state")
52+
private Integer codeState;
53+
54+
/**
55+
* code 过期时间,秒级时间戳.
56+
*/
57+
@SerializedName("code_expire_time")
58+
private Long codeExpireTime;
59+
}
60+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package cn.binarywang.wx.miniapp.bean;
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+
* 更新服务卡片扩展信息请求.
14+
*
15+
* <p>接口文档:
16+
* <a href="https://developers.weixin.qq.com/miniprogram/dev/server/API/mp-message-management/subscribe-message/api_setusernotifyext.html">
17+
* 更新服务卡片扩展信息</a>
18+
*
19+
* @author GitHub Copilot
20+
*/
21+
@Data
22+
@Builder
23+
@NoArgsConstructor
24+
@AllArgsConstructor
25+
public class WxMaServiceNotifyExtRequest implements Serializable {
26+
private static final long serialVersionUID = 1L;
27+
28+
/**
29+
* 用户身份标识符.
30+
* <pre>
31+
* 参数:openid
32+
* 是否必填:是
33+
* 描述:用户身份标识符。
34+
* 当使用微信支付订单号作为 code 时,需要与实际支付用户一致;
35+
* 当通过前端获取 code 时,需要与点击 button 的用户一致。
36+
* </pre>
37+
*/
38+
@SerializedName("openid")
39+
private String openid;
40+
41+
/**
42+
* 卡片ID.
43+
* <pre>
44+
* 参数:notify_type
45+
* 是否必填:是
46+
* 描述:卡片ID。
47+
* </pre>
48+
*/
49+
@SerializedName("notify_type")
50+
private Integer notifyType;
51+
52+
/**
53+
* 动态更新令牌.
54+
* <pre>
55+
* 参数:notify_code
56+
* 是否必填:是
57+
* 描述:动态更新令牌。
58+
* </pre>
59+
*/
60+
@SerializedName("notify_code")
61+
private String notifyCode;
62+
63+
/**
64+
* 扩展信息.
65+
* <pre>
66+
* 参数:ext_json
67+
* 是否必填:是
68+
* 描述:扩展信息,不同卡片的定义不同。
69+
* </pre>
70+
*/
71+
@SerializedName("ext_json")
72+
private String extJson;
73+
74+
/**
75+
* 转为 JSON 字符串.
76+
*
77+
* @return JSON 字符串
78+
*/
79+
public String toJson() {
80+
return WxMaGsonBuilder.create().toJson(this);
81+
}
82+
}

0 commit comments

Comments
 (0)