Browse Source

issue #1 添加分组管理接口-查询用户所在分组

master
Daniel Qian 10 years ago
parent
commit
74f0b44be6
5 changed files with 31 additions and 2 deletions
  1. +10
    -0
      src/main/java/chanjarster/weixin/api/WxService.java
  2. +8
    -0
      src/main/java/chanjarster/weixin/api/WxServiceImpl.java
  3. +4
    -0
      src/main/java/chanjarster/weixin/bean/WxGroup.java
  4. +2
    -2
      src/test/java/chanjarster/weixin/api/WxCustomMessageAPITest.java
  5. +7
    -0
      src/test/java/chanjarster/weixin/api/WxGroupAPITest.java

+ 10
- 0
src/main/java/chanjarster/weixin/api/WxService.java View File

@@ -189,6 +189,16 @@ public interface WxService {
*/
public WxGroup groupCreate(String name) throws WxErrorException;
/**
* <pre>
* 分组管理接口 - 查询用户所在分组
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口
* </pre>
* @param openid 微信用户的openid
* @throws WxErrorException
*/
public long groupQueryUserGroup(String openid) throws WxErrorException;
/**
* <pre>
* 分组管理接口 - 查询所有分组


+ 8
- 0
src/main/java/chanjarster/weixin/api/WxServiceImpl.java View File

@@ -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<List<WxGroup>>(){}.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


+ 4
- 0
src/main/java/chanjarster/weixin/bean/WxGroup.java View File

@@ -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 + "]";
}
}

+ 2
- 2
src/test/java/chanjarster/weixin/api/WxCustomMessageAPITest.java View File

@@ -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超链接:<a href=\"http://www.baidu.com\">Hello World</a>");

wxService.customMessageSend(message);


+ 7
- 0
src/test/java/chanjarster/weixin/api/WxGroupAPITest.java View File

@@ -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());
}
}

Loading…
Cancel
Save