优化代码

This commit is contained in:
jinyu 2016-02-27 09:53:36 +08:00
parent c6a9854717
commit fe51ab7813
7 changed files with 81 additions and 51 deletions

View File

@ -6,7 +6,7 @@ package com.foxinmy.weixin4j.jssdk;
* @className JSSDKAPI
* @author jy
* @date 2015年12月23日
* @since JDK 1.7
* @since JDK 1.6
* @see
*/
public enum JSSDKAPI {

View File

@ -3,7 +3,6 @@ package com.foxinmy.weixin4j.qy;
import java.io.InputStream;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.JsonResult;
import com.foxinmy.weixin4j.model.Button;
@ -199,9 +198,9 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.tuple.File
* @see com.foxinmy.weixin4j.tuple.News
* @see com.foxinmy.weixin4j.tuple.MpNews
* @see com.foxinmy.weixin4j.qy.message.NotifyMessage
* @see com.foxinmy.weixin4j.qy.model.IdParameter
*/
public JSONObject sendNotifyMessage(NotifyMessage message)
public IdParameter sendNotifyMessage(NotifyMessage message)
throws WeixinException {
return notifyApi.sendNotifyMessage(message);
}
@ -710,22 +709,19 @@ public class WeixinProxy {
}
/**
* 根据code获取成员信息
* 根据code获取成员ID信息
*
* @param code
* 通过员工授权获取到的code每次员工授权带上的code将不一样code只能使用一次5分钟未被使用自动过期
* @return { "UserId":"USERID", "DeviceId":"DEVICEID" } or
* {"OpenId":"OPENID","DeviceId":"DEVICEID"}
* @return 两个元素的数组 <font color="red">第一个元素为userId或者openId
* 第二个元素为deviceId</font>
* @see com.foxinmy.weixin4j.qy.api.UserApi
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E8%8E%B7%E5%8F%96code">
* 企业获取code</a>
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E6%A0%B9%E6%8D%AEcode%E8%8E%B7%E5%8F%96%E6%88%90%E5%91%98%E4%BF%A1%E6%81%AF">
* 根据code获取成员信息</a>
* "http://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3">
* oauth授权获取用户信息</a>
* @throws WeixinException
*/
public JSONObject getUserIdByCode(String code) throws WeixinException {
public String[] getUserIdByCode(String code) throws WeixinException {
return userApi.getUserIdByCode(code);
}

View File

@ -1,5 +1,8 @@
package com.foxinmy.weixin4j.qy.api;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
@ -68,8 +71,9 @@ public class NotifyApi extends QyApi {
* @see com.foxinmy.weixin4j.tuple.News
* @see com.foxinmy.weixin4j.tuple.MpNews
* @see com.foxinmy.weixin4j.qy.message.NotifyMessage
* @see com.foxinmy.weixin4j.qy.model.IdParameter
*/
public JSONObject sendNotifyMessage(NotifyMessage message)
public IdParameter sendNotifyMessage(NotifyMessage message)
throws WeixinException {
NotifyTuple tuple = message.getTuple();
Map<String, String> target = message.getTarget().getParameter();
@ -87,8 +91,29 @@ public class NotifyApi extends QyApi {
WeixinResponse response = weixinExecutor.post(
String.format(message_send_uri, token.getAccessToken()),
obj.toJSONString());
return response.getAsJson();
obj = response.getAsJson();
IdParameter idParameter = new IdParameter();
if (obj.containsKey("invaliduser")) {
idParameter.setUserIds(Arrays.asList(obj.getString("invalidlist")
.split(IdParameter.SEPARATORS)));
}
if (obj.containsKey("invalidparty")) {
List<Integer> partyIds = new ArrayList<Integer>();
for (String id : obj.getString("invalidlist").split(
IdParameter.SEPARATORS)) {
partyIds.add(Integer.parseInt(id));
}
idParameter.setPartyIds(partyIds);
}
if (obj.containsKey("invalidtag")) {
List<Integer> tagIds = new ArrayList<Integer>();
for (String id : obj.getString("invalidtag").split(
IdParameter.SEPARATORS)) {
tagIds.add(Integer.parseInt(id));
}
idParameter.setPartyIds(tagIds);
}
return idParameter;
}
/**

View File

@ -205,7 +205,7 @@ public class TagApi extends QyApi {
IdParameter idParameter = new IdParameter();
if (obj.containsKey("invalidlist")) {
idParameter.setUserIds(Arrays.asList(obj.getString("invalidlist")
.split(String.valueOf(IdParameter.SEPARATOR))));
.split(IdParameter.SEPARATORS)));
}
if (obj.containsKey("partylist")) {
idParameter.setPartyIds(JSON.parseArray(obj.getString("partylist"),

View File

@ -17,6 +17,7 @@ import com.foxinmy.weixin4j.qy.type.InviteType;
import com.foxinmy.weixin4j.qy.type.UserStatus;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.util.NameValue;
import com.foxinmy.weixin4j.util.StringUtil;
/**
* 成员API
@ -165,7 +166,7 @@ public class UserApi extends QyApi {
}
/**
* code获取userid(管理员须拥有agent的使用权限agentid必须和跳转链接时所在的企业应用ID相同)
* 根据code获取用户信息
*
* @param code
* 通过员工授权获取到的code每次员工授权带上的code将不一样code只能使用一次5分钟未被使用自动过期
@ -174,15 +175,45 @@ public class UserApi extends QyApi {
* @see {@link #getUser(String)}
* @see {@link #getUserIdByCode(String,int)}
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E8%8E%B7%E5%8F%96code">
* 企业获取code</a>
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E6%A0%B9%E6%8D%AEcode%E8%8E%B7%E5%8F%96%E6%88%90%E5%91%98%E4%BF%A1%E6%81%AF">
* 根据code获取成员信息</a>
* "http://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3">
* oauth授权获取用户信息</a>
* @throws WeixinException
*/
public User getUserByCode(String code) throws WeixinException {
return getUser(getUserIdByCode(code).getString("UserId"));
String[] userIds = getUserIdByCode(code);
if (Boolean.parseBoolean(userIds[2])) {
return getUser(openid2userid(userIds[0]));
} else {
return getUser(userIds[0]);
}
}
/**
* 根据code获取成员ID信息
*
* @param code
* 通过员工授权获取到的code每次员工授权带上的code将不一样code只能使用一次5分钟未被使用自动过期
* @return 两个元素的数组 <font color="red">第一个元素为userId或者openId
* 第二个元素为deviceId</font>
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3">
* oauth授权获取用户信息</a>
* @throws WeixinException
*/
public String[] getUserIdByCode(String code) throws WeixinException {
String user_getid_uri = getRequestUri("user_getid_uri");
Token token = tokenHolder.getToken();
WeixinResponse response = weixinExecutor.get(String.format(
user_getid_uri, token.getAccessToken(), code));
JSONObject result = response.getAsJson();
String userId = result.getString("UserId");
boolean need2 = false;
if (StringUtil.isBlank(userId)) {
userId = result.getString("OpenId");
need2 = true;
}
return new String[] { userId, result.getString("DeviceId"),
Boolean.toString(need2) };
}
/**
@ -208,28 +239,6 @@ public class UserApi extends QyApi {
return JSON.parseObject(response.getAsString(), OUserInfo.class);
}
/**
* 根据code获取成员信息
*
* @param code
* 通过员工授权获取到的code每次员工授权带上的code将不一样code只能使用一次5分钟未被使用自动过期
* @return { "UserId":"USERID", "DeviceId":"DEVICEID" }
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/indexo.php?title=%E4%BC%81%E4%B8%9A%E8%8E%B7%E5%8F%96code">
* 企业获取code</a>
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E6%A0%B9%E6%8D%AEcode%E8%8E%B7%E5%8F%96%E6%88%90%E5%91%98%E4%BF%A1%E6%81%AF">
* 根据code获取成员信息</a>
* @throws WeixinException
*/
public JSONObject getUserIdByCode(String code) throws WeixinException {
String user_getid_uri = getRequestUri("user_getid_uri");
Token token = tokenHolder.getToken();
WeixinResponse response = weixinExecutor.get(String.format(
user_getid_uri, token.getAccessToken(), code));
return response.getAsJson();
}
/**
* 获取部门成员
*

View File

@ -25,6 +25,8 @@ public class IdParameter implements Serializable {
public static final char SEPARATOR = '|';
public static final String SEPARATORS = String.valueOf(SEPARATOR);
@JSONField(name = "user")
private List<String> userIds;
@JSONField(name = "party")

View File

@ -1,14 +1,12 @@
package com.foxinmy.weixin4j.qy.test;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.qy.api.NotifyApi;
import com.foxinmy.weixin4j.qy.message.NotifyMessage;
import com.foxinmy.weixin4j.qy.model.IdParameter;
import com.foxinmy.weixin4j.tuple.File;
import com.foxinmy.weixin4j.tuple.Image;
import com.foxinmy.weixin4j.tuple.MpNews;
@ -88,8 +86,8 @@ public class NotifyTest extends TokenTest {
@Test
public void send1() throws WeixinException {
Text text = new Text("this is a text");
JSONObject result = notifyApi.sendNotifyMessage(new NotifyMessage(1,
IdParameter result = notifyApi.sendNotifyMessage(new NotifyMessage(1,
text));
Assert.assertEquals(0, result.getIntValue("errcode"));
System.err.println(result);
}
}