Skip to content

Commit 8428270

Browse files
youflintsysong
and
sysong
authored
🆕 #1793 企业微信添加应用管理的设置工作台自定义展示模块
Co-authored-by: sysong <sysong@chutianyun.gov.cn>
1 parent cd0c63f commit 8428270

File tree

10 files changed

+421
-0
lines changed

10 files changed

+421
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package me.chanjar.weixin.cp.api;
2+
3+
import me.chanjar.weixin.common.error.WxErrorException;
4+
import me.chanjar.weixin.cp.bean.WxCpAgentWorkBench;
5+
6+
/**
7+
* @author songshiyu
8+
* @date : create in 16:16 2020/9/27
9+
* @description: 工作台自定义展示:https://work.weixin.qq.com/api/doc/90000/90135/92535
10+
*/
11+
public interface WxCpAgentWorkBenchService {
12+
13+
void setWorkBenchTemplate(WxCpAgentWorkBench wxCpAgentWorkBench) throws WxErrorException;
14+
15+
String getWorkBenchTemplate(Long agentid) throws WxErrorException;
16+
17+
void setWorkBenchData(WxCpAgentWorkBench wxCpAgentWorkBench) throws WxErrorException;
18+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,12 @@ public interface WxCpService {
403403
* @return 群机器人消息推送服务 group robot service
404404
*/
405405
WxCpGroupRobotService getGroupRobotService();
406+
/*
407+
* 获取工作台服务
408+
*
409+
* @return the workbench service
410+
* */
411+
WxCpAgentWorkBenchService getWorkBenchService();
406412

407413
/**
408414
* http请求对象

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
5454
private WxCpGroupRobotService groupRobotService = new WxCpGroupRobotServiceImpl(this);
5555
private WxCpMessageService messageService = new WxCpMessageServiceImpl(this);
5656
private WxCpOaCalendarService oaCalendarService = new WxCpOaCalendarServiceImpl(this);
57+
private WxCpAgentWorkBenchService workBenchService = new WxCpAgentWorkBenchServiceImpl(this);
5758

5859
/**
5960
* 全局的是否正在刷新access token的锁.
@@ -437,6 +438,11 @@ public WxCpGroupRobotService getGroupRobotService() {
437438
return groupRobotService;
438439
}
439440

441+
@Override
442+
public WxCpAgentWorkBenchService getWorkBenchService() {
443+
return workBenchService;
444+
}
445+
440446
@Override
441447
public WxCpTaskCardService getTaskCardService() {
442448
return taskCardService;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package me.chanjar.weixin.cp.api.impl;
2+
3+
import com.google.gson.JsonObject;
4+
import lombok.RequiredArgsConstructor;
5+
import me.chanjar.weixin.common.error.WxErrorException;
6+
import me.chanjar.weixin.cp.api.WxCpAgentWorkBenchService;
7+
import me.chanjar.weixin.cp.api.WxCpService;
8+
import me.chanjar.weixin.cp.bean.WxCpAgentWorkBench;
9+
10+
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.WorkBench.WORKBENCH_DATA_SET;
11+
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.WorkBench.WORKBENCH_TEMPLATE_GET;
12+
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.WorkBench.WORKBENCH_TEMPLATE_SET;
13+
14+
/**
15+
* @author songshiyu
16+
* @date : create in 11:24 2020/9/28
17+
* @description: 工作台自定义展示实现
18+
*/
19+
@RequiredArgsConstructor
20+
public class WxCpAgentWorkBenchServiceImpl implements WxCpAgentWorkBenchService {
21+
private final WxCpService mainService;
22+
23+
@Override
24+
public void setWorkBenchTemplate(WxCpAgentWorkBench wxCpAgentWorkBench) throws WxErrorException {
25+
final String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WORKBENCH_TEMPLATE_SET));
26+
this.mainService.post(url, wxCpAgentWorkBench.toTemplateString());
27+
}
28+
29+
@Override
30+
public String getWorkBenchTemplate(Long agentId) throws WxErrorException {
31+
final String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WORKBENCH_TEMPLATE_GET));
32+
JsonObject jsonObject = new JsonObject();
33+
jsonObject.addProperty("agentid", agentId);
34+
return this.mainService.post(url, jsonObject.toString());
35+
}
36+
37+
@Override
38+
public void setWorkBenchData(WxCpAgentWorkBench wxCpAgentWorkBench) throws WxErrorException {
39+
final String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WORKBENCH_DATA_SET));
40+
this.mainService.post(url, wxCpAgentWorkBench.toUserDataString());
41+
}
42+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonObject;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
import me.chanjar.weixin.cp.bean.workbench.WorkBenchKeyData;
10+
import me.chanjar.weixin.cp.bean.workbench.WorkBenchList;
11+
import me.chanjar.weixin.cp.constant.WxCpConsts;
12+
13+
import java.io.Serializable;
14+
import java.util.List;
15+
16+
/**
17+
* @author songshiyu
18+
* @date : create in 16:09 2020/9/27
19+
* @description: 工作台自定义展示
20+
*/
21+
@Data
22+
@Builder
23+
@NoArgsConstructor
24+
@AllArgsConstructor
25+
public class WxCpAgentWorkBench implements Serializable {
26+
private static final long serialVersionUid = 1L;
27+
28+
/*
29+
* 展示类型,目前支持 “keydata”、 “image”、 “list” 、”webview”
30+
* */
31+
private String type;
32+
/*
33+
* 用户的userid
34+
* */
35+
private String userId;
36+
/*
37+
* 应用id
38+
* */
39+
private Long agentId;
40+
/*
41+
* 点击跳转url,若不填且应用设置了主页url,则跳转到主页url,否则跳到应用会话窗口
42+
* */
43+
private String jumpUrl;
44+
/*
45+
* 若应用为小程序类型,该字段填小程序pagepath,若未设置,跳到小程序主页
46+
* */
47+
private String pagePath;
48+
/*
49+
* 图片url:图片的最佳比例为3.35:1;webview:渲染展示的url
50+
* */
51+
private String url;
52+
/*
53+
* 是否覆盖用户工作台的数据。设置为true的时候,会覆盖企业所有用户当前设置的数据。若设置为false,则不会覆盖用户当前设置的所有数据
54+
* */
55+
private Boolean replaceUserData;
56+
57+
private List<WorkBenchKeyData> keyDataList;
58+
59+
private List<WorkBenchList> lists;
60+
61+
// 生成模板Json字符串
62+
public String toTemplateString() {
63+
JsonObject templateObject = new JsonObject();
64+
templateObject.addProperty("agentid", this.agentId);
65+
templateObject.addProperty("type", this.type);
66+
if (this.replaceUserData != null) {
67+
templateObject.addProperty("replace_user_data", this.replaceUserData);
68+
}
69+
this.handle(templateObject);
70+
return templateObject.toString();
71+
}
72+
73+
// 生成用户数据Json字符串
74+
public String toUserDataString() {
75+
JsonObject userDataObject = new JsonObject();
76+
userDataObject.addProperty("agentid", this.agentId);
77+
userDataObject.addProperty("userid", this.userId);
78+
userDataObject.addProperty("type", this.type);
79+
this.handle(userDataObject);
80+
return userDataObject.toString();
81+
}
82+
83+
// 处理不用类型的工作台数据
84+
private void handle(JsonObject templateObject) {
85+
switch (this.getType()) {
86+
case WxCpConsts.WorkBenchType.KEYDATA: {
87+
JsonArray keyDataArray = new JsonArray();
88+
JsonObject itemsObject = new JsonObject();
89+
for (WorkBenchKeyData keyDataItem : this.keyDataList) {
90+
JsonObject keyDataObject = new JsonObject();
91+
keyDataObject.addProperty("key", keyDataItem.getKey());
92+
keyDataObject.addProperty("data", keyDataItem.getData());
93+
keyDataObject.addProperty("jump_url", keyDataItem.getJumpUrl());
94+
keyDataObject.addProperty("pagepath", keyDataItem.getPagePath());
95+
keyDataArray.add(keyDataObject);
96+
}
97+
itemsObject.add("items", keyDataArray);
98+
templateObject.add("keydata", itemsObject);
99+
break;
100+
}
101+
case WxCpConsts.WorkBenchType.IMAGE: {
102+
JsonObject image = new JsonObject();
103+
image.addProperty("url", this.url);
104+
image.addProperty("jump_url", this.jumpUrl);
105+
image.addProperty("pagepath", this.pagePath);
106+
templateObject.add("image", image);
107+
break;
108+
}
109+
case WxCpConsts.WorkBenchType.LIST: {
110+
JsonArray listArray = new JsonArray();
111+
JsonObject itemsObject = new JsonObject();
112+
for (WorkBenchList listItem : this.lists) {
113+
JsonObject listObject = new JsonObject();
114+
listObject.addProperty("title", listItem.getTitle());
115+
listObject.addProperty("jump_url", listItem.getJumpUrl());
116+
listObject.addProperty("pagepath", listItem.getPagePath());
117+
listArray.add(listObject);
118+
}
119+
itemsObject.add("items",listArray);
120+
templateObject.add("list", itemsObject);
121+
break;
122+
}
123+
case WxCpConsts.WorkBenchType.WEBVIEW: {
124+
JsonObject webview = new JsonObject();
125+
webview.addProperty("url", this.url);
126+
webview.addProperty("jump_url", this.jumpUrl);
127+
webview.addProperty("pagepath", this.pagePath);
128+
templateObject.add("webview", webview);
129+
break;
130+
}
131+
default: {
132+
//do nothing
133+
}
134+
}
135+
}
136+
137+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package me.chanjar.weixin.cp.bean.workbench;
2+
3+
import lombok.Data;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* @author songshiyu
9+
* @date : create in 10:21 2020/9/28
10+
* @description: 关键数据型模板类型
11+
*/
12+
@Data
13+
public class WorkBenchKeyData implements Serializable {
14+
/*
15+
* 关键数据名称
16+
* */
17+
private String key;
18+
/*
19+
* 关键数据
20+
* */
21+
private String data;
22+
/*
23+
* 点击跳转url,若不填且应用设置了主页url,则跳转到主页url,否则跳到应用会话窗口
24+
* */
25+
private String jumpUrl;
26+
/*
27+
* 若应用为小程序类型,该字段填小程序pagepath,若未设置,跳到小程序主页
28+
* */
29+
private String pagePath;
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package me.chanjar.weixin.cp.bean.workbench;
2+
3+
import lombok.Data;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* @author songshiyu
9+
* @date : create in 10:21 2020/9/28
10+
* @description: 列表模板类型
11+
*/
12+
@Data
13+
public class WorkBenchList implements Serializable {
14+
/*
15+
* 列表显示文字,不超过128个字节
16+
* */
17+
private String title;
18+
/*
19+
* 点击跳转url,若不填且应用设置了主页url,则跳转到主页url,否则跳到应用会话窗口
20+
* */
21+
private String jumpUrl;
22+
/*
23+
* 若应用为小程序类型,该字段填小程序pagepath,若未设置,跳到小程序主页
24+
* */
25+
private String pagePath;
26+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public static class Agent {
5454
public static final String AGENT_LIST = "/cgi-bin/agent/list";
5555
}
5656

57+
@UtilityClass
58+
public static class WorkBench {
59+
public static final String WORKBENCH_TEMPLATE_SET = "/cgi-bin/agent/set_workbench_template";
60+
public static final String WORKBENCH_TEMPLATE_GET = "/cgi-bin/agent/get_workbench_template";
61+
public static final String WORKBENCH_DATA_SET = "/cgi-bin/agent/set_workbench_data";
62+
}
63+
5764
@UtilityClass
5865
public static class OAuth2 {
5966
public static final String GET_USER_INFO = "/cgi-bin/user/getuserinfo?code=%s&agentid=%d";

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpConsts.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,23 @@ public static class AppChatMsgType {
314314
*/
315315
public static final String MARKDOWN = "markdown";
316316
}
317+
318+
public static class WorkBenchType {
319+
/*
320+
* 关键数据型
321+
* */
322+
public static final String KEYDATA = "keydata";
323+
/*
324+
* 图片型
325+
* */
326+
public static final String IMAGE = "image";
327+
/*
328+
* 列表型
329+
* */
330+
public static final String LIST = "list";
331+
/*
332+
* webview型
333+
* */
334+
public static final String WEBVIEW = "webview";
335+
}
317336
}

0 commit comments

Comments
 (0)