| @@ -1,10 +1,10 @@ | |||
| package me.chanjar.weixin.cp.api; | |||
| import java.util.List; | |||
| import me.chanjar.weixin.common.error.WxErrorException; | |||
| import me.chanjar.weixin.cp.bean.WxCpDepart; | |||
| import java.util.List; | |||
| /** | |||
| * <pre> | |||
| * 部门管理接口 | |||
| @@ -17,42 +17,47 @@ public interface WxCpDepartmentService { | |||
| /** | |||
| * <pre> | |||
| * 部门管理接口 - 创建部门 | |||
| * 部门管理接口 - 创建部门. | |||
| * 最多支持创建500个部门 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口 | |||
| * </pre> | |||
| * | |||
| * @param depart 部门 | |||
| * @return 部门id | |||
| * @throws WxErrorException 异常 | |||
| */ | |||
| Integer create(WxCpDepart depart) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 部门管理接口 - 查询部门 | |||
| * 部门管理接口 - 查询部门. | |||
| * 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E5.88.97.E8.A1.A8 | |||
| * </pre> | |||
| * | |||
| * @param id 部门id。获取指定部门及其下的子部门。非必需,可为null | |||
| * @throws WxErrorException 异常 | |||
| */ | |||
| List<WxCpDepart> list(Integer id) throws WxErrorException; | |||
| List<WxCpDepart> list(Long id) 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 异常 | |||
| */ | |||
| void update(WxCpDepart group) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 部门管理接口 - 删除部门 | |||
| * 部门管理接口 - 删除部门. | |||
| * </pre> | |||
| * | |||
| * @param departId 部门id | |||
| * @throws WxErrorException 异常 | |||
| */ | |||
| void delete(Integer departId) throws WxErrorException; | |||
| void delete(Long departId) throws WxErrorException; | |||
| } | |||
| @@ -1,5 +1,7 @@ | |||
| package me.chanjar.weixin.cp.api.impl; | |||
| import java.util.List; | |||
| import com.google.gson.JsonElement; | |||
| import com.google.gson.JsonParser; | |||
| import com.google.gson.reflect.TypeToken; | |||
| @@ -10,8 +12,6 @@ import me.chanjar.weixin.cp.api.WxCpService; | |||
| import me.chanjar.weixin.cp.bean.WxCpDepart; | |||
| import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
| import java.util.List; | |||
| /** | |||
| * <pre> | |||
| * 部门管理接口 | |||
| @@ -42,13 +42,13 @@ public class WxCpDepartmentServiceImpl implements WxCpDepartmentService { | |||
| } | |||
| @Override | |||
| public void delete(Integer departId) throws WxErrorException { | |||
| public void delete(Long departId) throws WxErrorException { | |||
| String url = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?id=" + departId; | |||
| this.mainService.get(url, null); | |||
| } | |||
| @Override | |||
| public List<WxCpDepart> list(Integer id) throws WxErrorException { | |||
| public List<WxCpDepart> list(Long id) throws WxErrorException { | |||
| String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list"; | |||
| if (id != null) { | |||
| url += "?id=" + id; | |||
| @@ -1,10 +1,10 @@ | |||
| package me.chanjar.weixin.cp.bean; | |||
| import java.io.Serializable; | |||
| import lombok.Data; | |||
| import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
| import java.io.Serializable; | |||
| /** | |||
| * 微信部门. | |||
| * | |||
| @@ -14,9 +14,9 @@ import java.io.Serializable; | |||
| public class WxCpDepart implements Serializable { | |||
| private static final long serialVersionUID = -5028321625140879571L; | |||
| private Integer id; | |||
| private Long id; | |||
| private String name; | |||
| private Integer parentId; | |||
| private Long parentId; | |||
| private Long order; | |||
| public static WxCpDepart fromJson(String json) { | |||
| @@ -8,31 +8,43 @@ | |||
| */ | |||
| package me.chanjar.weixin.cp.util.json; | |||
| import com.google.gson.*; | |||
| import java.lang.reflect.Type; | |||
| import com.google.gson.JsonDeserializationContext; | |||
| import com.google.gson.JsonDeserializer; | |||
| import com.google.gson.JsonElement; | |||
| import com.google.gson.JsonObject; | |||
| import com.google.gson.JsonParseException; | |||
| import com.google.gson.JsonSerializationContext; | |||
| import com.google.gson.JsonSerializer; | |||
| import me.chanjar.weixin.common.util.json.GsonHelper; | |||
| import me.chanjar.weixin.cp.bean.WxCpDepart; | |||
| import java.lang.reflect.Type; | |||
| /** | |||
| * WxCpDepart的gson适配器. | |||
| * | |||
| * @author Daniel Qian | |||
| */ | |||
| public class WxCpDepartGsonAdapter implements JsonSerializer<WxCpDepart>, JsonDeserializer<WxCpDepart> { | |||
| private static final String ID = "id"; | |||
| private static final String NAME = "name"; | |||
| private static final String PARENT_ID = "parentid"; | |||
| private static final String ORDER = "order"; | |||
| @Override | |||
| public JsonElement serialize(WxCpDepart group, Type typeOfSrc, JsonSerializationContext context) { | |||
| JsonObject json = new JsonObject(); | |||
| if (group.getId() != null) { | |||
| json.addProperty("id", group.getId()); | |||
| json.addProperty(ID, group.getId()); | |||
| } | |||
| if (group.getName() != null) { | |||
| json.addProperty("name", group.getName()); | |||
| json.addProperty(NAME, group.getName()); | |||
| } | |||
| if (group.getParentId() != null) { | |||
| json.addProperty("parentid", group.getParentId()); | |||
| json.addProperty(PARENT_ID, group.getParentId()); | |||
| } | |||
| if (group.getOrder() != null) { | |||
| json.addProperty("order", String.valueOf(group.getOrder())); | |||
| json.addProperty(ORDER, String.valueOf(group.getOrder())); | |||
| } | |||
| return json; | |||
| } | |||
| @@ -42,17 +54,17 @@ public class WxCpDepartGsonAdapter implements JsonSerializer<WxCpDepart>, JsonDe | |||
| throws JsonParseException { | |||
| WxCpDepart depart = new WxCpDepart(); | |||
| JsonObject departJson = json.getAsJsonObject(); | |||
| if (departJson.get("id") != null && !departJson.get("id").isJsonNull()) { | |||
| depart.setId(GsonHelper.getAsInteger(departJson.get("id"))); | |||
| if (departJson.get(ID) != null && !departJson.get(ID).isJsonNull()) { | |||
| depart.setId(GsonHelper.getAsLong(departJson.get(ID))); | |||
| } | |||
| if (departJson.get("name") != null && !departJson.get("name").isJsonNull()) { | |||
| depart.setName(GsonHelper.getAsString(departJson.get("name"))); | |||
| if (departJson.get(NAME) != null && !departJson.get(NAME).isJsonNull()) { | |||
| depart.setName(GsonHelper.getAsString(departJson.get(NAME))); | |||
| } | |||
| if (departJson.get("order") != null && !departJson.get("order").isJsonNull()) { | |||
| depart.setOrder(GsonHelper.getAsLong(departJson.get("order"))); | |||
| if (departJson.get(ORDER) != null && !departJson.get(ORDER).isJsonNull()) { | |||
| depart.setOrder(GsonHelper.getAsLong(departJson.get(ORDER))); | |||
| } | |||
| if (departJson.get("parentid") != null && !departJson.get("parentid").isJsonNull()) { | |||
| depart.setParentId(GsonHelper.getAsInteger(departJson.get("parentid"))); | |||
| if (departJson.get(PARENT_ID) != null && !departJson.get(PARENT_ID).isJsonNull()) { | |||
| depart.setParentId(GsonHelper.getAsLong(departJson.get(PARENT_ID))); | |||
| } | |||
| return depart; | |||
| } | |||
| @@ -1,15 +1,15 @@ | |||
| package me.chanjar.weixin.cp.api.impl; | |||
| import java.util.List; | |||
| import org.testng.annotations.*; | |||
| import com.google.inject.Inject; | |||
| import me.chanjar.weixin.cp.api.ApiTestModule; | |||
| import me.chanjar.weixin.cp.api.WxCpService; | |||
| import me.chanjar.weixin.cp.bean.WxCpDepart; | |||
| import org.testng.annotations.*; | |||
| import java.util.List; | |||
| import static org.assertj.core.api.Assertions.assertThat; | |||
| import static org.testng.Assert.*; | |||
| /** | |||
| * <pre> | |||
| @@ -29,7 +29,7 @@ public class WxCpDepartmentServiceImplTest { | |||
| public void testCreate() throws Exception { | |||
| WxCpDepart cpDepart = new WxCpDepart(); | |||
| cpDepart.setName("子部门" + System.currentTimeMillis()); | |||
| cpDepart.setParentId(1); | |||
| cpDepart.setParentId(1L); | |||
| cpDepart.setOrder(1L); | |||
| Integer departId = this.wxCpService.getDepartmentService().create(cpDepart); | |||
| System.out.println(departId); | |||
| @@ -45,7 +45,7 @@ public class WxCpDepartmentServiceImplTest { | |||
| } | |||
| @Test(dataProvider = "departIds") | |||
| public void testList(Integer id) throws Exception { | |||
| public void testList(Long id) throws Exception { | |||
| System.out.println("=================获取部门"); | |||
| List<WxCpDepart> departList = this.wxCpService.getDepartmentService().list(id); | |||
| assertThat(departList).isNotEmpty(); | |||