Ver código fonte

#707 企业微信增加应用管理里的设置和列表接口

Binary Wang 6 anos atrás
pai
commit
4dd67f46b8

+ 25 - 1
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpAgentService.java

@@ -3,9 +3,12 @@ package me.chanjar.weixin.cp.api;
 import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.cp.bean.WxCpAgent;
 
+import java.util.List;
+
 /**
  * <pre>
  *  管理企业号应用
+ *  文档地址:https://work.weixin.qq.com/api/doc#10087
  *  Created by huansinho on 2018/4/13.
  * </pre>
  *
@@ -17,7 +20,7 @@ public interface WxCpAgentService {
    * <pre>
    * 获取企业号应用信息
    * 该API用于获取企业号某个应用的基本信息,包括头像、昵称、帐号类型、认证类型、可见范围等信息
-   * 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=获取企业号应用
+   * 详情请见: https://work.weixin.qq.com/api/doc#10087
    * </pre>
    *
    * @param agentId 企业应用的id
@@ -25,4 +28,25 @@ public interface WxCpAgentService {
    */
   WxCpAgent get(Integer agentId) throws WxErrorException;
 
+  /**
+   * <pre>
+   * 设置应用.
+   * 仅企业可调用,可设置当前凭证对应的应用;第三方不可调用。
+   * 详情请见: https://work.weixin.qq.com/api/doc#10088
+   * </pre>
+   *
+   * @param agentInfo 应用信息
+   */
+  void set(WxCpAgent agentInfo) throws WxErrorException;
+
+  /**
+   * <pre>
+   * 获取应用列表.
+   * 企业仅可获取当前凭证对应的应用;第三方仅可获取被授权的应用。
+   * 详情请见: https://work.weixin.qq.com/api/doc#11214
+   * </pre>
+   *
+   */
+  List<WxCpAgent> list() throws WxErrorException;
+
 }

+ 33 - 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImpl.java

@@ -1,9 +1,17 @@
 package me.chanjar.weixin.cp.api.impl;
 
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.reflect.TypeToken;
+import me.chanjar.weixin.common.error.WxError;
 import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.cp.api.WxCpAgentService;
 import me.chanjar.weixin.cp.api.WxCpService;
 import me.chanjar.weixin.cp.bean.WxCpAgent;
+import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
+
+import java.util.List;
 
 
 /**
@@ -15,6 +23,8 @@ import me.chanjar.weixin.cp.bean.WxCpAgent;
  * @author <a href="https://github.com/huansinho">huansinho</a>
  */
 public class WxCpAgentServiceImpl implements WxCpAgentService {
+  private static final JsonParser JSON_PARSER = new JsonParser();
+
   private WxCpService mainService;
 
   public WxCpAgentServiceImpl(WxCpService mainService) {
@@ -32,4 +42,27 @@ public class WxCpAgentServiceImpl implements WxCpAgentService {
     return WxCpAgent.fromJson(responseContent);
   }
 
+  @Override
+  public void set(WxCpAgent agentInfo) throws WxErrorException {
+    String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/set";
+    String responseContent = this.mainService.post(url, agentInfo.toJson());
+    JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
+    if (jsonObject.get("errcode").getAsInt() != 0) {
+      throw new WxErrorException(WxError.fromJson(responseContent));
+    }
+  }
+
+  @Override
+  public List<WxCpAgent> list() throws WxErrorException {
+    String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/list";
+    String responseContent = this.mainService.get(url, null);
+    JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
+    if (jsonObject.get("errcode").getAsInt() != 0) {
+      throw new WxErrorException(WxError.fromJson(responseContent));
+    }
+
+    return WxCpGsonBuilder.create().fromJson(jsonObject.get("agentlist").toString(), new TypeToken<List<WxCpAgent>>() {
+    }.getType());
+  }
+
 }

+ 11 - 2
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAgent.java

@@ -1,7 +1,10 @@
 package me.chanjar.weixin.cp.bean;
 
 import com.google.gson.annotations.SerializedName;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
 import lombok.Data;
+import lombok.NoArgsConstructor;
 import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
 
 import java.io.Serializable;
@@ -16,6 +19,9 @@ import java.util.List;
  * @author <a href="https://github.com/huansinho">huansinho</a>
  */
 @Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
 public class WxCpAgent implements Serializable {
   private static final long serialVersionUID = 5002894979081127234L;
 
@@ -34,6 +40,9 @@ public class WxCpAgent implements Serializable {
   @SerializedName("square_logo_url")
   private String squareLogoUrl;
 
+  @SerializedName("logo_mediaid")
+  private String logoMediaId;
+
   @SerializedName("description")
   private String description;
 
@@ -41,7 +50,7 @@ public class WxCpAgent implements Serializable {
   private Users allowUserInfos;
 
   @SerializedName("allow_partys")
-  private Partys allowParties;
+  private Parties allowParties;
 
   @SerializedName("allow_tags")
   private Tags allowTags;
@@ -82,7 +91,7 @@ public class WxCpAgent implements Serializable {
   }
 
   @Data
-  public class Partys {
+  public class Parties {
     @SerializedName("partyid")
     private List<Integer> partyIds = null;
   }

Diferenças do arquivo suprimidas por serem muito extensas
+ 58 - 10
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImplTest.java