Преглед изворни кода

:new: #1793 企业微信添加应用管理的设置工作台自定义展示模块

Co-authored-by: sysong <sysong@chutianyun.gov.cn>
amhere пре 4 година
родитељ
комит
8428270e67

+ 18 - 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpAgentWorkBenchService.java

@@ -0,0 +1,18 @@
+package me.chanjar.weixin.cp.api;
+
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.cp.bean.WxCpAgentWorkBench;
+
+/**
+ * @author songshiyu
+ * @date : create in 16:16 2020/9/27
+ * @description: 工作台自定义展示:https://work.weixin.qq.com/api/doc/90000/90135/92535
+ */
+public interface WxCpAgentWorkBenchService {
+
+  void setWorkBenchTemplate(WxCpAgentWorkBench wxCpAgentWorkBench) throws WxErrorException;
+
+  String getWorkBenchTemplate(Long agentid) throws WxErrorException;
+
+  void setWorkBenchData(WxCpAgentWorkBench wxCpAgentWorkBench) throws WxErrorException;
+}

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

@@ -403,6 +403,12 @@ public interface WxCpService {
    * @return 群机器人消息推送服务 group robot service
    */
   WxCpGroupRobotService getGroupRobotService();
+  /*
+  * 获取工作台服务
+  *
+  * @return the workbench service
+  * */
+  WxCpAgentWorkBenchService getWorkBenchService();
 
   /**
    * http请求对象

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

@@ -54,6 +54,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
   private WxCpGroupRobotService groupRobotService = new WxCpGroupRobotServiceImpl(this);
   private WxCpMessageService messageService = new WxCpMessageServiceImpl(this);
   private WxCpOaCalendarService oaCalendarService = new WxCpOaCalendarServiceImpl(this);
+  private WxCpAgentWorkBenchService workBenchService = new WxCpAgentWorkBenchServiceImpl(this);
 
   /**
    * 全局的是否正在刷新access token的锁.
@@ -438,6 +439,11 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
   }
 
   @Override
+  public WxCpAgentWorkBenchService getWorkBenchService() {
+    return workBenchService;
+  }
+
+  @Override
   public WxCpTaskCardService getTaskCardService() {
     return taskCardService;
   }

+ 42 - 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpAgentWorkBenchServiceImpl.java

@@ -0,0 +1,42 @@
+package me.chanjar.weixin.cp.api.impl;
+
+import com.google.gson.JsonObject;
+import lombok.RequiredArgsConstructor;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.cp.api.WxCpAgentWorkBenchService;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.bean.WxCpAgentWorkBench;
+
+import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.WorkBench.WORKBENCH_DATA_SET;
+import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.WorkBench.WORKBENCH_TEMPLATE_GET;
+import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.WorkBench.WORKBENCH_TEMPLATE_SET;
+
+/**
+ * @author songshiyu
+ * @date : create in 11:24 2020/9/28
+ * @description: 工作台自定义展示实现
+ */
+@RequiredArgsConstructor
+public class WxCpAgentWorkBenchServiceImpl implements WxCpAgentWorkBenchService {
+  private final WxCpService mainService;
+
+  @Override
+  public void setWorkBenchTemplate(WxCpAgentWorkBench wxCpAgentWorkBench) throws WxErrorException {
+    final String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WORKBENCH_TEMPLATE_SET));
+    this.mainService.post(url, wxCpAgentWorkBench.toTemplateString());
+  }
+
+  @Override
+  public String getWorkBenchTemplate(Long agentId) throws WxErrorException {
+    final String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WORKBENCH_TEMPLATE_GET));
+    JsonObject jsonObject = new JsonObject();
+    jsonObject.addProperty("agentid", agentId);
+    return this.mainService.post(url, jsonObject.toString());
+  }
+
+  @Override
+  public void setWorkBenchData(WxCpAgentWorkBench wxCpAgentWorkBench) throws WxErrorException {
+    final String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(WORKBENCH_DATA_SET));
+    this.mainService.post(url, wxCpAgentWorkBench.toUserDataString());
+  }
+}

+ 137 - 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAgentWorkBench.java

