diff --git a/src/main/java/chanjarster/weixin/api/WxService.java b/src/main/java/chanjarster/weixin/api/WxService.java index c2f1ee38..b7128987 100644 --- a/src/main/java/chanjarster/weixin/api/WxService.java +++ b/src/main/java/chanjarster/weixin/api/WxService.java @@ -232,7 +232,19 @@ public interface WxService { * @param to_groupid 移动到的分组id * @throws WxErrorException */ - public void groupMoveUser(String openid, long to_groupid) throws WxErrorException; + public void userUpdateGroup(String openid, long to_groupid) throws WxErrorException; + + /** + *
+   * 设置用户备注名接口
+   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=设置用户备注名接口
+   * 
+   * 
+ * @param openid 用户openid + * @param remark 备注名 + * @throws WxErrorException + */ + public void userUpdateRemark(String openid, String remark) throws WxErrorException; /** * 注入 {@link WxConfigStorage} 的实现 diff --git a/src/main/java/chanjarster/weixin/api/WxServiceImpl.java b/src/main/java/chanjarster/weixin/api/WxServiceImpl.java index f414e0c4..8c462cc3 100644 --- a/src/main/java/chanjarster/weixin/api/WxServiceImpl.java +++ b/src/main/java/chanjarster/weixin/api/WxServiceImpl.java @@ -42,6 +42,7 @@ import chanjarster.weixin.util.json.GsonHelper; import chanjarster.weixin.util.json.WxGsonBuilder; import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import com.google.gson.internal.Streams; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; @@ -196,10 +197,15 @@ public class WxServiceImpl implements WxService { public WxGroup groupCreate(String name) throws WxErrorException { String url = "https://api.weixin.qq.com/cgi-bin/groups/create"; + JsonObject json = new JsonObject(); + JsonObject groupJson = new JsonObject(); + json.add("group", groupJson); + groupJson.addProperty("name", name); + String responseContent = execute( new SimplePostRequestExecutor(), url, - MessageFormat.format("'{'\"group\":'{'\"name\":\"{0}\"}}", name)); + json.toString()); return WxGroup.fromJson(responseContent); } @@ -226,9 +232,20 @@ public class WxServiceImpl implements WxService { execute(new SimplePostRequestExecutor(), url, group.toJson()); } - public void groupMoveUser(String openid, long to_groupid) throws WxErrorException { + public void userUpdateGroup(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)); + JsonObject json = new JsonObject(); + json.addProperty("openid", openid); + json.addProperty("to_groupid", to_groupid); + execute(new SimplePostRequestExecutor(), url, json.toString()); + } + + public void userUpdateRemark(String openid, String remark) throws WxErrorException { + String url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark"; + JsonObject json = new JsonObject(); + json.addProperty("openid", openid); + json.addProperty("remark", remark); + execute(new SimplePostRequestExecutor(), url, json.toString()); } /** diff --git a/src/test/java/chanjarster/weixin/api/WxGroupAPITest.java b/src/test/java/chanjarster/weixin/api/WxGroupAPITest.java index d592c5e0..d3ab5699 100644 --- a/src/test/java/chanjarster/weixin/api/WxGroupAPITest.java +++ b/src/test/java/chanjarster/weixin/api/WxGroupAPITest.java @@ -58,6 +58,6 @@ public class WxGroupAPITest { @Test(dependsOnMethods={"testGroupGet", "testGroupCreate"}) public void getGroupMoveUser() throws WxErrorException { WxXmlConfigStorage configStorage = (WxXmlConfigStorage) wxService.wxConfigStorage; - wxService.groupMoveUser(configStorage.getOpenId(), group.getId()); + wxService.userUpdateGroup(configStorage.getOpenId(), group.getId()); } } diff --git a/src/test/java/chanjarster/weixin/api/WxMassMessageAPITest.java b/src/test/java/chanjarster/weixin/api/WxMassMessageAPITest.java index a5fca2ec..77031b68 100644 --- a/src/test/java/chanjarster/weixin/api/WxMassMessageAPITest.java +++ b/src/test/java/chanjarster/weixin/api/WxMassMessageAPITest.java @@ -32,7 +32,7 @@ public class WxMassMessageAPITest { @Inject protected WxServiceImpl wxService; - @Test(enabled = false) + @Test public void testSendMassTextByOpenIds() throws WxErrorException { // 发送群发消息 WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage; @@ -46,7 +46,7 @@ public class WxMassMessageAPITest { Assert.assertNotNull(massResult.getMsg_id()); } - @Test(enabled = true, dataProvider="massMessages") + @Test(dataProvider="massMessages") public void testSendMassByOpenIds(String massMsgType, String mediaId) throws WxErrorException, IOException { // 发送群发消息 WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage; diff --git a/src/test/java/chanjarster/weixin/api/WxUserAPITest.java b/src/test/java/chanjarster/weixin/api/WxUserAPITest.java new file mode 100644 index 00000000..4875055f --- /dev/null +++ b/src/test/java/chanjarster/weixin/api/WxUserAPITest.java @@ -0,0 +1,29 @@ +package chanjarster.weixin.api; + +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +import chanjarster.weixin.api.ApiTestModule.WxXmlConfigStorage; +import chanjarster.weixin.exception.WxErrorException; + +import com.google.inject.Inject; + +/** + * 测试用户相关的接口 + * @author chanjarster + * + */ +@Test(groups = "userAPI", dependsOnGroups = { "baseAPI" }) +@Guice(modules = ApiTestModule.class) +public class WxUserAPITest { + + @Inject + protected WxServiceImpl wxService; + + @Test + public void testUserUpdateRemark() throws WxErrorException { + WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage; + wxService.userUpdateRemark(configProvider.getOpenId(), "测试备注名"); + } + +}