新增企业号应用设置接口

This commit is contained in:
jinyu 2015-03-17 10:51:45 +08:00
parent ef131f68bd
commit 7cb8e21579
12 changed files with 428 additions and 5 deletions

View File

@ -181,3 +181,7 @@
* 2015-03-08
+ **weixin4j-qy**: 新增根据code获取成员信息接口
* 2015-03-17
+ **weixin4j-qy**: 新增企业应用设置接口

View File

@ -20,6 +20,8 @@ weixin4j-qy
+ NotifyApi `消息发送API`
+ AgentApi `应用设置API`
* **weixin4j-qy-server**
+ `netty服务器` & `消息分发`
@ -110,3 +112,7 @@ weixin4j-qy
* 2015-03-08
+ **weixin4j-qy-api**: 新增根据code获取成员信息接口
* 2015-03-17
+ **weixin4j-qy-api**: 新增企业应用设置接口

View File

@ -13,6 +13,14 @@ weixin4j-qy-api
* TagApi `标签管理API`
* MediaApi `多媒体管理API`
* MenuApi `菜单管理API`
* NotifyApi `消息发送API`
* AgentApi `应用设置API`
如何使用
--------
1.API工程可以单独打包到其他项目中使用,需新增或拷贝`weixin.properties`文件到项目的`classpath`
@ -75,3 +83,7 @@ weixin.properties说明
* 2015-03-08
+ 新增根据code获取成员信息接口
* 2015-03-17
+ 新增企业应用设置接口

View File

@ -4,10 +4,13 @@ import java.util.List;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.JsonResult;
import com.foxinmy.weixin4j.qy.api.AgentApi;
import com.foxinmy.weixin4j.qy.api.DepartApi;
import com.foxinmy.weixin4j.qy.api.HelperApi;
import com.foxinmy.weixin4j.qy.api.TagApi;
import com.foxinmy.weixin4j.qy.api.UserApi;
import com.foxinmy.weixin4j.qy.model.AgentInfo;
import com.foxinmy.weixin4j.qy.model.AgentSetter;
import com.foxinmy.weixin4j.qy.model.Department;
import com.foxinmy.weixin4j.qy.model.Tag;
import com.foxinmy.weixin4j.qy.model.User;
@ -32,6 +35,7 @@ public class WeixinProxy {
private final UserApi userApi;
private final TagApi tagApi;
private final HelperApi helperApi;
private final AgentApi agentApi;
/**
* 默认采用文件存放Token信息
@ -61,6 +65,7 @@ public class WeixinProxy {
this.userApi = new UserApi(tokenHolder);
this.tagApi = new TagApi(tokenHolder);
this.helperApi = new HelperApi(tokenHolder);
this.agentApi = new AgentApi(tokenHolder);
}
/**
@ -408,4 +413,36 @@ public class WeixinProxy {
public List<String> getcallbackip() throws WeixinException {
return helperApi.getcallbackip();
}
/**
* 获取企业号某个应用的基本信息包括头像昵称帐号类型认证类型可见范围等信息
*
* @param agentid
* 授权方应用id
* @return 应用信息
* @see com.foxinmy.weixin4j.qy.model.AgentInfo
* @see com.foxinmy.weixin4j.qy.api.AgentApi
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E4%BC%81%E4%B8%9A%E5%8F%B7%E5%BA%94%E7%94%A8">企业号应用的信息</a>
* @throws WeixinException
*/
public AgentInfo getAgent(int agentid) throws WeixinException {
return agentApi.getAgent(agentid);
}
/**
* 设置企业应用的选项设置信息地理位置上报等
*
* @param agentSet
* 设置参数
* @see com.foxinmy.weixin4j.qy.model.AgentSetter
* @see com.foxinmy.weixin4j.qy.api.AgentApi
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%AE%BE%E7%BD%AE%E4%BC%81%E4%B8%9A%E5%8F%B7%E5%BA%94%E7%94%A8">设置企业号信息</a>
* @return 处理结果
* @throws WeixinException
*/
public JsonResult setAgent(AgentSetter agentSet) throws WeixinException {
return agentApi.setAgent(agentSet);
}
}

