浏览代码

issue #1 添加分组管理接口-移动用户分组

master
Daniel Qian 10 年前
父节点
当前提交
5ea560f6b4
共有 4 个文件被更改,包括 35 次插入9 次删除
  1. +17
    -4
      src/main/java/chanjarster/weixin/api/WxService.java
  2. +5
    -0
      src/main/java/chanjarster/weixin/api/WxServiceImpl.java
  3. +1
    -1
      src/main/java/chanjarster/weixin/bean/WxGroup.java
  4. +12
    -4
      src/test/java/chanjarster/weixin/api/WxGroupAPITest.java

+ 17
- 4
src/main/java/chanjarster/weixin/api/WxService.java 查看文件

@@ -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>
* @return
* @throws WxErrorException
*/
public List<WxGroup> groupGet() throws WxErrorException;
/**
* <pre>
* 分组管理接口 - 查询用户所在分组
@@ -213,14 +223,17 @@ public interface WxService {
/**
* <pre>
* 分组管理接口 - 查询所有分组
* 分组管理接口 - 移动用户分组
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口
*
* 如果to_groupid为0(未分组),1(黑名单),2(星标组),或者不存在的id,微信会返回系统繁忙的错误
* </pre>
* @return
* @param openid 用户openid
* @param to_groupid 移动到的分组id
* @throws WxErrorException
*/
public List<WxGroup> groupGet() throws WxErrorException;
public void groupMoveUser(String openid, long to_groupid) throws WxErrorException;
/**
* 注入 {@link WxConfigStorage} 的实现
* @param wxConfigProvider


+ 5
- 0
src/main/java/chanjarster/weixin/api/WxServiceImpl.java 查看文件

@@ -226,6 +226,11 @@ public class WxServiceImpl implements WxService {
execute(new SimplePostRequestExecutor(), url, group.toJson());
}
public void groupMoveUser(String openid, long to_groupid) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/groups/members/update";
execute(new SimplePostRequestExecutor(), url, MessageFormat.format("'{'\"openid\":\"{0}\", \"to_groupid\":{1,number,#}}", openid, to_groupid));
}
/**
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
* @param executor


+ 1
- 1
src/main/java/chanjarster/weixin/bean/WxGroup.java 查看文件

@@ -9,7 +9,7 @@ import chanjarster.weixin.util.json.WxGsonBuilder;
*/
public class WxGroup {

private long id;
private long id = -1;
private String name;
private long count;
public long getId() {


+ 12
- 4
src/test/java/chanjarster/weixin/api/WxGroupAPITest.java 查看文件

@@ -24,6 +24,8 @@ public class WxGroupAPITest {
@Inject
protected WxServiceImpl wxService;

protected WxGroup group;
public void testGroupCreate() throws WxErrorException {
WxGroup res = wxService.groupCreate("测试分组1");
Assert.assertEquals(res.getName(), "测试分组1");
@@ -35,7 +37,7 @@ public class WxGroupAPITest {
Assert.assertNotNull(groupList);
Assert.assertTrue(groupList.size() > 0);
for (WxGroup g : groupList) {
System.out.println(g.toString());
group = g;
Assert.assertNotNull(g.getName());
}
}
@@ -44,12 +46,18 @@ public class WxGroupAPITest {
public void testGroupQueryUserGroup() throws WxErrorException {
WxXmlConfigStorage configStorage = (WxXmlConfigStorage) wxService.wxConfigStorage;
long groupid = wxService.groupQueryUserGroup(configStorage.getOpenId());
Assert.assertTrue(groupid != -1l);
}
@Test(dependsOnMethods={"testGroupGet", "testGroupCreate"})
public void getGroupUpdate() throws WxErrorException {
WxGroup group = new WxGroup();
group.setId(3);
group.setName("未命名分组");
group.setName("分组改名");
wxService.groupUpdate(group);
}

@Test(dependsOnMethods={"testGroupGet", "testGroupCreate"})
public void getGroupMoveUser() throws WxErrorException {
WxXmlConfigStorage configStorage = (WxXmlConfigStorage) wxService.wxConfigStorage;
wxService.groupMoveUser(configStorage.getOpenId(), group.getId());
}
}

正在加载...
取消
保存