From 74f0b44be6979294a50e79ab37dc06979304b286 Mon Sep 17 00:00:00 2001 From: Daniel Qian Date: Mon, 25 Aug 2014 19:32:24 +0800 Subject: [PATCH] =?UTF-8?q?issue=20#1=20=E6=B7=BB=E5=8A=A0=E5=88=86?= =?UTF-8?q?=E7=BB=84=E7=AE=A1=E7=90=86=E6=8E=A5=E5=8F=A3-=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E7=94=A8=E6=88=B7=E6=89=80=E5=9C=A8=E5=88=86=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/chanjarster/weixin/api/WxService.java | 10 ++++++++++ .../java/chanjarster/weixin/api/WxServiceImpl.java | 8 ++++++++ src/main/java/chanjarster/weixin/bean/WxGroup.java | 4 ++++ .../chanjarster/weixin/api/WxCustomMessageAPITest.java | 4 ++-- .../java/chanjarster/weixin/api/WxGroupAPITest.java | 7 +++++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/java/chanjarster/weixin/api/WxService.java b/src/main/java/chanjarster/weixin/api/WxService.java index 3bfbb07c..67e0fd03 100644 --- a/src/main/java/chanjarster/weixin/api/WxService.java +++ b/src/main/java/chanjarster/weixin/api/WxService.java @@ -189,6 +189,16 @@ public interface WxService { */ public WxGroup groupCreate(String name) throws WxErrorException; + /** + *
+   * 分组管理接口 - 查询用户所在分组
+   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口
+   * 
+ * @param openid 微信用户的openid + * @throws WxErrorException + */ + public long groupQueryUserGroup(String openid) throws WxErrorException; + /** *
    * 分组管理接口 - 查询所有分组
diff --git a/src/main/java/chanjarster/weixin/api/WxServiceImpl.java b/src/main/java/chanjarster/weixin/api/WxServiceImpl.java
index b0607356..56a61c7f 100644
--- a/src/main/java/chanjarster/weixin/api/WxServiceImpl.java
+++ b/src/main/java/chanjarster/weixin/api/WxServiceImpl.java
@@ -38,6 +38,7 @@ import chanjarster.weixin.util.http.MediaUploadRequestExecutor;
 import chanjarster.weixin.util.http.RequestExecutor;
 import chanjarster.weixin.util.http.SimpleGetRequestExecutor;
 import chanjarster.weixin.util.http.SimplePostRequestExecutor;
+import chanjarster.weixin.util.json.GsonHelper;
 import chanjarster.weixin.util.json.WxGsonBuilder;
 
 import com.google.gson.JsonElement;
@@ -210,6 +211,13 @@ public class WxServiceImpl implements WxService {
     return WxGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("groups"), new TypeToken>(){}.getType());
   }
   
+  public long groupQueryUserGroup(String openid) throws WxErrorException {
+    String url = "https://api.weixin.qq.com/cgi-bin/groups/getid";
+    String responseContent = execute(new SimplePostRequestExecutor(), url, MessageFormat.format("'{'\"openid\":\"{0}\"}", openid));
+    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
+    return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("groupid"));
+  }
+  
   /**
    * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
    * @param executor
diff --git a/src/main/java/chanjarster/weixin/bean/WxGroup.java b/src/main/java/chanjarster/weixin/bean/WxGroup.java
index 90a6f159..38c5ceb8 100644
--- a/src/main/java/chanjarster/weixin/bean/WxGroup.java
+++ b/src/main/java/chanjarster/weixin/bean/WxGroup.java
@@ -38,5 +38,9 @@ public class WxGroup {
   public String toJson(String json) {
     return WxGsonBuilder.create().toJson(this);
   }
+  @Override
+  public String toString() {
+    return "WxGroup [id=" + id + ", name=" + name + ", count=" + count + "]";
+  }
   
 }
diff --git a/src/test/java/chanjarster/weixin/api/WxCustomMessageAPITest.java b/src/test/java/chanjarster/weixin/api/WxCustomMessageAPITest.java
index 9dfa08b4..77cdd596 100644
--- a/src/test/java/chanjarster/weixin/api/WxCustomMessageAPITest.java
+++ b/src/test/java/chanjarster/weixin/api/WxCustomMessageAPITest.java
@@ -22,10 +22,10 @@ public class WxCustomMessageAPITest {
   protected WxServiceImpl wxService;
 
   public void testSendCustomMessage() throws WxErrorException {
-    WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage;
+    WxXmlConfigStorage configStorage = (WxXmlConfigStorage) wxService.wxConfigStorage;
     WxCustomMessage message = new WxCustomMessage();
     message.setMsgtype(WxConsts.CUSTOM_MSG_TEXT);
-    message.setTouser(configProvider.getOpenId());
+    message.setTouser(configStorage.getOpenId());
     message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:Hello World");
 
     wxService.customMessageSend(message);
diff --git a/src/test/java/chanjarster/weixin/api/WxGroupAPITest.java b/src/test/java/chanjarster/weixin/api/WxGroupAPITest.java
index b20b2f66..82d1c3dc 100644
--- a/src/test/java/chanjarster/weixin/api/WxGroupAPITest.java
+++ b/src/test/java/chanjarster/weixin/api/WxGroupAPITest.java
@@ -6,6 +6,7 @@ import org.testng.Assert;
 import org.testng.annotations.Guice;
 import org.testng.annotations.Test;
 
+import chanjarster.weixin.api.ApiTestModule.WxXmlConfigStorage;
 import chanjarster.weixin.bean.WxGroup;
 import chanjarster.weixin.exception.WxErrorException;
 
@@ -38,4 +39,10 @@ public class WxGroupAPITest {
     }
   }
   
+  @Test(dependsOnMethods="testGroupCreate")
+  public void groupQueryUserGroup() throws WxErrorException {
+    WxXmlConfigStorage configStorage = (WxXmlConfigStorage) wxService.wxConfigStorage;
+    long groupid = wxService.groupQueryUserGroup(configStorage.getOpenId());
+  }
+  
 }