@@ -0,0 +1,137 @@
+package me.chanjar.weixin.cp.bean;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import me.chanjar.weixin.cp.bean.workbench.WorkBenchKeyData;
+import me.chanjar.weixin.cp.bean.workbench.WorkBenchList;
+import me.chanjar.weixin.cp.constant.WxCpConsts;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * @author songshiyu
+ * @date : create in 16:09 2020/9/27
+ * @description: 工作台自定义展示
+ */
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class WxCpAgentWorkBench implements Serializable {
+  private static final long serialVersionUid = 1L;
+
+  /*
+  * 展示类型,目前支持 “keydata”、 “image”、 “list” 、”webview”
+  * */
+  private String type;
+  /*
+  * 用户的userid
+  * */
+  private String userId;
+  /*
+  * 应用id
+  * */
+  private Long agentId;
+  /*
+  * 点击跳转url,若不填且应用设置了主页url,则跳转到主页url,否则跳到应用会话窗口
+  * */
+  private String jumpUrl;
+  /*
+  * 若应用为小程序类型,该字段填小程序pagepath,若未设置,跳到小程序主页
+  * */
+  private String pagePath;
+  /*
+  * 图片url:图片的最佳比例为3.35:1;webview:渲染展示的url
+  * */
+  private String url;
+  /*
+  * 是否覆盖用户工作台的数据。设置为true的时候,会覆盖企业所有用户当前设置的数据。若设置为false,则不会覆盖用户当前设置的所有数据
+  * */
+  private Boolean replaceUserData;
+
+  private List<WorkBenchKeyData> keyDataList;
+
+  private List<WorkBenchList> lists;
+
+  // 生成模板Json字符串
+  public String toTemplateString() {
+    JsonObject templateObject = new JsonObject();
+    templateObject.addProperty("agentid", this.agentId);
+    templateObject.addProperty("type", this.type);
+    if (this.replaceUserData != null) {
+      templateObject.addProperty("replace_user_data", this.replaceUserData);
+    }
+    this.handle(templateObject);
+    return templateObject.toString();
+  }
+
+  // 生成用户数据Json字符串
+  public String toUserDataString() {
+    JsonObject userDataObject = new JsonObject();
+    userDataObject.addProperty("agentid", this.agentId);
+    userDataObject.addProperty("userid", this.userId);
+    userDataObject.addProperty("type", this.type);
+    this.handle(userDataObject);
+    return userDataObject.toString();
+  }
+
+  // 处理不用类型的工作台数据
+  private void handle(JsonObject templateObject) {
+    switch (this.getType()) {
+      case WxCpConsts.WorkBenchType.KEYDATA: {
+        JsonArray keyDataArray = new JsonArray();
+        JsonObject itemsObject = new JsonObject();
+        for (WorkBenchKeyData keyDataItem : this.keyDataList) {
+          JsonObject keyDataObject = new JsonObject();
+          keyDataObject.addProperty("key", keyDataItem.getKey());
+          keyDataObject.addProperty("data", keyDataItem.getData());
+          keyDataObject.addProperty("jump_url", keyDataItem.getJumpUrl());
+          keyDataObject.addProperty("pagepath", keyDataItem.getPagePath());
+          keyDataArray.add(keyDataObject);
+        }
+        itemsObject.add("items", keyDataArray);
+        templateObject.add("keydata", itemsObject);
+        break;
+      }
+      case WxCpConsts.WorkBenchType.IMAGE: {
+        JsonObject image = new JsonObject();
+        image.addProperty("url", this.url);
+        image.addProperty("jump_url", this.jumpUrl);
+        image.addProperty("pagepath", this.pagePath);
+        templateObject.add("image", image);
+        break;
+      }
+      case WxCpConsts.WorkBenchType.LIST: {
+        JsonArray listArray = new JsonArray();
+        JsonObject itemsObject = new JsonObject();
+        for (WorkBenchList listItem : this.lists) {
+          JsonObject listObject = new JsonObject();
+          listObject.addProperty("title", listItem.getTitle());
+          listObject.addProperty("jump_url", listItem.getJumpUrl());
+          listObject.addProperty("pagepath", listItem.getPagePath());
+          listArray.add(listObject);
+        }
+        itemsObject.add("items",listArray);
+        templateObject.add("list", itemsObject);
+        break;
+      }
+      case WxCpConsts.WorkBenchType.WEBVIEW: {
+        JsonObject webview = new JsonObject();
+        webview.addProperty("url", this.url);
+        webview.addProperty("jump_url", this.jumpUrl);
+        webview.addProperty("pagepath", this.pagePath);
+        templateObject.add("webview", webview);
+        break;
+      }
+      default: {
+        //do nothing
+      }
+    }
+  }
+
+}

