Skip to content

Commit 74c99c4

Browse files
committed
🎨 #3808 【小程序】重构微信小程序用工关系文档地址,请求实体,增加单元测试
1 parent a23429c commit 74c99c4

File tree

6 files changed

+149
-31
lines changed

6 files changed

+149
-31
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,8 @@ WxMaApiResponse execute(
625625
/**
626626
* 获取用工关系服务对象。
627627
* <br>
628-
* 文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/laboruse/intro.html
628+
* 服务端api文档:https://developers.weixin.qq.com/miniprogram/dev/server/API/laboruse/
629+
* 整体流程文档: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/laboruse/intro.html
629630
*
630631
* @return 用工关系服务对象WxMaEmployeeRelationService
631632
*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*
1616
* @author <a href="https://github.com/binarywang">Binary Wang</a>
1717
* created on 2025-12-19
18+
* update on 2026-01-22 15:06:33
1819
*/
1920
@RequiredArgsConstructor
2021
public class WxMaEmployeeRelationServiceImpl implements WxMaEmployeeRelationService {

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/employee/WxMaSendEmployeeMsgRequest.java

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
/**
1313
* 小程序推送用工消息请求实体
1414
* <p>
15-
* 文档地址:<a href="https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/laboruse/api_sendemployeerelationmsg.html">推送用工消息</a>
15+
* 文档地址:<a href="https://developers.weixin.qq.com/miniprogram/dev/server/API/laboruse/api_sendemployeerelationmsg.html">推送用工消息</a>
1616
* </p>
1717
*
1818
* @author <a href="https://github.com/binarywang">Binary Wang</a>
1919
* created on 2025-12-19
20+
* update on 2026-01-22 15:13:28
2021
*/
2122
@Data
2223
@Builder(builderMethodName = "newBuilder")
@@ -27,33 +28,74 @@ public class WxMaSendEmployeeMsgRequest implements Serializable {
2728

2829
/**
2930
* <pre>
30-
* 字段名:用户openid
31+
* 字段名:模板id
3132
* 是否必填:是
32-
* 描述:需要接收消息的用户openid
33+
* 描述:需要在微信后台申请用工关系权限,通过后创建的模板审核通过后可以复制模板ID
3334
* </pre>
3435
*/
35-
@SerializedName("openid")
36-
private String openid;
36+
@SerializedName("template_id")
37+
private String templateId;
3738

3839
/**
3940
* <pre>
40-
* 字段名:企业id
41+
* 字段名:页面
4142
* 是否必填:是
42-
* 描述:企业id,小程序管理员在微信开放平台配置
43+
* 描述:用工消息通知跳转的page小程序链接(注意 小程序页面链接要是申请模板的小程序)
4344
* </pre>
4445
*/
45-
@SerializedName("corp_id")
46-
private String corpId;
46+
@SerializedName("page")
47+
private String page;
48+
49+
/**
50+
* <pre>
51+
* 字段名:被推送用户的openId
52+
* 是否必填:是
53+
* 描述:被推送用户的openId
54+
* </pre>
55+
*/
56+
@SerializedName("touser")
57+
private String touser;
4758

4859
/**
4960
* <pre>
5061
* 字段名:消息内容
5162
* 是否必填:是
52-
* 描述:推送的消息内容,文本格式,最长不超过200个字符
63+
* 描述:需要根据小程序后台审核通过的模板id的字段类型序列化json传递
64+
*
65+
* </pre>
66+
*
67+
* 参考组装代码
68+
* <pre>
69+
* <code>
70+
* // 使用 HashMap 构建数据结构
71+
* Map<String, Object> data1 = new HashMap<>();
72+
* // 内层字段
73+
* Map<String, String> thing1 = new HashMap<>();
74+
* Map<String, String> thing2 = new HashMap<>();
75+
* Map<String, String> time1 = new HashMap<>();
76+
* Map<String, String> character_string1 = new HashMap<>();
77+
* Map<String, String> time2 = new HashMap<>();
78+
* thing1.put("value", "高和蓝枫箱体测试");
79+
* thing2.put("value", "门口全英测试");
80+
* time1.put("value", "2026年11月23日 19:19");
81+
* character_string1.put("value", "50kg");
82+
* time2.put("value", "2026年11月23日 19:19");
83+
*
84+
* // 模板消息变量,有顺序要求
85+
* Map<String, Object> dataContent = new LinkedHashMap<>();
86+
* dataContent.put("thing1", thing1);
87+
* dataContent.put("thing2", thing2);
88+
* dataContent.put("time1", time1);
89+
* dataContent.put("character_string1", character_string1);
90+
* dataContent.put("time2", time2);
91+
*
92+
* data1.put("data", dataContent);
93+
* </code>
5394
* </pre>
5495
*/
55-
@SerializedName("msg")
56-
private String msg;
96+
97+
@SerializedName("data")
98+
private String data;
5799

58100
public String toJson() {
59101
return WxMaGsonBuilder.create().toJson(this);

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/employee/WxMaUnbindEmployeeRequest.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
import lombok.NoArgsConstructor;
99

1010
import java.io.Serializable;
11+
import java.util.List;
1112

1213
/**
1314
* 小程序解绑用工关系请求实体
1415
* <p>
15-
* 文档地址:<a href="https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/laboruse/api_unbinduserb2cauthinfo.html">解绑用工关系</a>
16+
* 文档地址:<a href="https://developers.weixin.qq.com/miniprogram/dev/server/API/laboruse/api_unbinduserb2cauthinfo.html">解绑用工关系</a>
1617
* </p>
1718
*
1819
* @author <a href="https://github.com/binarywang">Binary Wang</a>
1920
* created on 2025-12-19
21+
* update on 2026-01-22 15:14:09
2022
*/
2123
@Data
2224
@Builder(builderMethodName = "newBuilder")
@@ -27,23 +29,13 @@ public class WxMaUnbindEmployeeRequest implements Serializable {
2729

2830
/**
2931
* <pre>
30-
* 字段名:用户openid
32+
* 字段名:用户openid列表
3133
* 是否必填:是
32-
* 描述:需要解绑的用户openid
34+
* 描述:需要解绑的用户openid列表
3335
* </pre>
3436
*/
35-
@SerializedName("openid")
36-
private String openid;
37-
38-
/**
39-
* <pre>
40-
* 字段名:企业id
41-
* 是否必填:是
42-
* 描述:企业id,小程序管理员在微信开放平台配置
43-
* </pre>
44-
*/
45-
@SerializedName("corp_id")
46-
private String corpId;
37+
@SerializedName("openid_list")
38+
private List<String> openidList;
4739

4840
public String toJson() {
4941
return WxMaGsonBuilder.create().toJson(this);

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,11 +1006,16 @@ public interface Complaint {
10061006
String UPLOAD_RESPONSE_IMAGE_URL = "https://api.weixin.qq.com/cgi-bin/miniapp/complaint/upload";
10071007
}
10081008

1009-
/** 用工关系 */
1009+
/**
1010+
* 小程序用工关系接口
1011+
* <pre>
1012+
* 文档地址: https://developers.weixin.qq.com/miniprogram/dev/server/API/laboruse/
1013+
* </pre>
1014+
*/
10101015
public interface Employee {
10111016
/** 解绑用工关系 */
1012-
String UNBIND_EMPLOYEE_URL = "https://api.weixin.qq.com/wxa/unbinduserb2cauthinfo";
1017+
String UNBIND_EMPLOYEE_URL = "https://api.weixin.qq.com/wxa/business/unbinduserb2cauthinfo";
10131018
/** 推送用工消息 */
1014-
String SEND_EMPLOYEE_MSG_URL = "https://api.weixin.qq.com/wxa/sendemployeerelationmsg";
1019+
String SEND_EMPLOYEE_MSG_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/employeerelationmsg/send";
10151020
}
10161021
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaService;
4+
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceSubscribeMessageRequest;
5+
import cn.binarywang.wx.miniapp.bean.device.WxMaDeviceTicketRequest;
6+
import cn.binarywang.wx.miniapp.bean.employee.WxMaSendEmployeeMsgRequest;
7+
import cn.binarywang.wx.miniapp.bean.employee.WxMaUnbindEmployeeRequest;
8+
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
9+
import cn.binarywang.wx.miniapp.test.ApiTestModule;
10+
import com.google.gson.JsonObject;
11+
import com.google.inject.Inject;
12+
import lombok.extern.slf4j.Slf4j;
13+
import me.chanjar.weixin.common.error.WxErrorException;
14+
import me.chanjar.weixin.common.util.json.GsonParser;
15+
import org.jetbrains.annotations.NotNull;
16+
import org.testng.annotations.Guice;
17+
import org.testng.annotations.Test;
18+
19+
import java.util.*;
20+
21+
@Slf4j
22+
@Test
23+
@Guice(modules = ApiTestModule.class)
24+
public class WxMaEmployeeRelationServiceImplTest {
25+
26+
@Inject
27+
protected WxMaService wxService;
28+
29+
@Test
30+
public void testSendEmployeeMsg() throws WxErrorException {
31+
WxMaSendEmployeeMsgRequest wxMaSendEmployeeMsgRequest = new WxMaSendEmployeeMsgRequest();
32+
wxMaSendEmployeeMsgRequest.setPage("/pages/index/index");
33+
wxMaSendEmployeeMsgRequest.setTouser("o0uBr12b1zdgCk1qDoBivmSYb9GA");
34+
wxMaSendEmployeeMsgRequest.setTemplateId("nmO-O4V33TOREVLAlumwPCsHssqkt7mea_cyWNE-IFmZqT9jh_LsERhzDOsOqa-3");
35+
36+
// 使用 HashMap 构建数据结构
37+
Map<String, Object> data1 = new HashMap<>();
38+
// 内层字段
39+
Map<String, Object> dataContent = getStringObjectMap();
40+
41+
data1.put("data", dataContent);
42+
wxMaSendEmployeeMsgRequest.setData(WxMaGsonBuilder.create().toJson(data1));
43+
this.wxService.getEmployeeRelationService().sendEmployeeMsg(wxMaSendEmployeeMsgRequest);
44+
}
45+
46+
@NotNull
47+
private static Map<String, Object> getStringObjectMap() {
48+
Map<String, String> thing1 = new HashMap<>();
49+
Map<String, String> thing2 = new HashMap<>();
50+
Map<String, String> time1 = new HashMap<>();
51+
Map<String, String> character_string1 = new HashMap<>();
52+
Map<String, String> time2 = new HashMap<>();
53+
thing1.put("value", "高和蓝枫箱体测试");
54+
thing2.put("value", "门口全英测试");
55+
time1.put("value", "2026年11月23日 19:19");
56+
character_string1.put("value", "50kg");
57+
time2.put("value", "2026年11月23日 19:19");
58+
59+
// 模板消息变量,有顺序要求
60+
Map<String, Object> dataContent = new LinkedHashMap<>();
61+
dataContent.put("thing1", thing1);
62+
dataContent.put("thing2", thing2);
63+
dataContent.put("time1", time1);
64+
dataContent.put("character_string1", character_string1);
65+
dataContent.put("time2", time2);
66+
return dataContent;
67+
}
68+
69+
70+
@Test
71+
public void testUnbinduserb2cauthinfo() throws WxErrorException {
72+
WxMaUnbindEmployeeRequest wxMaUnbindEmployeeRequest = new WxMaUnbindEmployeeRequest();
73+
wxMaUnbindEmployeeRequest.setOpenidList(List.of("o0uBr12b1zdgCk1qDoBivmSYb9GA"));
74+
this.wxService.getEmployeeRelationService().unbindEmployee(wxMaUnbindEmployeeRequest);
75+
}
76+
77+
}

0 commit comments

Comments
 (0)