调整细节&新增小视频消息

This commit is contained in:
jinyu 2015-04-08 18:28:32 +08:00
parent 78e3757029
commit 624ccff953
17 changed files with 137 additions and 83 deletions

View File

@ -226,4 +226,4 @@
+ **weixin4j-qy**: [MediaApi](./weixin4j-qy/weixin4j-qy-api/src/main/java/com/foxinmy/weixin4j/qy/api/MediaApi.java)新增批量上传成员和部门接口 + **weixin4j-qy**: [MediaApi](./weixin4j-qy/weixin4j-qy-api/src/main/java/com/foxinmy/weixin4j/qy/api/MediaApi.java)新增批量上传成员和部门接口
<font color="red">released 1.3</font> + <font color="red">released 1.3</font>

View File

@ -17,11 +17,13 @@ import com.foxinmy.weixin4j.type.MessageType;
*/ */
public abstract class AbstractActionMapping implements ActionMapping { public abstract class AbstractActionMapping implements ActionMapping {
protected final static String DECOLLATOR = ":";
protected String getMappingKey(String xmlMsg) throws DocumentException { protected String getMappingKey(String xmlMsg) throws DocumentException {
Document doc = DocumentHelper.parseText(xmlMsg); Document doc = DocumentHelper.parseText(xmlMsg);
String msgType = doc.selectSingleNode("/xml/MsgType").getStringValue(); String msgType = doc.selectSingleNode("/xml/MsgType").getStringValue();
if (msgType.equalsIgnoreCase(MessageType.event.name())) { if (msgType.equalsIgnoreCase(MessageType.event.name())) {
msgType += "_" msgType += DECOLLATOR
+ doc.selectSingleNode("/xml/Event").getStringValue(); + doc.selectSingleNode("/xml/Event").getStringValue();
} }
return msgType.toLowerCase(); return msgType.toLowerCase();

View File

@ -27,7 +27,8 @@ public class AnnotationActionMapping extends AbstractActionMapping {
actionMap = new HashMap<String, WeixinAction>(); actionMap = new HashMap<String, WeixinAction>();
Set<Class<?>> weixinActions = ClassUtil.getClasses(actionPackage); Set<Class<?>> weixinActions = ClassUtil.getClasses(actionPackage);
for (Class<?> clazz : weixinActions) { for (Class<?> clazz : weixinActions) {
ActionAnnotation action = clazz.getAnnotation(ActionAnnotation.class); ActionAnnotation action = clazz
.getAnnotation(ActionAnnotation.class);
if (action == null) { if (action == null) {
continue; continue;
} }
@ -41,9 +42,8 @@ public class AnnotationActionMapping extends AbstractActionMapping {
EventType[] eventTypes = action.eventType(); EventType[] eventTypes = action.eventType();
if (eventTypes != null && eventTypes.length > 0) { if (eventTypes != null && eventTypes.length > 0) {
for (EventType e : eventTypes) { for (EventType e : eventTypes) {
actionMap.put( actionMap.put((msgType.name() + DECOLLATOR + e.name())
(msgType.name() + "_" + e.name()).toLowerCase(), .toLowerCase(), weixinAction);
weixinAction);
} }
continue; continue;
} }

View File

@ -198,7 +198,6 @@ public class HttpRequest {
.getFirstHeader(HttpHeaders.CONTENT_TYPE); .getFirstHeader(HttpHeaders.CONTENT_TYPE);
Header disposition = httpResponse Header disposition = httpResponse
.getFirstHeader("Content-disposition"); .getFirstHeader("Content-disposition");
System.err.println(response.getAsString());
// json // json
if (contentType.getValue().contains( if (contentType.getValue().contains(
ContentType.APPLICATION_JSON.getMimeType()) ContentType.APPLICATION_JSON.getMimeType())

View File

@ -43,10 +43,10 @@ public class BaseMsg implements Serializable {
*/ */
@XStreamAlias("MsgId") @XStreamAlias("MsgId")
private long msgId; private long msgId;
@XStreamAlias("AgentID")
/** /**
* 企业号独有的应用ID * 企业号独有的应用ID
*/ */
@XStreamAlias("AgentID")
private String agentId; private String agentId;
public BaseMsg() { public BaseMsg() {

View File

@ -41,6 +41,12 @@ public enum MessageType {
* @see com.foxinmy.weixin4j.msg.VideoMessage * @see com.foxinmy.weixin4j.msg.VideoMessage
*/ */
video(VideoMessage.class), video(VideoMessage.class),
/**
* 小视频消息
*
* @see com.foxinmy.weixin4j.msg.VideoMessage
*/
shortvideo(VideoMessage.class),
/** /**
* 位置消息 * 位置消息
* *

View File

@ -178,7 +178,7 @@ public class MessageUtil {
* @see <a * @see <a
* href="http://mp.weixin.qq.com/wiki/10/79502792eef98d6e0c6e1739da387346.html">普通消息</a> * href="http://mp.weixin.qq.com/wiki/10/79502792eef98d6e0c6e1739da387346.html">普通消息</a>
* @see <a * @see <a
* href="http://mp.weixin.qq.com/wiki/2/5baf56ce4947d35003b86a9805634b1e.html">事件触发</a> * href="http://mp.weixin.qq.com/wiki/2/5baf56ce4947d35003b86a9805634b1e.html">事件消息</a>
* @see com.foxinmy.weixin4j.type.MessageType * @see com.foxinmy.weixin4j.type.MessageType
* @see com.feican.weixin.msg.BaeMessage * @see com.feican.weixin.msg.BaeMessage
* @see com.foxinmy.weixin4j.msg.TextMessage * @see com.foxinmy.weixin4j.msg.TextMessage

View File

@ -347,7 +347,23 @@ public class WeixinProxy {
* *
* @param notify * @param notify
* 客服消息对象 * 客服消息对象
* @return 处理结果
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#sendNotify(NotifyMessage,String) }
* @throws WeixinException * @throws WeixinException
*/
public JsonResult sendNotify(NotifyMessage notify) throws WeixinException {
return notifyApi.sendNotify(notify);
}
/**
* 发送客服消息(在48小时内不限制发送次数)
*
* @param notify
* 客服消息对象
* @param kfAccount
* 客服账号 可为空
* @throws WeixinException
* @return 处理结果
* @see <a * @see <a
* href="http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html#.E5.AE.A2.E6.9C.8D.E6.8E.A5.E5.8F.A3-.E5.8F.91.E6.B6.88.E6.81.AF">发送客服消息</a> * href="http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html#.E5.AE.A2.E6.9C.8D.E6.8E.A5.E5.8F.A3-.E5.8F.91.E6.B6.88.E6.81.AF">发送客服消息</a>
* @see com.foxinmy.weixin4j.msg.model.Text * @see com.foxinmy.weixin4j.msg.model.Text
@ -358,8 +374,9 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.msg.model.News * @see com.foxinmy.weixin4j.msg.model.News
* @see com.foxinmy.weixin4j.mp.api.NotifyApi * @see com.foxinmy.weixin4j.mp.api.NotifyApi
*/ */
public JsonResult sendNotify(NotifyMessage notify) throws WeixinException { public JsonResult sendNotify(NotifyMessage notify, String kfAccount)
return notifyApi.sendNotify(notify); throws WeixinException {
return notifyApi.sendNotify(notify, kfAccount);
} }
/** /**

View File

@ -1,10 +1,14 @@
package com.foxinmy.weixin4j.mp.api; package com.foxinmy.weixin4j.mp.api;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.JsonResult; import com.foxinmy.weixin4j.http.JsonResult;
import com.foxinmy.weixin4j.http.Response; import com.foxinmy.weixin4j.http.Response;
import com.foxinmy.weixin4j.model.Token; import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.mp.message.NotifyMessage; import com.foxinmy.weixin4j.mp.message.NotifyMessage;
import com.foxinmy.weixin4j.msg.model.Base;
import com.foxinmy.weixin4j.msg.model.Notifyable; import com.foxinmy.weixin4j.msg.model.Notifyable;
import com.foxinmy.weixin4j.token.TokenHolder; import com.foxinmy.weixin4j.token.TokenHolder;
@ -32,7 +36,23 @@ public class NotifyApi extends MpApi {
* *
* @param notify * @param notify
* 客服消息对象 * 客服消息对象
* @return 处理结果
* @see {@link com.foxinmy.weixin4j.mp.api.NotifyApi#sendNotify(NotifyMessage, String)}
* @throws WeixinException * @throws WeixinException
*/
public JsonResult sendNotify(NotifyMessage notify) throws WeixinException {
return sendNotify(notify, null);
}
/**
* 发送客服消息(在48小时内不限制发送次数)
*
* @param notify
* 客服消息对象
* @param kfAccount
* 客服账号 可为空
* @throws WeixinException
* @return 处理结果
* @see <a * @see <a
* href="http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html#.E5.AE.A2.E6.9C.8D.E6.8E.A5.E5.8F.A3-.E5.8F.91.E6.B6.88.E6.81.AF">发送客服消息</a> * href="http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html#.E5.AE.A2.E6.9C.8D.E6.8E.A5.E5.8F.A3-.E5.8F.91.E6.B6.88.E6.81.AF">发送客服消息</a>
* @see com.foxinmy.weixin4j.msg.model.Text * @see com.foxinmy.weixin4j.msg.model.Text
@ -43,16 +63,28 @@ public class NotifyApi extends MpApi {
* @see com.foxinmy.weixin4j.msg.model.News * @see com.foxinmy.weixin4j.msg.model.News
* @see com.foxinmy.weixin4j.mp.message.NotifyMessage * @see com.foxinmy.weixin4j.mp.message.NotifyMessage
*/ */
public JsonResult sendNotify(NotifyMessage notify) throws WeixinException { public JsonResult sendNotify(NotifyMessage notify, String kfAccount)
if (!(notify.getBox() instanceof Notifyable)) { throws WeixinException {
Base box = notify.getBox();
if (!(box instanceof Notifyable)) {
throw new WeixinException(String.format( throw new WeixinException(String.format(
"%s not implement Notifyable", notify.getBox().getClass())); "%s not implement Notifyable", box.getClass()));
}
String msgtype = box.getMediaType().name();
JSONObject obj = new JSONObject();
obj.put("touser", notify.getTouser());
obj.put("msgtype", msgtype);
obj.put(msgtype, box);
if (StringUtils.isNotBlank(kfAccount)) {
JSONObject kf = new JSONObject();
kf.put("kf_account", kfAccount);
obj.put("customservice", kf);
} }
String custom_notify_uri = getRequestUri("custom_notify_uri"); String custom_notify_uri = getRequestUri("custom_notify_uri");
Token token = tokenHolder.getToken(); Token token = tokenHolder.getToken();
Response response = request.post( Response response = request.post(
String.format(custom_notify_uri, token.getAccessToken()), String.format(custom_notify_uri, token.getAccessToken()),
notify.toJson()); obj.toJSONString());
return response.getAsJsonResult(); return response.getAsJsonResult();
} }

View File

@ -27,3 +27,5 @@
* DataApi `数据统计API` * DataApi `数据统计API`
* OauthApi `oauth授权API` * OauthApi `oauth授权API`
* CashApi `现金API`

View File

@ -149,7 +149,7 @@ datacube_uri={api_base_url}/datacube/%s?access_token=%s
# \u4e0a\u4f20\u6c38\u4e45\u56fe\u6587\u7d20\u6750 # \u4e0a\u4f20\u6c38\u4e45\u56fe\u6587\u7d20\u6750
material_article_upload_uri={api_cgi_url}/material/add_news?access_token=%s material_article_upload_uri={api_cgi_url}/material/add_news?access_token=%s
# \u4e0a\u4f20\u6c38\u4e45\u5a92\u4f53\u7d20\u6750 # \u4e0a\u4f20\u6c38\u4e45\u5a92\u4f53\u7d20\u6750
material_media_upload_uri={file_base_url}/material/add_material?access_token=%s material_media_upload_uri={api_cgi_url}/material/add_material?access_token=%s
# \u4e0b\u8f7d\u6c38\u4e45\u5a92\u4f53\u7d20\u6750 # \u4e0b\u8f7d\u6c38\u4e45\u5a92\u4f53\u7d20\u6750
material_media_download_uri={api_cgi_url}/material/get_material?access_token=%s material_media_download_uri={api_cgi_url}/material/get_material?access_token=%s
# \u66f4\u65b0\u6c38\u4e45\u56fe\u6587\u7d20\u6750 # \u66f4\u65b0\u6c38\u4e45\u56fe\u6587\u7d20\u6750

View File

@ -2,9 +2,6 @@ package com.foxinmy.weixin4j.mp.message;
import java.io.Serializable; import java.io.Serializable;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.msg.model.Base; import com.foxinmy.weixin4j.msg.model.Base;
/** /**
@ -33,13 +30,8 @@ public class NotifyMessage implements Serializable {
/** /**
* 消息对象 * 消息对象
*/ */
@JSONField(name = "%s")
private Base box; private Base box;
public NotifyMessage(Base box) {
this(null, box);
}
public NotifyMessage(String touser, Base box) { public NotifyMessage(String touser, Base box) {
this.touser = touser; this.touser = touser;
this.box = box; this.box = box;
@ -61,18 +53,6 @@ public class NotifyMessage implements Serializable {
this.box = box; this.box = box;
} }
/**
* 客服消息json化
*
* @return {"touser": "to","msgtype": "text","text": {"content": "123"}}
*/
public String toJson() {
String msgtype = box.getMediaType().name();
JSONObject obj = (JSONObject) JSON.toJSON(this);
obj.put("msgtype", msgtype);
return String.format(obj.toJSONString(), msgtype);
}
@Override @Override
public String toString() { public String toString() {
return "NotifyMessage [touser=" + touser + ", box=" + box + "]"; return "NotifyMessage [touser=" + touser + ", box=" + box + "]";

View File

@ -24,7 +24,7 @@ import com.foxinmy.weixin4j.type.MediaType;
/** /**
* 客服消息测试 * 客服消息测试
* *
* @className MessageNotifyTest * @className NotifyMsgTest
* @author jy.hu * @author jy.hu
* @date 2014年4月10日 * @date 2014年4月10日
* @since JDK 1.7 * @since JDK 1.7
@ -42,42 +42,42 @@ public class NotifyMsgTest extends TokenTest {
} }
@Test @Test
public void text() { public void text() throws WeixinException {
NotifyMessage notify = new NotifyMessage("to", new Text("ttt")); NotifyMessage notify = new NotifyMessage("to", new Text("ttt"));
System.out.println(notify.toJson()); System.out.println(notifyApi.sendNotify(notify));
} }
@Test @Test
public void image() { public void image() throws WeixinException {
NotifyMessage notify = new NotifyMessage("to", new Image("image")); NotifyMessage notify = new NotifyMessage("to", new Image("image"));
System.out.println(notify.toJson()); System.out.println(notifyApi.sendNotify(notify));
} }
@Test @Test
public void voice() { public void voice() throws WeixinException {
NotifyMessage notify = new NotifyMessage("to", new Voice("voice")); NotifyMessage notify = new NotifyMessage("to", new Voice("voice"));
System.out.println(notify.toJson()); System.out.println(notifyApi.sendNotify(notify));
} }
@Test @Test
public void video() { public void video() throws WeixinException {
NotifyMessage notify = new NotifyMessage("to", new Video("video")); NotifyMessage notify = new NotifyMessage("to", new Video("video"));
System.out.println(notify.toJson()); System.out.println(notifyApi.sendNotify(notify));
} }
@Test @Test
public void music() { public void music() throws WeixinException {
NotifyMessage notify = new NotifyMessage("to", new Music("music")); NotifyMessage notify = new NotifyMessage("to", new Music("music"));
System.out.println(notify.toJson()); System.out.println(notifyApi.sendNotify(notify));
} }
@Test @Test
public void news() { public void news() throws WeixinException {
News news = new News(); News news = new News();
NotifyMessage notify = new NotifyMessage("to", news); NotifyMessage notify = new NotifyMessage("to", news);
news.pushArticle("title1", "desc1", "picUrl1", "url1"); news.pushArticle("title1", "desc1", "picUrl1", "url1");
news.pushArticle("title2", "desc2", "picUrl2", "url2"); news.pushArticle("title2", "desc2", "picUrl2", "url2");
System.out.println(notify.toJson()); System.out.println(notifyApi.sendNotify(notify));
} }
@Test @Test

View File

@ -0,0 +1,20 @@
package com.foxinmy.weixin4j.mp.action;
import com.foxinmy.weixin4j.action.DebugAction;
import com.foxinmy.weixin4j.action.mapping.ActionAnnotation;
import com.foxinmy.weixin4j.msg.VideoMessage;
import com.foxinmy.weixin4j.type.MessageType;
/**
* 小视频消息处理
*
* @className ShortvideoAction
* @author jy
* @date 2015年4月6日
* @since JDK 1.7
* @see com.foxinmy.weixin4j.msg.VideoMessage
*/
@ActionAnnotation(msgType = MessageType.shortvideo)
public class ShortvideoAction extends DebugAction<VideoMessage> {
}

View File

@ -1,9 +1,12 @@
package com.foxinmy.weixin4j.qy.api; package com.foxinmy.weixin4j.qy.api;
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;
import com.foxinmy.weixin4j.http.Response; import com.foxinmy.weixin4j.http.Response;
import com.foxinmy.weixin4j.model.Token; import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.msg.model.Base;
import com.foxinmy.weixin4j.msg.model.Notifyable;
import com.foxinmy.weixin4j.qy.message.NotifyMessage; import com.foxinmy.weixin4j.qy.message.NotifyMessage;
import com.foxinmy.weixin4j.token.TokenHolder; import com.foxinmy.weixin4j.token.TokenHolder;
@ -63,11 +66,20 @@ public class NotifyApi extends QyApi {
* @see com.foxinmy.weixin4j.qy.message.NotifyMessage * @see com.foxinmy.weixin4j.qy.message.NotifyMessage
*/ */
public JSONObject sendNotify(NotifyMessage notify) throws WeixinException { public JSONObject sendNotify(NotifyMessage notify) throws WeixinException {
Base box = notify.getBox();
if (!(box instanceof Notifyable)) {
throw new WeixinException(String.format(
"%s not implement Notifyable", box.getClass()));
}
String msgtype = box.getMediaType().name();
JSONObject obj = (JSONObject) JSON.toJSON(notify);
obj.put("msgtype", msgtype);
obj.put(msgtype, box);
String message_send_uri = getRequestUri("message_send_uri"); String message_send_uri = getRequestUri("message_send_uri");
Token token = tokenHolder.getToken(); Token token = tokenHolder.getToken();
Response response = request.post( Response response = request.post(
String.format(message_send_uri, token.getAccessToken()), String.format(message_send_uri, token.getAccessToken()),
notify.toJson()); obj.toJSONString());
return response.getAsJson(); return response.getAsJson();
} }

View File

@ -5,8 +5,6 @@ import java.util.List;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.msg.model.Base; import com.foxinmy.weixin4j.msg.model.Base;
@ -55,7 +53,7 @@ public class NotifyMessage implements Serializable {
/** /**
* 消息对象 * 消息对象
*/ */
@JSONField(name = "%s") @JSONField(serialize = false)
private Base box; private Base box;
public NotifyMessage(Base box, int agentid) { public NotifyMessage(Base box, int agentid) {
@ -149,20 +147,6 @@ public class NotifyMessage implements Serializable {
this.box = box; this.box = box;
} }
/**
* 消息json化
*
* @return
* {"image":{"media_id":"123"},"agentid":0,"msgtype":"image","safe":0
* ,"touser":"@all"}
*/
public String toJson() {
String msgtype = box.getMediaType().name();
JSONObject obj = (JSONObject) JSON.toJSON(this);
obj.put("msgtype", msgtype);
return String.format(obj.toJSONString(), msgtype);
}
@Override @Override
public String toString() { public String toString() {
return "NotifyMessage [touser=" + touser + ", toparty=" + toparty return "NotifyMessage [touser=" + touser + ", toparty=" + toparty

View File

@ -36,58 +36,58 @@ public class NotifyMsgTest extends TokenTest {
} }
@Test @Test
public void text() { public void text() throws WeixinException {
NotifyMessage notify = new NotifyMessage(new Text("content"), 0); NotifyMessage notify = new NotifyMessage(new Text("content"), 0);
System.out.println(notify.toJson()); System.out.println(notifyApi.sendNotify(notify));
} }
@Test @Test
public void image() { public void image() throws WeixinException {
NotifyMessage notify = new NotifyMessage(new Image("123"), 0); NotifyMessage notify = new NotifyMessage(new Image("123"), 0);
System.out.println(notify.toJson()); System.out.println(notifyApi.sendNotify(notify));
} }
@Test @Test
public void voice() { public void voice() throws WeixinException {
NotifyMessage notify = new NotifyMessage(new Voice("123"), 0); NotifyMessage notify = new NotifyMessage(new Voice("123"), 0);
System.out.println(notify.toJson()); System.out.println(notifyApi.sendNotify(notify));
} }
@Test @Test
public void video() { public void video() throws WeixinException {
NotifyMessage notify = new NotifyMessage(new Video("123"), 0); NotifyMessage notify = new NotifyMessage(new Video("123"), 0);
System.out.println(notify.toJson()); System.out.println(notifyApi.sendNotify(notify));
} }
@Test @Test
public void file() { public void file() throws WeixinException {
File file = new File("file"); File file = new File("file");
NotifyMessage notify = new NotifyMessage(file, 0); NotifyMessage notify = new NotifyMessage(file, 0);
System.out.println(notify.toJson()); System.out.println(notifyApi.sendNotify(notify));
} }
@Test @Test
public void news() { public void news() throws WeixinException {
News news = new News(); News news = new News();
NotifyMessage notify = new NotifyMessage(news, 0); NotifyMessage notify = new NotifyMessage(news, 0);
news.pushArticle("title1", "desc1", "picUrl1", "url1"); news.pushArticle("title1", "desc1", "picUrl1", "url1");
news.pushArticle("title2", "desc2", "picUrl2", "url2"); news.pushArticle("title2", "desc2", "picUrl2", "url2");
System.out.println(notify.toJson()); System.out.println(notifyApi.sendNotify(notify));
} }
@Test @Test
public void mpnews() { public void mpnews() throws WeixinException {
MpNews news = new MpNews(); MpNews news = new MpNews();
NotifyMessage notify = new NotifyMessage(news, 0); NotifyMessage notify = new NotifyMessage(news, 0);
news.pushArticle("thumbMediaId1", "title1", "content1"); news.pushArticle("thumbMediaId1", "title1", "content1");
news.pushArticle("thumbMediaId2", "title1", "content2"); news.pushArticle("thumbMediaId2", "title1", "content2");
System.out.println(notify.toJson()); System.out.println(notifyApi.sendNotify(notify));
} }
@Test @Test
public void send1() throws WeixinException { public void send1() throws WeixinException {
Text text = new Text("this is a text"); Text text = new Text("this is a text");
JSONObject result = notifyApi.sendNotify(new NotifyMessage(text, 0)); JSONObject result = notifyApi.sendNotify(new NotifyMessage(text, 1));
Assert.assertEquals(0, result.getIntValue("errcode")); Assert.assertEquals(0, result.getIntValue("errcode"));
} }
} }