diff --git a/src/main/java/chanjarster/weixin/api/WxService.java b/src/main/java/chanjarster/weixin/api/WxService.java index 3bfbb07cb..67e0fd03a 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 b06073567..56a61c7fb 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 90a6f159b..38c5ceb8f 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 9dfa08b4f..77cdd5963 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 b20b2f667..82d1c3dcd 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());
+ }
+
}