+ 30 - 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/workbench/WorkBenchKeyData.java

@@ -0,0 +1,30 @@
+package me.chanjar.weixin.cp.bean.workbench;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * @author songshiyu
+ * @date : create in 10:21 2020/9/28
+ * @description: 关键数据型模板类型
+ */
+@Data
+public class WorkBenchKeyData implements Serializable {
+  /*
+   * 关键数据名称
+   * */
+  private String key;
+  /*
+   * 关键数据
+   * */
+  private String data;
+  /*
+   * 点击跳转url,若不填且应用设置了主页url,则跳转到主页url,否则跳到应用会话窗口
+   * */
+  private String jumpUrl;
+  /*
+   * 若应用为小程序类型,该字段填小程序pagepath,若未设置,跳到小程序主页
+   * */
+  private String pagePath;
+}

+ 26 - 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/workbench/WorkBenchList.java

@@ -0,0 +1,26 @@
+package me.chanjar.weixin.cp.bean.workbench;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * @author songshiyu
+ * @date : create in 10:21 2020/9/28
+ * @description: 列表模板类型
+ */
+@Data
+public class WorkBenchList implements Serializable {
+  /*
+   * 列表显示文字,不超过128个字节
+   * */
+  private String title;
+  /*
+   * 点击跳转url,若不填且应用设置了主页url,则跳转到主页url,否则跳到应用会话窗口
+   * */
+  private String jumpUrl;
+  /*
+   * 若应用为小程序类型,该字段填小程序pagepath,若未设置,跳到小程序主页
+   * */
+  private String pagePath;
+}

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

@@ -55,6 +55,13 @@ public final class WxCpApiPathConsts {
   }
 
   @UtilityClass
+  public static class WorkBench {
+    public static final String WORKBENCH_TEMPLATE_SET = "/cgi-bin/agent/set_workbench_template";
+    public static final String WORKBENCH_TEMPLATE_GET = "/cgi-bin/agent/get_workbench_template";
+    public static final String WORKBENCH_DATA_SET = "/cgi-bin/agent/set_workbench_data";
+  }
+
+  @UtilityClass
   public static class OAuth2 {
     public static final String GET_USER_INFO = "/cgi-bin/user/getuserinfo?code=%s&agentid=%d";
     public static final String GET_USER_DETAIL = "/cgi-bin/user/getuserdetail";

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

@@ -314,4 +314,23 @@ public class WxCpConsts {
      */
     public static final String MARKDOWN = "markdown";
   }
+
+  public static class WorkBenchType {
+    /*
+    * 关键数据型
+    * */
+    public static final String KEYDATA = "keydata";
+    /*
+    * 图片型
+    * */
+    public static final String IMAGE = "image";
+    /*
+    * 列表型
+    * */
+    public static final String LIST = "list";
+    /*
+    * webview型
+    * */
+    public static final String WEBVIEW = "webview";
+  }
 }

+ 130 - 0
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpAgentWorkBenchImplTest.java

