@@ -3,9 +3,12 @@ package me.chanjar.weixin.cp.api; | |||||
import me.chanjar.weixin.common.error.WxErrorException; | import me.chanjar.weixin.common.error.WxErrorException; | ||||
import me.chanjar.weixin.cp.bean.WxCpAgent; | import me.chanjar.weixin.cp.bean.WxCpAgent; | ||||
import java.util.List; | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
* 管理企业号应用 | * 管理企业号应用 | ||||
* 文档地址:https://work.weixin.qq.com/api/doc#10087 | |||||
* Created by huansinho on 2018/4/13. | * Created by huansinho on 2018/4/13. | ||||
* </pre> | * </pre> | ||||
* | * | ||||
@@ -17,7 +20,7 @@ public interface WxCpAgentService { | |||||
* <pre> | * <pre> | ||||
* 获取企业号应用信息 | * 获取企业号应用信息 | ||||
* 该API用于获取企业号某个应用的基本信息,包括头像、昵称、帐号类型、认证类型、可见范围等信息 | * 该API用于获取企业号某个应用的基本信息,包括头像、昵称、帐号类型、认证类型、可见范围等信息 | ||||
* 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=获取企业号应用 | |||||
* 详情请见: https://work.weixin.qq.com/api/doc#10087 | |||||
* </pre> | * </pre> | ||||
* | * | ||||
* @param agentId 企业应用的id | * @param agentId 企业应用的id | ||||
@@ -25,4 +28,25 @@ public interface WxCpAgentService { | |||||
*/ | */ | ||||
WxCpAgent get(Integer agentId) throws WxErrorException; | WxCpAgent get(Integer agentId) throws WxErrorException; | ||||
/** | |||||
* <pre> | |||||
* 设置应用. | |||||
* 仅企业可调用,可设置当前凭证对应的应用;第三方不可调用。 | |||||
* 详情请见: https://work.weixin.qq.com/api/doc#10088 | |||||
* </pre> | |||||
* | |||||
* @param agentInfo 应用信息 | |||||
*/ | |||||
void set(WxCpAgent agentInfo) throws WxErrorException; | |||||
/** | |||||
* <pre> | |||||
* 获取应用列表. | |||||
* 企业仅可获取当前凭证对应的应用;第三方仅可获取被授权的应用。 | |||||
* 详情请见: https://work.weixin.qq.com/api/doc#11214 | |||||
* </pre> | |||||
* | |||||
*/ | |||||
List<WxCpAgent> list() throws WxErrorException; | |||||
} | } |
@@ -1,9 +1,17 @@ | |||||
package me.chanjar.weixin.cp.api.impl; | package me.chanjar.weixin.cp.api.impl; | ||||
import com.google.gson.Gson; | |||||
import com.google.gson.JsonObject; | |||||
import com.google.gson.JsonParser; | |||||
import com.google.gson.reflect.TypeToken; | |||||
import me.chanjar.weixin.common.error.WxError; | |||||
import me.chanjar.weixin.common.error.WxErrorException; | import me.chanjar.weixin.common.error.WxErrorException; | ||||
import me.chanjar.weixin.cp.api.WxCpAgentService; | import me.chanjar.weixin.cp.api.WxCpAgentService; | ||||
import me.chanjar.weixin.cp.api.WxCpService; | import me.chanjar.weixin.cp.api.WxCpService; | ||||
import me.chanjar.weixin.cp.bean.WxCpAgent; | import me.chanjar.weixin.cp.bean.WxCpAgent; | ||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||||
import java.util.List; | |||||
/** | /** | ||||
@@ -15,6 +23,8 @@ import me.chanjar.weixin.cp.bean.WxCpAgent; | |||||
* @author <a href="https://github.com/huansinho">huansinho</a> | * @author <a href="https://github.com/huansinho">huansinho</a> | ||||
*/ | */ | ||||
public class WxCpAgentServiceImpl implements WxCpAgentService { | public class WxCpAgentServiceImpl implements WxCpAgentService { | ||||
private static final JsonParser JSON_PARSER = new JsonParser(); | |||||
private WxCpService mainService; | private WxCpService mainService; | ||||
public WxCpAgentServiceImpl(WxCpService mainService) { | public WxCpAgentServiceImpl(WxCpService mainService) { | ||||
@@ -32,4 +42,27 @@ public class WxCpAgentServiceImpl implements WxCpAgentService { | |||||
return WxCpAgent.fromJson(responseContent); | return WxCpAgent.fromJson(responseContent); | ||||
} | } | ||||
@Override | |||||
public void set(WxCpAgent agentInfo) throws WxErrorException { | |||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/set"; | |||||
String responseContent = this.mainService.post(url, agentInfo.toJson()); | |||||
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); | |||||
if (jsonObject.get("errcode").getAsInt() != 0) { | |||||
throw new WxErrorException(WxError.fromJson(responseContent)); | |||||
} | |||||
} | |||||
@Override | |||||
public List<WxCpAgent> list() throws WxErrorException { | |||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/list"; | |||||
String responseContent = this.mainService.get(url, null); | |||||
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); | |||||
if (jsonObject.get("errcode").getAsInt() != 0) { | |||||
throw new WxErrorException(WxError.fromJson(responseContent)); | |||||
} | |||||
return WxCpGsonBuilder.create().fromJson(jsonObject.get("agentlist").toString(), new TypeToken<List<WxCpAgent>>() { | |||||
}.getType()); | |||||
} | |||||
} | } |
@@ -1,7 +1,10 @@ | |||||
package me.chanjar.weixin.cp.bean; | package me.chanjar.weixin.cp.bean; | ||||
import com.google.gson.annotations.SerializedName; | import com.google.gson.annotations.SerializedName; | ||||
import lombok.AllArgsConstructor; | |||||
import lombok.Builder; | |||||
import lombok.Data; | import lombok.Data; | ||||
import lombok.NoArgsConstructor; | |||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | ||||
import java.io.Serializable; | import java.io.Serializable; | ||||
@@ -16,6 +19,9 @@ import java.util.List; | |||||
* @author <a href="https://github.com/huansinho">huansinho</a> | * @author <a href="https://github.com/huansinho">huansinho</a> | ||||
*/ | */ | ||||
@Data | @Data | ||||
@Builder | |||||
@NoArgsConstructor | |||||
@AllArgsConstructor | |||||
public class WxCpAgent implements Serializable { | public class WxCpAgent implements Serializable { | ||||
private static final long serialVersionUID = 5002894979081127234L; | private static final long serialVersionUID = 5002894979081127234L; | ||||
@@ -34,6 +40,9 @@ public class WxCpAgent implements Serializable { | |||||
@SerializedName("square_logo_url") | @SerializedName("square_logo_url") | ||||
private String squareLogoUrl; | private String squareLogoUrl; | ||||
@SerializedName("logo_mediaid") | |||||
private String logoMediaId; | |||||
@SerializedName("description") | @SerializedName("description") | ||||
private String description; | private String description; | ||||
@@ -41,7 +50,7 @@ public class WxCpAgent implements Serializable { | |||||
private Users allowUserInfos; | private Users allowUserInfos; | ||||
@SerializedName("allow_partys") | @SerializedName("allow_partys") | ||||
private Partys allowParties; | |||||
private Parties allowParties; | |||||
@SerializedName("allow_tags") | @SerializedName("allow_tags") | ||||
private Tags allowTags; | private Tags allowTags; | ||||
@@ -82,7 +91,7 @@ public class WxCpAgent implements Serializable { | |||||
} | } | ||||
@Data | @Data | ||||
public class Partys { | |||||
public class Parties { | |||||
@SerializedName("partyid") | @SerializedName("partyid") | ||||
private List<Integer> partyIds = null; | private List<Integer> partyIds = null; | ||||
} | } | ||||
@@ -1,13 +1,21 @@ | |||||
package me.chanjar.weixin.cp.api.impl; | package me.chanjar.weixin.cp.api.impl; | ||||
import com.google.inject.Inject; | |||||
import me.chanjar.weixin.common.error.WxErrorException; | |||||
import me.chanjar.weixin.cp.api.ApiTestModule; | |||||
import me.chanjar.weixin.cp.api.WxCpAgentService; | import me.chanjar.weixin.cp.api.WxCpAgentService; | ||||
import me.chanjar.weixin.cp.api.WxCpService; | import me.chanjar.weixin.cp.api.WxCpService; | ||||
import me.chanjar.weixin.cp.bean.WxCpAgent; | import me.chanjar.weixin.cp.bean.WxCpAgent; | ||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Guice; | |||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
import java.util.List; | |||||
import static org.assertj.core.api.Assertions.assertThat; | |||||
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.assertEquals; | |||||
/** | /** | ||||
@@ -18,24 +26,64 @@ import static org.mockito.Mockito.when; | |||||
* | * | ||||
* @author <a href="https://github.com/huansinho">huansinho</a> | * @author <a href="https://github.com/huansinho">huansinho</a> | ||||
*/ | */ | ||||
@Guice(modules = ApiTestModule.class) | |||||
public class WxCpAgentServiceImplTest { | public class WxCpAgentServiceImplTest { | ||||
protected WxCpService wxService = mock(WxCpService.class); | |||||
@Inject | |||||
private WxCpService wxCpService; | |||||
@Test | @Test | ||||
public void testGet() throws Exception { | public void testGet() throws Exception { | ||||
String returnJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"agentid\": 9,\"name\": \"测试应用\",\"square_logo_url\": \"http://wx.qlogo.cn/mmhead/alksjf;lasdjf;lasjfuodiuj3rj2o34j/0\",\"description\": \"这是一个企业号应用\",\"allow_userinfos\": {\"user\": [{\"userid\": \"0009854\"}, {\"userid\": \"1723\"}, {\"userid\": \"5625\"}]},\"allow_partys\": {\"partyid\": [42762742]},\"allow_tags\": {\"tagid\": [23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7]},\"close\": 0,\"redirect_domain\": \"weixin.com.cn\",\"report_location_flag\": 0,\"isreportenter\": 0,\"home_url\": \"\"}"; | |||||
when(wxService.get("https://qyapi.weixin.qq.com/cgi-bin/agent/get?agentid=9", null)).thenReturn(returnJson); | |||||
when(wxService.getAgentService()).thenReturn(new WxCpAgentServiceImpl(wxService)); | |||||
final Integer agentId = this.wxCpService.getWxCpConfigStorage().getAgentId(); | |||||
WxCpAgent wxCpAgent = this.wxCpService.getAgentService().get(agentId); | |||||
assertThat(wxCpAgent.getAgentId()).isEqualTo(agentId); | |||||
assertThat(wxCpAgent.getAllowUserInfos().getUsers().toArray()).isNotEmpty(); | |||||
assertThat(wxCpAgent.getAllowParties().getPartyIds().toArray()).isNotEmpty(); | |||||
assertThat(wxCpAgent.getAllowTags().getTagIds().toArray()).isNotEmpty(); | |||||
} | |||||
@Test | |||||
public void testSet() throws WxErrorException { | |||||
final Integer agentId = this.wxCpService.getWxCpConfigStorage().getAgentId(); | |||||
this.wxCpService.getAgentService().set(WxCpAgent.builder() | |||||
.description("abcddd") | |||||
.logoMediaId("aaaaaaaaaaaaaa") | |||||
.agentId(agentId) | |||||
.build()); | |||||
} | |||||
@Test | |||||
public void testList() throws WxErrorException { | |||||
List<WxCpAgent> list = this.wxCpService.getAgentService().list(); | |||||
assertThat(list).isNotEmpty(); | |||||
assertThat(list.get(0).getAgentId()).isNotNull(); | |||||
assertThat(list.get(0).getName()).isNotEmpty(); | |||||
assertThat(list.get(0).getSquareLogoUrl()).isNotEmpty(); | |||||
} | |||||
public static class MockTest { | |||||
private WxCpService wxService = mock(WxCpService.class); | |||||
@Test | |||||
public void testGet() throws Exception { | |||||
String returnJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"agentid\": 9,\"name\": \"测试应用\",\"square_logo_url\": \"http://wx.qlogo.cn/mmhead/alksjf;lasdjf;lasjfuodiuj3rj2o34j/0\",\"description\": \"这是一个企业号应用\",\"allow_userinfos\": {\"user\": [{\"userid\": \"0009854\"}, {\"userid\": \"1723\"}, {\"userid\": \"5625\"}]},\"allow_partys\": {\"partyid\": [42762742]},\"allow_tags\": {\"tagid\": [23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7]},\"close\": 0,\"redirect_domain\": \"weixin.com.cn\",\"report_location_flag\": 0,\"isreportenter\": 0,\"home_url\": \"\"}"; | |||||
when(wxService.get("https://qyapi.weixin.qq.com/cgi-bin/agent/get?agentid=9", null)).thenReturn(returnJson); | |||||
when(wxService.getAgentService()).thenReturn(new WxCpAgentServiceImpl(wxService)); | |||||
WxCpAgentService wxAgentService = this.wxService.getAgentService(); | |||||
WxCpAgent wxCpAgent = wxAgentService.get(9); | |||||
WxCpAgentService wxAgentService = this.wxService.getAgentService(); | |||||
WxCpAgent wxCpAgent = wxAgentService.get(9); | |||||
assertEquals(9, wxCpAgent.getAgentId().intValue()); | |||||
Assert.assertEquals(9, wxCpAgent.getAgentId().intValue()); | |||||
assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowParties().getPartyIds().toArray()); | |||||
Assert.assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowParties().getPartyIds().toArray()); | |||||
assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagIds().toArray()); | |||||
Assert.assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagIds().toArray()); | |||||
} | |||||
} | } | ||||