| @@ -3,6 +3,7 @@ package chanjarster.weixin.api; | |||||
| import java.io.File; | import java.io.File; | ||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.InputStream; | import java.io.InputStream; | ||||
| import java.util.List; | |||||
| import chanjarster.weixin.bean.WxCustomMessage; | import chanjarster.weixin.bean.WxCustomMessage; | ||||
| import chanjarster.weixin.bean.WxGroup; | import chanjarster.weixin.bean.WxGroup; | ||||
| @@ -180,6 +181,7 @@ public interface WxService { | |||||
| /** | /** | ||||
| * <pre> | * <pre> | ||||
| * 分组管理接口 - 创建分组 | * 分组管理接口 - 创建分组 | ||||
| * 最多支持创建500个分组 | |||||
| * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口 | * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口 | ||||
| * </pre> | * </pre> | ||||
| * @param name 分组名字(30个字符以内) | * @param name 分组名字(30个字符以内) | ||||
| @@ -187,6 +189,16 @@ public interface WxService { | |||||
| */ | */ | ||||
| public WxGroup groupCreate(String name) throws WxErrorException; | 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; | |||||
| /** | /** | ||||
| * 注入 {@link WxConfigStorage} 的实现 | * 注入 {@link WxConfigStorage} 的实现 | ||||
| * @param wxConfigProvider | * @param wxConfigProvider | ||||
| @@ -3,9 +3,11 @@ package chanjarster.weixin.api; | |||||
| import java.io.File; | import java.io.File; | ||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.InputStream; | import java.io.InputStream; | ||||
| import java.io.StringReader; | |||||
| import java.security.MessageDigest; | import java.security.MessageDigest; | ||||
| import java.text.MessageFormat; | import java.text.MessageFormat; | ||||
| import java.util.Arrays; | import java.util.Arrays; | ||||
| import java.util.List; | |||||
| import java.util.UUID; | import java.util.UUID; | ||||
| import java.util.concurrent.atomic.AtomicBoolean; | import java.util.concurrent.atomic.AtomicBoolean; | ||||
| @@ -36,6 +38,12 @@ import chanjarster.weixin.util.http.MediaUploadRequestExecutor; | |||||
| import chanjarster.weixin.util.http.RequestExecutor; | import chanjarster.weixin.util.http.RequestExecutor; | ||||
| import chanjarster.weixin.util.http.SimpleGetRequestExecutor; | import chanjarster.weixin.util.http.SimpleGetRequestExecutor; | ||||
| import chanjarster.weixin.util.http.SimplePostRequestExecutor; | import chanjarster.weixin.util.http.SimplePostRequestExecutor; | ||||
| import chanjarster.weixin.util.json.WxGsonBuilder; | |||||
| import com.google.gson.JsonElement; | |||||
| import com.google.gson.internal.Streams; | |||||
| import com.google.gson.reflect.TypeToken; | |||||
| import com.google.gson.stream.JsonReader; | |||||
| public class WxServiceImpl implements WxService { | public class WxServiceImpl implements WxService { | ||||
| @@ -190,6 +198,17 @@ public class WxServiceImpl implements WxService { | |||||
| 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); | ||||
| } | } | ||||
| public List<WxGroup> groupGet() throws WxErrorException { | |||||
| String url = "https://api.weixin.qq.com/cgi-bin/groups/get"; | |||||
| String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | |||||
| /* | |||||
| * 操蛋的微信API,创建时返回的是 { group : { id : ..., name : ...} } | |||||
| * 查询时返回的是 { groups : [ { id : ..., name : ..., count : ... }, ... ] } | |||||
| */ | |||||
| JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||||
| return WxGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("groups"), new TypeToken<List<WxGroup>>(){}.getType()); | |||||
| } | |||||
| /** | /** | ||||
| * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 | * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 | ||||
| @@ -39,7 +39,10 @@ public class WxGroupGsonAdapter implements JsonSerializer<WxGroup>, JsonDeserial | |||||
| public WxGroup deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | public WxGroup deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||||
| WxGroup group = new WxGroup(); | WxGroup group = new WxGroup(); | ||||
| JsonObject groupJson = json.getAsJsonObject().get("group").getAsJsonObject(); | |||||
| JsonObject groupJson = json.getAsJsonObject(); | |||||
| if (json.getAsJsonObject().get("group") != null) { | |||||
| groupJson = json.getAsJsonObject().get("group").getAsJsonObject(); | |||||
| } | |||||
| if (groupJson.get("name") != null && !groupJson.get("name").isJsonNull()) { | if (groupJson.get("name") != null && !groupJson.get("name").isJsonNull()) { | ||||
| group.setName(GsonHelper.getAsString(groupJson.get("name"))); | group.setName(GsonHelper.getAsString(groupJson.get("name"))); | ||||
| } | } | ||||
| @@ -1,5 +1,7 @@ | |||||
| package chanjarster.weixin.api; | package chanjarster.weixin.api; | ||||
| import java.util.List; | |||||
| import org.testng.Assert; | import org.testng.Assert; | ||||
| import org.testng.annotations.Guice; | import org.testng.annotations.Guice; | ||||
| import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
| @@ -21,9 +23,19 @@ public class WxGroupAPITest { | |||||
| @Inject | @Inject | ||||
| protected WxServiceImpl wxService; | protected WxServiceImpl wxService; | ||||
| public void testCreateMenu() throws WxErrorException { | |||||
| public void testGroupCreate() throws WxErrorException { | |||||
| WxGroup res = wxService.groupCreate("测试分组1"); | WxGroup res = wxService.groupCreate("测试分组1"); | ||||
| Assert.assertEquals(res.getName(), "测试分组1"); | Assert.assertEquals(res.getName(), "测试分组1"); | ||||
| } | } | ||||
| @Test(dependsOnMethods="testGroupCreate") | |||||
| public void testGroupGet() throws WxErrorException { | |||||
| List<WxGroup> groupList = wxService.groupGet(); | |||||
| Assert.assertNotNull(groupList); | |||||
| Assert.assertTrue(groupList.size() > 0); | |||||
| for (WxGroup g : groupList) { | |||||
| Assert.assertNotNull(g.getName()); | |||||
| } | |||||
| } | |||||
| } | } | ||||