View File

@ -0,0 +1,96 @@
package com.foxinmy.weixin4j.qy.api;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.JsonResult;
import com.foxinmy.weixin4j.http.Response;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.qy.model.AgentInfo;
import com.foxinmy.weixin4j.qy.model.AgentSetter;
import com.foxinmy.weixin4j.qy.model.User;
import com.foxinmy.weixin4j.token.TokenHolder;
/**
* 管理应用接口
*
* @className AgentApi
* @author jy
* @date 2015年3月16日
* @since JDK 1.7
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E4%BC%81%E4%B8%9A%E5%8F%B7%E5%BA%94%E7%94%A8">管理应用接口说明</a>
*/
public class AgentApi extends QyApi {
private final TokenHolder tokenHolder;
public AgentApi(TokenHolder tokenHolder) {
this.tokenHolder = tokenHolder;
}
/**
* 获取企业号某个应用的基本信息包括头像昵称帐号类型认证类型可见范围等信息
*
* @param agentid
* 授权方应用id
* @return 应用信息
* @see com.foxinmy.weixin4j.qy.model.AgentInfo
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E4%BC%81%E4%B8%9A%E5%8F%B7%E5%BA%94%E7%94%A8">企业号应用的信息</a>
* @throws WeixinException
*/
public AgentInfo getAgent(int agentid) throws WeixinException {
String agent_get_uri = getRequestUri("agent_get_uri");
Token token = tokenHolder.getToken();
Response response = request.post(String.format(agent_get_uri,
token.getAccessToken(), agentid));
JSONObject jsonObj = response.getAsJson();
AgentInfo agent = JSON.toJavaObject(jsonObj, AgentInfo.class);
agent.setAllowUsers(JSON.parseArray(
jsonObj.getJSONObject("allow_userinfos").getString("user"),
User.class));
agent.setAllowPartys(JSON.parseArray(
jsonObj.getJSONObject("allow_partys").getString("partyid"),
Integer.class));
agent.setAllowTags(JSON.parseArray(jsonObj.getJSONObject("allow_tags")
.getString("tagid"), Integer.class));
return agent;
}
/**
* 设置企业应用的选项设置信息地理位置上报等
*
* @param agentSet
* 设置参数
* @see com.foxinmy.weixin4j.qy.model.AgentSetter
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%AE%BE%E7%BD%AE%E4%BC%81%E4%B8%9A%E5%8F%B7%E5%BA%94%E7%94%A8">设置企业号信息</a>
* @return 处理结果
* @throws WeixinException
*/
public JsonResult setAgent(AgentSetter agentSet) throws WeixinException {
String agent_set_uri = getRequestUri("agent_set_uri");
Token token = tokenHolder.getToken();
Response response = request.post(
String.format(agent_set_uri, token.getAccessToken()),
JSON.toJSONString(agentSet, typeFilter));
return response.getAsJsonResult();
}
private static ValueFilter typeFilter;
static {
typeFilter = new ValueFilter() {
@Override
public Object process(Object object, String name, Object value) {
if (value instanceof Boolean) {
return ((Boolean) value) ? 1 : 0;
}
if (value instanceof Enum) {
return ((Enum<?>) value).ordinal();
}
return value;
}
};
}
}

View File

@ -61,3 +61,7 @@ menu_get_uri={api_base_url}/menu/get?access_token=%s&agentid=%d
message_send_uri={api_base_url}/message/send?access_token=%s
# \u83b7\u53d6\u5fae\u4fe1IP\u5730\u5740
getcallbackip_uri={api_base_url}/getcallbackip?access_token=%s
# \u83b7\u53d6\u4f01\u4e1a\u53f7\u5e94\u7528\u4fe1\u606f
agent_get_uri={api_base_url}/agent/get?access_token=%s&agentid=%d
# \u8bbe\u7f6e\u4f01\u4e1a\u53f7\u5e94\u7528\u4fe1\u606f
agent_set_uri={api_base_url}/agent/set?access_token=%s

