weixin4j-qy:NotifyMessage中的发送对象参数调整为IdParameter

This commit is contained in:
jinyu 2015-07-18 15:25:23 +08:00
parent 56f91c0283
commit f5e9046294
3 changed files with 29 additions and 92 deletions

View File

@ -104,7 +104,7 @@ public class XmlstreamTest {
// xml2map(); // xml2map();
// xml2order(); // xml2order();
// System.err.println(xml2refundRecordV2()); // System.err.println(xml2refundRecordV2());
xml2refundRecordV3(); // xml2refundRecordV3();
// object2xmlWithoutRootElement(); // object2xmlWithoutRootElement();
/*RefundRecord refundRecord = xml2refundRecordV2(); /*RefundRecord refundRecord = xml2refundRecordV2();
System.err.println(refundRecord); System.err.println(refundRecord);

View File

@ -1,5 +1,7 @@
package com.foxinmy.weixin4j.qy.api; package com.foxinmy.weixin4j.qy.api;
import java.util.Map;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.exception.WeixinException;
@ -46,9 +48,8 @@ public class NotifyApi extends QyApi {
* *
* @param notify * @param notify
* 客服消息对象 * 客服消息对象
* @return * @return 如果无权限则本次发送失败如果收件人不存在或未关注发送仍然执行两种情况下均返回无效的部分</br> { "errcode":
* 如果无权限则本次发送失败如果收件人不存在或未关注发送仍然执行两种情况下均返回无效的部分</br> * 0, "errmsg": "ok", "invaliduser": "UserID1",
* { "errcode": 0, "errmsg": "ok", "invaliduser": "UserID1",
* "invalidparty":"PartyID1", "invalidtag":"TagID1" } * "invalidparty":"PartyID1", "invalidtag":"TagID1" }
* @throws WeixinException * @throws WeixinException
* @see <a * @see <a
@ -66,10 +67,16 @@ public class NotifyApi extends QyApi {
*/ */
public JSONObject sendNotify(NotifyMessage notify) throws WeixinException { public JSONObject sendNotify(NotifyMessage notify) throws WeixinException {
NotifyTuple tuple = notify.getTuple(); NotifyTuple tuple = notify.getTuple();
Map<String, String> target = notify.getTarget().getParameter();
String msgtype = tuple.getMessageType(); String msgtype = tuple.getMessageType();
JSONObject obj = (JSONObject) JSON.toJSON(notify); JSONObject obj = (JSONObject) JSON.toJSON(notify);
obj.put("msgtype", msgtype); obj.put("msgtype", msgtype);
obj.put(msgtype, tuple); obj.put(msgtype, tuple);
if (target == null || target.isEmpty()) {
obj.put("touser", "@all");
} else {
obj.putAll(target);
}
String message_send_uri = getRequestUri("message_send_uri"); String message_send_uri = getRequestUri("message_send_uri");
Token token = tokenHolder.getToken(); Token token = tokenHolder.getToken();
WeixinResponse response = weixinClient.post( WeixinResponse response = weixinClient.post(

View File

@ -1,11 +1,10 @@
package com.foxinmy.weixin4j.qy.message; package com.foxinmy.weixin4j.qy.message;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.qy.model.IdParameter;
import com.foxinmy.weixin4j.tuple.NotifyTuple; import com.foxinmy.weixin4j.tuple.NotifyTuple;
import com.foxinmy.weixin4j.util.StringUtil;
/** /**
* 发送消息对象 * 发送消息对象
@ -27,21 +26,7 @@ import com.foxinmy.weixin4j.util.StringUtil;
public class NotifyMessage implements Serializable { public class NotifyMessage implements Serializable {
private static final long serialVersionUID = 1219589414293000383L; private static final long serialVersionUID = 1219589414293000383L;
private static final char SEPARATOR = '|';
/**
* UserID列表消息接收者多个接收者用|分隔特殊情况指定为@all则向关注该企业应用的全部成员发送
*/
private String touser;
/**
* PartyID列表多个接受者用|分隔当touser为@all时忽略本参数
*/
private String toparty;
/**
* TagID列表多个接受者用|分隔当touser为@all时忽略本参数
*/
private String totag;
/** /**
* 企业应用的id整型可在应用的设置页面查看 * 企业应用的id整型可在应用的设置页面查看
*/ */
@ -55,102 +40,47 @@ public class NotifyMessage implements Serializable {
*/ */
@JSONField(serialize = false) @JSONField(serialize = false)
private NotifyTuple tuple; private NotifyTuple tuple;
/**
* id参数
*/
@JSONField(serialize = false)
private IdParameter target;
public NotifyMessage(NotifyTuple tuple, int agentid) { public NotifyMessage(NotifyTuple tuple, int agentid) {
this(null, null, null, tuple, agentid, false); this(agentid, tuple, new IdParameter(), false);
this.touser = "@all";
} }
public NotifyMessage(List<String> tousers, List<String> topartys, public NotifyMessage(int agentid, NotifyTuple tuple, IdParameter target,
List<String> totags, NotifyTuple tuple, int agentid, boolean isSafe) { boolean isSafe) {
if (tousers != null && !tousers.isEmpty()) {
this.touser = StringUtil.join(tousers, SEPARATOR);
}
if (topartys != null && !topartys.isEmpty()) {
this.toparty = StringUtil.join(topartys, SEPARATOR);
}
if (totags != null && !totags.isEmpty()) {
this.totag = StringUtil.join(totags, SEPARATOR);
}
this.agentid = agentid; this.agentid = agentid;
this.safe = isSafe ? 1 : 0; this.safe = isSafe ? 1 : 0;
this.tuple = tuple; this.tuple = tuple;
} this.target = target;
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public void setTouser(List<String> tousers) {
if (tousers != null && !tousers.isEmpty()) {
this.touser = StringUtil.join(tousers, SEPARATOR);
}
}
public String getToparty() {
return toparty;
}
public void setToparty(String toparty) {
this.toparty = toparty;
}
public void setToparty(List<String> topartys) {
if (topartys != null && !topartys.isEmpty()) {
this.toparty = StringUtil.join(topartys, SEPARATOR);
}
}
public String getTotag() {
return totag;
}
public void setTotag(String totag) {
this.totag = totag;
}
public void setTotag(List<String> totags) {
if (totags != null && !totags.isEmpty()) {
this.totag = StringUtil.join(totags, SEPARATOR);
}
}
public int getAgentid() {
return agentid;
}
public void setAgentid(int agentid) {
this.agentid = agentid;
} }
public int getSafe() { public int getSafe() {
return safe; return safe;
} }
public void setSafe(int safe) {
this.safe = safe;
}
public void setSafe(boolean isSafe) { public void setSafe(boolean isSafe) {
this.safe = isSafe ? 1 : 0; this.safe = isSafe ? 1 : 0;
} }
public int getAgentid() {
return agentid;
}
public NotifyTuple getTuple() { public NotifyTuple getTuple() {
return tuple; return tuple;
} }
public void setTuple(NotifyTuple tuple) { public IdParameter getTarget() {
this.tuple = tuple; return target;
} }
@Override @Override
public String toString() { public String toString() {
return "NotifyMessage [touser=" + touser + ", toparty=" + toparty return "NotifyMessage [agentid=" + agentid + ", safe=" + safe
+ ", totag=" + totag + ", agentid=" + agentid + ", safe=" + ", tuple=" + tuple + ", target=" + target + "]";
+ safe + ", tuple=" + tuple + "]";
} }
} }