| @@ -199,6 +199,18 @@ public interface WxService { | |||||
| */ | */ | ||||
| public long groupQueryUserGroup(String openid) throws WxErrorException; | public long groupQueryUserGroup(String openid) throws WxErrorException; | ||||
| /** | |||||
| * <pre> | |||||
| * 分组管理接口 - 修改分组名 | |||||
| * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口 | |||||
| * | |||||
| * 如果id为0(未分组),1(黑名单),2(星标组),或者不存在的id,微信会返回系统繁忙的错误 | |||||
| * </pre> | |||||
| * @param group 要更新的group,group的id,name必须设置 | |||||
| * @throws WxErrorException | |||||
| */ | |||||
| public void groupUpdate(WxGroup group) throws WxErrorException; | |||||
| /** | /** | ||||
| * <pre> | * <pre> | ||||
| * 分组管理接口 - 查询所有分组 | * 分组管理接口 - 查询所有分组 | ||||
| @@ -196,7 +196,10 @@ public class WxServiceImpl implements WxService { | |||||
| public WxGroup groupCreate(String name) throws WxErrorException { | public WxGroup groupCreate(String name) throws WxErrorException { | ||||
| String url = "https://api.weixin.qq.com/cgi-bin/groups/create"; | String url = "https://api.weixin.qq.com/cgi-bin/groups/create"; | ||||
| String responseContent = execute(new SimplePostRequestExecutor(), url, MessageFormat.format("'{'\"group\":'{'\"name\":\"{0}\"}}", name)); | |||||
| String responseContent = execute( | |||||
| new SimplePostRequestExecutor(), | |||||
| url, | |||||
| MessageFormat.format("'{'\"group\":'{'\"name\":\"{0}\"}}", name)); | |||||
| return WxGroup.fromJson(responseContent); | return WxGroup.fromJson(responseContent); | ||||
| } | } | ||||
| @@ -218,6 +221,11 @@ public class WxServiceImpl implements WxService { | |||||
| return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("groupid")); | return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("groupid")); | ||||
| } | } | ||||
| public void groupUpdate(WxGroup group) throws WxErrorException { | |||||
| String url = "https://api.weixin.qq.com/cgi-bin/groups/update"; | |||||
| execute(new SimplePostRequestExecutor(), url, group.toJson()); | |||||
| } | |||||
| /** | /** | ||||
| * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 | * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 | ||||
| * @param executor | * @param executor | ||||
| @@ -255,11 +263,11 @@ public class WxServiceImpl implements WxService { | |||||
| if(retryTimes.get() == null) { | if(retryTimes.get() == null) { | ||||
| retryTimes.set(0); | retryTimes.set(0); | ||||
| } | } | ||||
| if (retryTimes.get() > 5) { | |||||
| if (retryTimes.get() > 4) { | |||||
| retryTimes.set(0); | retryTimes.set(0); | ||||
| throw new RuntimeException("微信服务端异常,超出重试次数"); | throw new RuntimeException("微信服务端异常,超出重试次数"); | ||||
| } | } | ||||
| int sleepMillis = 1000 * (1 >> (retryTimes.get() - 1)); | |||||
| int sleepMillis = 1000 * (1 << retryTimes.get()); | |||||
| try { | try { | ||||
| System.out.println("微信系统繁忙," + sleepMillis + "ms后重试"); | System.out.println("微信系统繁忙," + sleepMillis + "ms后重试"); | ||||
| Thread.sleep(sleepMillis); | Thread.sleep(sleepMillis); | ||||
| @@ -35,7 +35,7 @@ public class WxGroup { | |||||
| return WxGsonBuilder.create().fromJson(json, WxGroup.class); | return WxGsonBuilder.create().fromJson(json, WxGroup.class); | ||||
| } | } | ||||
| public String toJson(String json) { | |||||
| public String toJson() { | |||||
| return WxGsonBuilder.create().toJson(this); | return WxGsonBuilder.create().toJson(this); | ||||
| } | } | ||||
| @Override | @Override | ||||
| @@ -35,14 +35,21 @@ public class WxGroupAPITest { | |||||
| Assert.assertNotNull(groupList); | Assert.assertNotNull(groupList); | ||||
| Assert.assertTrue(groupList.size() > 0); | Assert.assertTrue(groupList.size() > 0); | ||||
| for (WxGroup g : groupList) { | for (WxGroup g : groupList) { | ||||
| System.out.println(g.toString()); | |||||
| Assert.assertNotNull(g.getName()); | Assert.assertNotNull(g.getName()); | ||||
| } | } | ||||
| } | } | ||||
| @Test(dependsOnMethods="testGroupCreate") | @Test(dependsOnMethods="testGroupCreate") | ||||
| public void groupQueryUserGroup() throws WxErrorException { | |||||
| public void testGroupQueryUserGroup() throws WxErrorException { | |||||
| WxXmlConfigStorage configStorage = (WxXmlConfigStorage) wxService.wxConfigStorage; | WxXmlConfigStorage configStorage = (WxXmlConfigStorage) wxService.wxConfigStorage; | ||||
| long groupid = wxService.groupQueryUserGroup(configStorage.getOpenId()); | long groupid = wxService.groupQueryUserGroup(configStorage.getOpenId()); | ||||
| } | } | ||||
| public void getGroupUpdate() throws WxErrorException { | |||||
| WxGroup group = new WxGroup(); | |||||
| group.setId(3); | |||||
| group.setName("未命名分组"); | |||||
| wxService.groupUpdate(group); | |||||
| } | |||||
| } | } | ||||