@@ -0,0 +1,130 @@
+package me.chanjar.weixin.cp.api.impl;
+
+import com.google.inject.Inject;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.cp.api.ApiTestModule;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.bean.WxCpAgentWorkBench;
+import me.chanjar.weixin.cp.bean.workbench.WorkBenchKeyData;
+import me.chanjar.weixin.cp.bean.workbench.WorkBenchList;
+import me.chanjar.weixin.cp.constant.WxCpConsts;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Guice;
+import org.testng.annotations.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author songshiyu
+ * @date : create in 10:31 2020/9/29
+ * @description: 测试工作台服务
+ */
+@Guice(modules = ApiTestModule.class)
+public class WxCpAgentWorkBenchImplTest {
+
+  @Inject
+  private WxCpService wxCpService;
+
+  @Test(dataProvider = "template")
+  public void testTemplateSet(WxCpAgentWorkBench template) throws WxErrorException {
+    this.wxCpService.getWorkBenchService().setWorkBenchTemplate(template);
+  }
+
+  @Test
+  public void testTemplateGet() throws WxErrorException {
+    String result = this.wxCpService.getWorkBenchService().getWorkBenchTemplate(1000011L);
+    System.out.println("获取工作台模板设置:"+result);
+  }
+
+  @Test(dataProvider = "userDatas")
+  public void testUserDataSet(WxCpAgentWorkBench userDatas) throws WxErrorException {
+    this.wxCpService.getWorkBenchService().setWorkBenchData(userDatas);
+  }
+
+  @DataProvider
+  public Object[][] template() {
+    return new Object[][]{
+      {WxCpAgentWorkBench.builder()
+        .agentId(1000011L)
+        .replaceUserData(true)
+        .type(WxCpConsts.WorkBenchType.IMAGE)
+        .url("http://www.qq.com")
+        .jumpUrl("http://www.qq.com")
+        .pagePath("pages/index")
+        .build()
+      },
+    };
+  }
+
+  @DataProvider
+  public Object[][] userDatas() {
+    return new Object[][]{
+      {WxCpAgentWorkBench.builder()
+        .agentId(1000011L)
+        .userId("HaHa")
+        .type(WxCpConsts.WorkBenchType.IMAGE)
+        .url("http://www.qq.com")
+        .jumpUrl("http://www.qq.com")
+        .pagePath("pages/index")
+        .build()
+      },
+    };
+  }
+
+  @Test
+  public void testKeyDataTemplateSet() throws WxErrorException {
+    WxCpAgentWorkBench template = new WxCpAgentWorkBench();
+    template.setAgentId(1000011L);
+    template.setType(WxCpConsts.WorkBenchType.KEYDATA);
+    List<WorkBenchKeyData> workBenchKeyDataList = new ArrayList<>();
+    for(int i = 1;i < 4;i++){
+      WorkBenchKeyData workBenchKeyData = new WorkBenchKeyData();
+      workBenchKeyData.setKey("审批"+i);
+      workBenchKeyData.setData(String.valueOf(i));
+      workBenchKeyData.setJumpUrl("http://www.qq.com");
+      workBenchKeyData.setPagePath("pages/index");
+      workBenchKeyDataList.add(workBenchKeyData);
+    }
+    template.setKeyDataList(workBenchKeyDataList);
+    template.setReplaceUserData(true);
+    this.wxCpService.getWorkBenchService().setWorkBenchTemplate(template);
+  }
+
+  @Test
+  public void testKeyDataUserDataSet() throws WxErrorException {
+    WxCpAgentWorkBench template = new WxCpAgentWorkBench();
+    template.setAgentId(1000011L);
+    template.setUserId("HaHa");
+    template.setType(WxCpConsts.WorkBenchType.KEYDATA);
+    List<WorkBenchKeyData> workBenchKeyDataList = new ArrayList<>();
+    WorkBenchKeyData workBenchKeyData = new WorkBenchKeyData();
+    workBenchKeyData.setKey("待审批");
+    workBenchKeyData.setData("2");
+    workBenchKeyData.setJumpUrl("http://www.qq.com");
+    workBenchKeyData.setPagePath("pages/index");
+    workBenchKeyDataList.add(workBenchKeyData);
+    template.setKeyDataList(workBenchKeyDataList);
+    this.wxCpService.getWorkBenchService().setWorkBenchTemplate(template);
+  }
+
+  @Test
+  public void testListTemplateSet() throws WxErrorException {
+    WxCpAgentWorkBench template = new WxCpAgentWorkBench();
+    template.setAgentId(1000011L);
+    template.setType(WxCpConsts.WorkBenchType.LIST);
+    List<WorkBenchList> workBenchListArray = new ArrayList<>();
+    for(int i = 0;i < 2;i++){
+      WorkBenchList workBenchlist = new WorkBenchList();
+      workBenchlist.setTitle("测试"+i);
+      workBenchlist.setJumpUrl("http://www.qq.com");
+      workBenchlist.setPagePath("pages/index");
+      workBenchListArray.add(workBenchlist);
+    }
+    template.setLists(workBenchListArray);
+    template.setReplaceUserData(true);
+    this.wxCpService.getWorkBenchService().setWorkBenchTemplate(template);
+  }
+
+
+}