View File

@ -0,0 +1,91 @@
package com.foxinmy.weixin4j.qy.model;
import java.util.List;
import com.alibaba.fastjson.annotation.JSONField;
/**
* 企业号应用的基本信息
*
* @className AgentInfo
* @author jy
* @date 2015年3月17日
* @since JDK 1.7
* @see
*/
public class AgentInfo extends AgentSetter {
private static final long serialVersionUID = -8975132919768696174L;
@JSONField(name = "square_logo_url")
private String squareLogoUrl;// 企业应用方形头像
@JSONField(name = "round_logo_url")
private String roundLogoUrl;// 企业应用圆形头像
@JSONField(serialize = false)
private List<User> allowUsers;// 企业应用可见范围人员其中包括userid和关注状态state
@JSONField(serialize = false)
private List<Integer> allowPartys; // 企业应用可见范围部门
@JSONField(serialize = false)
private List<Integer> allowTags; // 企业应用可见范围标签
private boolean close; // 企业应用是否被禁用
public AgentInfo() {
super(0);
}
public String getSquareLogoUrl() {
return squareLogoUrl;
}
public void setSquareLogoUrl(String squareLogoUrl) {
this.squareLogoUrl = squareLogoUrl;
}
public String getRoundLogoUrl() {
return roundLogoUrl;
}
public void setRoundLogoUrl(String roundLogoUrl) {
this.roundLogoUrl = roundLogoUrl;
}
public List<User> getAllowUsers() {
return allowUsers;
}
public void setAllowUsers(List<User> allowUsers) {
this.allowUsers = allowUsers;
}
public List<Integer> getAllowPartys() {
return allowPartys;
}
public void setAllowPartys(List<Integer> allowPartys) {
this.allowPartys = allowPartys;
}
public List<Integer> getAllowTags() {
return allowTags;
}
public void setAllowTags(List<Integer> allowTags) {
this.allowTags = allowTags;
}
public boolean isClose() {
return close;
}
public void setClose(boolean close) {
this.close = close;
}
@Override
public String toString() {
return "AgentInfo [squareLogoUrl=" + squareLogoUrl + ", roundLogoUrl="
+ roundLogoUrl + ", allowUsers=" + allowUsers
+ ", allowPartys=" + allowPartys + ", allowTags=" + allowTags
+ ", close=" + close + ", " + super.toString() + "]";
}
}

View File

