@@ -10,7 +10,7 @@ import java.util.List; | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
* 标签管理接口 | |||||
* 标签管理接口. | |||||
* Created by BinaryWang on 2017/6/24. | * Created by BinaryWang on 2017/6/24. | ||||
* </pre> | * </pre> | ||||
* | * | ||||
@@ -18,14 +18,14 @@ import java.util.List; | |||||
*/ | */ | ||||
public interface WxCpTagService { | public interface WxCpTagService { | ||||
/** | /** | ||||
* 创建标签 | |||||
* 创建标签. | |||||
* | * | ||||
* @param tagName 标签名 | * @param tagName 标签名 | ||||
*/ | */ | ||||
String create(String tagName) throws WxErrorException; | String create(String tagName) throws WxErrorException; | ||||
/** | /** | ||||
* 更新标签 | |||||
* 更新标签. | |||||
* | * | ||||
* @param tagId 标签id | * @param tagId 标签id | ||||
* @param tagName 标签名 | * @param tagName 标签名 | ||||
@@ -33,46 +33,48 @@ public interface WxCpTagService { | |||||
void update(String tagId, String tagName) throws WxErrorException; | void update(String tagId, String tagName) throws WxErrorException; | ||||
/** | /** | ||||
* 删除标签 | |||||
* 删除标签. | |||||
* | * | ||||
* @param tagId 标签id | * @param tagId 标签id | ||||
*/ | */ | ||||
void delete(String tagId) throws WxErrorException; | void delete(String tagId) throws WxErrorException; | ||||
/** | /** | ||||
* 获得标签列表 | |||||
* 获得标签列表. | |||||
*/ | */ | ||||
List<WxCpTag> listAll() throws WxErrorException; | List<WxCpTag> listAll() throws WxErrorException; | ||||
/** | /** | ||||
* 获取标签成员 | |||||
* 获取标签成员. | |||||
* | * | ||||
* @param tagId 标签ID | * @param tagId 标签ID | ||||
*/ | */ | ||||
List<WxCpUser> listUsersByTagId(String tagId) throws WxErrorException; | List<WxCpUser> listUsersByTagId(String tagId) throws WxErrorException; | ||||
/** | /** | ||||
* 增加标签成员 | |||||
* @param tagId 标签id | |||||
* @param userIds 用户ID 列表 | |||||
* 增加标签成员. | |||||
* | |||||
* @param tagId 标签id | |||||
* @param userIds 用户ID 列表 | |||||
* @param partyIds 企业部门ID列表 | |||||
*/ | */ | ||||
WxCpTagAddOrRemoveUsersResult addUsers2Tag(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException; | WxCpTagAddOrRemoveUsersResult addUsers2Tag(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException; | ||||
/** | /** | ||||
* 移除标签成员 | |||||
* @param tagId 标签id | |||||
* @param userIds 用户id列表 | |||||
* 移除标签成员. | |||||
* | |||||
* @param tagId 标签id | |||||
* @param userIds 用户id列表 | |||||
* @param partyIds 企业部门ID列表 | |||||
*/ | */ | ||||
WxCpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds) throws WxErrorException; | |||||
WxCpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException; | |||||
/** | /** | ||||
* 获取标签成员 | |||||
* 获取标签成员. | |||||
* 对应: http://qydev.weixin.qq.com/wiki/index.php?title=管理标签 中的get接口 | * 对应: http://qydev.weixin.qq.com/wiki/index.php?title=管理标签 中的get接口 | ||||
* | * | ||||
* @param tagId | |||||
* @return | |||||
* @throws WxErrorException | |||||
* @param tagId 标签id | |||||
*/ | */ | ||||
WxCpTagGetResult get(String tagId) throws WxErrorException; | WxCpTagGetResult get(String tagId) throws WxErrorException; | ||||
@@ -83,6 +83,22 @@ public class WxCpTagServiceImpl implements WxCpTagService { | |||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers"; | String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers"; | ||||
JsonObject jsonObject = new JsonObject(); | JsonObject jsonObject = new JsonObject(); | ||||
jsonObject.addProperty("tagid", tagId); | jsonObject.addProperty("tagid", tagId); | ||||
this.addUserIdsAndPartyIdsToJson(userIds, partyIds, jsonObject); | |||||
return WxCpTagAddOrRemoveUsersResult.fromJson(this.mainService.post(url, jsonObject.toString())); | |||||
} | |||||
@Override | |||||
public WxCpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException { | |||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers"; | |||||
JsonObject jsonObject = new JsonObject(); | |||||
jsonObject.addProperty("tagid", tagId); | |||||
this.addUserIdsAndPartyIdsToJson(userIds, partyIds, jsonObject); | |||||
return WxCpTagAddOrRemoveUsersResult.fromJson(this.mainService.post(url, jsonObject.toString())); | |||||
} | |||||
private void addUserIdsAndPartyIdsToJson(List<String> userIds, List<String> partyIds, JsonObject jsonObject) { | |||||
if (userIds != null) { | if (userIds != null) { | ||||
JsonArray jsonArray = new JsonArray(); | JsonArray jsonArray = new JsonArray(); | ||||
for (String userId : userIds) { | for (String userId : userIds) { | ||||
@@ -90,6 +106,7 @@ public class WxCpTagServiceImpl implements WxCpTagService { | |||||
} | } | ||||
jsonObject.add("userlist", jsonArray); | jsonObject.add("userlist", jsonArray); | ||||
} | } | ||||
if (partyIds != null) { | if (partyIds != null) { | ||||
JsonArray jsonArray = new JsonArray(); | JsonArray jsonArray = new JsonArray(); | ||||
for (String userId : partyIds) { | for (String userId : partyIds) { | ||||
@@ -97,22 +114,6 @@ public class WxCpTagServiceImpl implements WxCpTagService { | |||||
} | } | ||||
jsonObject.add("partylist", jsonArray); | jsonObject.add("partylist", jsonArray); | ||||
} | } | ||||
return WxCpTagAddOrRemoveUsersResult.fromJson(this.mainService.post(url, jsonObject.toString())); | |||||
} | |||||
@Override | |||||
public WxCpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds) throws WxErrorException { | |||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers"; | |||||
JsonObject jsonObject = new JsonObject(); | |||||
jsonObject.addProperty("tagid", tagId); | |||||
JsonArray jsonArray = new JsonArray(); | |||||
for (String userId : userIds) { | |||||
jsonArray.add(new JsonPrimitive(userId)); | |||||
} | |||||
jsonObject.add("userlist", jsonArray); | |||||
return WxCpTagAddOrRemoveUsersResult.fromJson(this.mainService.post(url, jsonObject.toString())); | |||||
} | } | ||||
@Override | @Override | ||||
@@ -10,13 +10,15 @@ import me.chanjar.weixin.cp.bean.WxCpTag; | |||||
import me.chanjar.weixin.cp.bean.WxCpTagAddOrRemoveUsersResult; | import me.chanjar.weixin.cp.bean.WxCpTagAddOrRemoveUsersResult; | ||||
import me.chanjar.weixin.cp.bean.WxCpTagGetResult; | import me.chanjar.weixin.cp.bean.WxCpTagGetResult; | ||||
import me.chanjar.weixin.cp.bean.WxCpUser; | import me.chanjar.weixin.cp.bean.WxCpUser; | ||||
import org.testng.annotations.*; | |||||
import org.testng.annotations.Guice; | |||||
import org.testng.annotations.Test; | |||||
import java.util.List; | import java.util.List; | ||||
import static org.mockito.Mockito.mock; | import static org.mockito.Mockito.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
import static org.testng.Assert.*; | |||||
import static org.testng.Assert.assertEquals; | |||||
import static org.testng.Assert.assertNotEquals; | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
@@ -68,7 +70,7 @@ public class WxCpTagServiceImplTest { | |||||
@Test(dependsOnMethods = {"testListUsersByTagId", "testAddUsers2Tag", "testListAll", "testUpdate", "testCreate"}) | @Test(dependsOnMethods = {"testListUsersByTagId", "testAddUsers2Tag", "testListAll", "testUpdate", "testCreate"}) | ||||
public void testRemoveUsersFromTag() throws Exception { | public void testRemoveUsersFromTag() throws Exception { | ||||
List<String> userIds = Splitter.on("|").splitToList(this.configStorage.getUserId()); | List<String> userIds = Splitter.on("|").splitToList(this.configStorage.getUserId()); | ||||
WxCpTagAddOrRemoveUsersResult result = this.wxService.getTagService().removeUsersFromTag(this.tagId, userIds); | |||||
WxCpTagAddOrRemoveUsersResult result = this.wxService.getTagService().removeUsersFromTag(this.tagId, userIds, null); | |||||
assertEquals(result.getErrCode(), Integer.valueOf(0)); | assertEquals(result.getErrCode(), Integer.valueOf(0)); | ||||
} | } | ||||