@ -0,0 +1,111 @@
package com.foxinmy.weixin4j.qy.model;
import java.io.Serializable;
import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.qy.type.ReportLocationType;
/**
* 设置企业号应用
*
* @className AgentSetter
* @author jy
* @date 2015年3月16日
* @since JDK 1.7
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%AE%BE%E7%BD%AE%E4%BC%81%E4%B8%9A%E5%8F%B7%E5%BA%94%E7%94%A8">设置企业号应用</a>
*/
public class AgentSetter implements Serializable {
private static final long serialVersionUID = 5420335232308079801L;
private int agentid; // 企业应用的id
@JSONField(name = "report_location_flag")
private ReportLocationType reportLocationType; // 企业应用是否打开地理位置上报
@JSONField(name = "logo_mediaid")
private String logoMediaid; // 企业应用头像的mediaid通过多媒体接口上传图片获得mediaid上传后会自动裁剪成方形和圆形两个头像
private String name; // 企业应用名称
private String description; // 企业应用详情
@JSONField(name = "redirect_domain")
private String redirectDomain; // 企业应用可信域名
@JSONField(name = "isreportuser")
private int isReportUser; // 是否接收用户变更通知0不接收1接收
@JSONField(name = "isreportenter")
private int isReportEnter; // 是否上报用户进入应用事件0不接收1接收
public AgentSetter(int agentid) {
this.agentid = agentid;
}
public int getAgentid() {
return agentid;
}
public void setAgentid(int agentid) {
this.agentid = agentid;
}
public ReportLocationType getReportLocationType() {
return reportLocationType;
}
public void setReportLocationType(ReportLocationType reportLocationType) {
this.reportLocationType = reportLocationType;
}
public String getLogoMediaid() {
return logoMediaid;
}
public void setLogoMediaid(String logoMediaid) {
this.logoMediaid = logoMediaid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getRedirectDomain() {
return redirectDomain;
}
public void setRedirectDomain(String redirectDomain) {
this.redirectDomain = redirectDomain;
}
public boolean getIsReportUser() {
return isReportUser == 1;
}
public void setIsReportUser(boolean isReportUser) {
this.isReportUser = isReportUser ? 1 : 0;
}
public boolean getIsReportEnter() {
return isReportEnter == 1;
}
public void setIsReportEnter(boolean isReportEnter) {
this.isReportEnter = isReportEnter ? 1 : 0;
}
@Override
public String toString() {
return "agentid=" + agentid + ", reportLocationType="
+ reportLocationType + ", logoMediaid=" + logoMediaid
+ ", name=" + name + ", description=" + description
+ ", redirectDomain=" + redirectDomain + ", isReportUser="
+ isReportUser + ", isReportEnter=" + isReportEnter;
}
}

View File

@ -0,0 +1,15 @@
package com.foxinmy.weixin4j.qy.type;
/**
* 上报策略
* @className ReportLocationType
* @author jy
* @date 2015年3月16日
* @since JDK 1.7
* @see
*/
public enum ReportLocationType {
NOT, // 不上报
DIALOG, // 进入回话上报
PERSIST; // 持续上报
}

View File

@ -1,6 +1,6 @@
# \u6d4b\u8bd5\u4e4b\u7528 \u6b63\u5f0f\u73af\u5883\u4e0bcopy\u4e00\u4efd\u5230classpath
# \u4f01\u4e1a\u53f7\u4fe1\u606f
account={"id":"wxd9d9fa581a07cefd","secret":"7tr6FXh2NORvqq1la9CVwxV0xZWGsRSgI6tfqd3-JFc9E2p7UukNNYeKoOTLe0Ru",\
account={"id":"wxf10bce209c91d0e2","secret":"cW0OtP7-7YJ7jHKFZaJW2skJHE9bLHadxOswBdVdI2walRBSPfTSA6QqD_QXw8JZ",\
"token":"gp2eGT5mIpngr",\
"encodingAesKey":"BRYfV4zPFUJb3v3MySNBg1ERKE3vyyMRoScu76vFySv"}

View File

@ -0,0 +1,47 @@
package com.foxinmy.weixin4j.qy.test;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.JsonResult;
import com.foxinmy.weixin4j.qy.api.AgentApi;
import com.foxinmy.weixin4j.qy.model.AgentInfo;
import com.foxinmy.weixin4j.qy.model.AgentSetter;
import com.foxinmy.weixin4j.qy.type.ReportLocationType;
/**
* 应用API测试
*
* @className AgentTest
* @author jy
* @date 2015年03月17日
* @since JDK 1.7
* @see
*/
public class AgentTest extends TokenTest {
public AgentApi agentApi;
@Before
public void init() {
this.agentApi = new AgentApi(tokenHolder);
}
@Test
public void get() throws WeixinException {
AgentInfo agent = agentApi.getAgent(0);
Assert.assertTrue(agent != null);
System.err.println(agent);
}
@Test
public void set() throws WeixinException {
AgentSetter agentSet = new AgentSetter(1);
agentSet.setDescription("test");
agentSet.setRedirectDomain("test.com");
agentSet.setReportLocationType(ReportLocationType.DIALOG);
JsonResult result = agentApi.setAgent(agentSet);
Assert.assertTrue(result.getCode() == 0);
}
}

View File

@ -14,9 +14,9 @@ import com.foxinmy.weixin4j.qy.model.Tag;
import com.foxinmy.weixin4j.qy.model.User;
/**
* 部门API测试
* 标签API测试
*
* @className DepartTest
* @className TagTest
* @author jy
* @date 2014年11月18日
* @since JDK 1.7