整理及重构

This commit is contained in:
Daniel Qian
2014-10-22 10:29:47 +08:00
parent 67795a092d
commit a8f5d07ff3
212 changed files with 1701 additions and 3459 deletions

View File

@@ -0,0 +1,66 @@
package me.chanjar.weixin.cp.bean;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
/**
* 微信部门
*
* @author Daniel Qian
*/
public class WxCpDepart {
private Integer id;
private String name;
private Integer parentId;
private Integer order;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
public static WxCpDepart fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpDepart.class);
}
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}
@Override
public String toString() {
return "WxCpDepart{" +
"id=" + id +
", name='" + name + '\'' +
", parentId=" + parentId +
", order=" + order +
'}';
}
}

View File

@@ -0,0 +1,215 @@
package me.chanjar.weixin.cp.bean;
import java.util.ArrayList;
import java.util.List;
import me.chanjar.weixin.cp.bean.messagebuilder.*;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
/**
* 消息
* @author Daniel Qian
*
*/
public class WxCpMessage {
private String toUser;
private String toParty;
private String toTag;
private String agentId;
private String msgType;
private String content;
private String mediaId;
private String thumbMediaId;
private String title;
private String description;
private String musicUrl;
private String hqMusicUrl;
private List<WxArticle> articles = new ArrayList<WxArticle>();
public String getToUser() {
return toUser;
}
public void setToUser(String toUser) {
this.toUser = toUser;
}
public String getToParty() {
return toParty;
}
public void setToParty(String toParty) {
this.toParty = toParty;
}
public String getToTag() {
return toTag;
}
public void setToTag(String toTag) {
this.toTag = toTag;
}
public String getAgentId() {
return agentId;
}
public void setAgentId(String agentId) {
this.agentId = agentId;
}
public String getMsgType() {
return msgType;
}
/**
* <pre>
* 请使用
* {@link me.chanjar.weixin.cp.api.WxCpConsts#CUSTOM_MSG_TEXT}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#CUSTOM_MSG_IMAGE}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#CUSTOM_MSG_VOICE}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#CUSTOM_MSG_MUSIC}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#CUSTOM_MSG_VIDEO}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#CUSTOM_MSG_NEWS}
* </pre>
* @param msgType
*/
public void setMsgType(String msgType) {
this.msgType = msgType;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getMediaId() {
return mediaId;
}
public void setMediaId(String mediaId) {
this.mediaId = mediaId;
}
public String getThumbMediaId() {
return thumbMediaId;
}
public void setThumbMediaId(String thumbMediaId) {
this.thumbMediaId = thumbMediaId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getMusicUrl() {
return musicUrl;
}
public void setMusicUrl(String musicUrl) {
this.musicUrl = musicUrl;
}
public String getHqMusicUrl() {
return hqMusicUrl;
}
public void setHqMusicUrl(String hqMusicUrl) {
this.hqMusicUrl = hqMusicUrl;
}
public List<WxArticle> getArticles() {
return articles;
}
public void setArticles(List<WxArticle> articles) {
this.articles = articles;
}
public String toJson() {
return WxCpGsonBuilder.INSTANCE.create().toJson(this);
}
public static class WxArticle {
private String title;
private String description;
private String url;
private String picUrl;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
}
/**
* 获得文本消息builder
* @return
*/
public static TextBuilder TEXT() {
return new TextBuilder();
}
/**
* 获得图片消息builder
* @return
*/
public static ImageBuilder IMAGE() {
return new ImageBuilder();
}
/**
* 获得语音消息builder
* @return
*/
public static VoiceBuilder VOICE() {
return new VoiceBuilder();
}
/**
* 获得视频消息builder
* @return
*/
public static VideoBuilder VIDEO() {
return new VideoBuilder();
}
/**
* 获得图文消息builder
* @return
*/
public static NewsBuilder NEWS() {
return new NewsBuilder();
}
/**
* 获得文件消息builder
* @return
*/
public static FileBuilder FILE() {
return new FileBuilder();
}
}

View File

@@ -0,0 +1,48 @@
package me.chanjar.weixin.cp.bean;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
/**
* Created by Daniel Qian
*/
public class WxCpTag {
private String id;
private String name;
public WxCpTag() {
super();
}
public WxCpTag(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public static WxCpTag fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpTag.class);
}
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}
}

View File

@@ -0,0 +1,142 @@
package me.chanjar.weixin.cp.bean;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.util.ArrayList;
import java.util.List;
/**
* 微信用户信息
*
* @author Daniel Qian
*/
public class WxCpUser {
private String userId;
private String name;
private Integer[] departIds;
private String position;
private String mobile;
private String gender;
private String tel;
private String email;
private String weiXinId;
private final List<Attr> extAttrs = new ArrayList<Attr>();
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer[] getDepartIds() {
return departIds;
}
public void setDepartIds(Integer[] departIds) {
this.departIds = departIds;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getWeiXinId() {
return weiXinId;
}
public void setWeiXinId(String weiXinId) {
this.weiXinId = weiXinId;
}
public void addExtAttr(String name, String value) {
this.extAttrs.add(new Attr(name, value));
}
public List<Attr> getExtAttrs() {
return extAttrs;
}
public String toJson() {
return WxCpGsonBuilder.INSTANCE.create().toJson(this);
}
public static WxCpUser fromJson(String json) {
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpUser.class);
}
public static class Attr {
private String name;
private String value;
public Attr(String name, String value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}

View File

@@ -0,0 +1,648 @@
package me.chanjar.weixin.cp.bean;
import me.chanjar.weixin.common.util.xml.AdapterCDATA;
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;
import me.chanjar.weixin.cp.util.xml.XmlTransformer;
import org.apache.commons.io.IOUtils;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* <pre>
* 微信推送过来的消息也是同步回复给用户的消息xml格式
* 相关字段的解释看微信开发者文档:
* http://mp.weixin.qq.com/wiki/index.php?title=接收普通消息
* http://mp.weixin.qq.com/wiki/index.php?title=接收事件推送
* http://mp.weixin.qq.com/wiki/index.php?title=接收语音识别结果
* </pre>
*
* @author Daniel Qian
*/
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class WxCpXmlMessage {
///////////////////////
// 以下都是微信推送过来的消息的xml的element所对应的属性
///////////////////////
@XmlElement(name="AgentID")
private Integer agentId;
@XmlElement(name = "ToUserName")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String toUserName;
@XmlElement(name = "FromUserName")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String fromUserName;
@XmlElement(name = "CreateTime")
private Long createTime;
@XmlElement(name = "MsgType")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String msgType;
@XmlElement(name = "Content")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String content;
@XmlElement(name = "MsgId")
private Long msgId;
@XmlElement(name = "PicUrl")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String picUrl;
@XmlElement(name = "MediaId")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String mediaId;
@XmlElement(name = "Format")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String format;
@XmlElement(name = "ThumbMediaId")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String thumbMediaId;
@XmlElement(name = "Location_X")
private Double locationX;
@XmlElement(name = "Location_Y")
private Double locationY;
@XmlElement(name = "Scale")
private Double scale;
@XmlElement(name = "Label")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String label;
@XmlElement(name = "Title")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String title;
@XmlElement(name = "Description")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String description;
@XmlElement(name = "Url")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String url;
@XmlElement(name = "Event")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String event;
@XmlElement(name = "EventKey")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String eventKey;
@XmlElement(name = "Ticket")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String ticket;
@XmlElement(name = "Latitude")
private Double latitude;
@XmlElement(name = "Longitude")
private Double longitude;
@XmlElement(name = "Precision")
private Double precision;
@XmlElement(name = "Recognition")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String recognition;
///////////////////////////////////////
// 群发消息返回的结果
///////////////////////////////////////
/**
* 群发的结果
*/
@XmlElement(name = "Status")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String status;
/**
* group_id下粉丝数或者openid_list中的粉丝数
*/
@XmlElement(name = "TotalCount")
private Integer totalCount;
/**
* 过滤过滤是指特定地区、性别的过滤、用户设置拒收的过滤用户接收已超4条的过滤准备发送的粉丝数原则上filterCount = sentCount + errorCount
*/
@XmlElement(name = "FilterCount")
private Integer filterCount;
/**
* 发送成功的粉丝数
*/
@XmlElement(name = "SentCount")
private Integer sentCount;
/**
* 发送失败的粉丝数
*/
@XmlElement(name = "ErrorCount")
private Integer errorCount;
@XmlElement(name = "ScanCodeInfo")
private ScanCodeInfo scanCodeInfo = new ScanCodeInfo();
@XmlElement(name = "SendPicsInfo")
private SendPicsInfo sendPicsInfo = new SendPicsInfo();
@XmlElement(name = "SendLocationInfo")
private SendLocationInfo sendLocationInfo = new SendLocationInfo();
public Integer getAgentId() {
return agentId;
}
public void setAgentId(Integer agentId) {
this.agentId = agentId;
}
public String getToUserName() {
return toUserName;
}
public void setToUserName(String toUserName) {
this.toUserName = toUserName;
}
public Long getCreateTime() {
return createTime;
}
public void setCreateTime(Long createTime) {
this.createTime = createTime;
}
/**
* <pre>
* 当接受用户消息时,可能会获得以下值:
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_TEXT}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_IMAGE}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_VOICE}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_VIDEO}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_LOCATION}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_LINK}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_EVENT}
* </pre>
*
* @return
*/
public String getMsgType() {
return msgType;
}
/**
* <pre>
* 当发送消息的时候使用:
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_TEXT}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_IMAGE}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_VOICE}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_VIDEO}
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_NEWS}
* </pre>
*
* @param msgType
*/
public void setMsgType(String msgType) {
this.msgType = msgType;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Long getMsgId() {
return msgId;
}
public void setMsgId(Long msgId) {
this.msgId = msgId;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public String getMediaId() {
return mediaId;
}
public void setMediaId(String mediaId) {
this.mediaId = mediaId;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public String getThumbMediaId() {
return thumbMediaId;
}
public void setThumbMediaId(String thumbMediaId) {
this.thumbMediaId = thumbMediaId;
}
public Double getLocationX() {
return locationX;
}
public void setLocationX(Double locationX) {
this.locationX = locationX;
}
public Double getLocationY() {
return locationY;
}
public void setLocationY(Double locationY) {
this.locationY = locationY;
}
public Double getScale() {
return scale;
}
public void setScale(Double scale) {
this.scale = scale;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getEvent() {
return event;
}
public void setEvent(String event) {
this.event = event;
}
public String getEventKey() {
return eventKey;
}
public void setEventKey(String eventKey) {
this.eventKey = eventKey;
}
public String getTicket() {
return ticket;
}
public void setTicket(String ticket) {
this.ticket = ticket;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getPrecision() {
return precision;
}
public void setPrecision(Double precision) {
this.precision = precision;
}
public String getRecognition() {
return recognition;
}
public void setRecognition(String recognition) {
this.recognition = recognition;
}
public String getFromUserName() {
return fromUserName;
}
public void setFromUserName(String fromUserName) {
this.fromUserName = fromUserName;
}
protected static WxCpXmlMessage fromXml(String xml) {
try {
return XmlTransformer.fromXml(WxCpXmlMessage.class, xml);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
protected static WxCpXmlMessage fromXml(InputStream is) {
try {
return XmlTransformer.fromXml(WxCpXmlMessage.class, is);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* 从加密字符串转换
*
* @param encryptedXml
* @param wxCpConfigStorage
* @param timestamp
* @param nonce
* @param msgSignature
* @return
*/
public static WxCpXmlMessage fromEncryptedXml(
String encryptedXml,
WxCpConfigStorage wxCpConfigStorage,
String timestamp, String nonce, String msgSignature) {
WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage);
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
return fromXml(plainText);
}
public static WxCpXmlMessage fromEncryptedXml(
InputStream is,
WxCpConfigStorage wxCpConfigStorage,
String timestamp, String nonce, String msgSignature) {
try {
return fromEncryptedXml(IOUtils.toString(is, "UTF-8"), wxCpConfigStorage, timestamp, nonce, msgSignature);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getFilterCount() {
return filterCount;
}
public void setFilterCount(Integer filterCount) {
this.filterCount = filterCount;
}
public Integer getSentCount() {
return sentCount;
}
public void setSentCount(Integer sentCount) {
this.sentCount = sentCount;
}
public Integer getErrorCount() {
return errorCount;
}
public void setErrorCount(Integer errorCount) {
this.errorCount = errorCount;
}
public WxCpXmlMessage.ScanCodeInfo getScanCodeInfo() {
return scanCodeInfo;
}
public void setScanCodeInfo(WxCpXmlMessage.ScanCodeInfo scanCodeInfo) {
this.scanCodeInfo = scanCodeInfo;
}
public WxCpXmlMessage.SendPicsInfo getSendPicsInfo() {
return sendPicsInfo;
}
public void setSendPicsInfo(WxCpXmlMessage.SendPicsInfo sendPicsInfo) {
this.sendPicsInfo = sendPicsInfo;
}
public WxCpXmlMessage.SendLocationInfo getSendLocationInfo() {
return sendLocationInfo;
}
public void setSendLocationInfo(WxCpXmlMessage.SendLocationInfo sendLocationInfo) {
this.sendLocationInfo = sendLocationInfo;
}
@XmlRootElement(name = "ScanCodeInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public static class ScanCodeInfo {
@XmlElement(name = "ScanType")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String scanType;
@XmlElement(name = "ScanResult")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String scanResult;
/**
* 扫描类型一般是qrcode
* @return
*/
public String getScanType() {
return scanType;
}
public void setScanType(String scanType) {
this.scanType = scanType;
}
/**
* 扫描结果,即二维码对应的字符串信息
* @return
*/
public String getScanResult() {
return scanResult;
}
public void setScanResult(String scanResult) {
this.scanResult = scanResult;
}
}
@XmlRootElement(name = "SendPicsInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public static class SendPicsInfo {
@XmlElement(name = "Count")
private Long count;
@XmlElementWrapper(name="PicList")
@XmlElement(name = "item")
protected final List<Item> picList = new ArrayList<Item>();
public Long getCount() {
return count;
}
public void setCount(Long count) {
this.count = count;
}
public List<Item> getPicList() {
return picList;
}
@XmlRootElement(name = "item")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "WxXmlMessage.SendPicsInfo.Item")
public static class Item {
@XmlElement(name = "PicMd5Sum")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String PicMd5Sum;
public String getPicMd5Sum() {
return PicMd5Sum;
}
public void setPicMd5Sum(String picMd5Sum) {
PicMd5Sum = picMd5Sum;
}
}
}
@XmlRootElement(name = "SendLocationInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public static class SendLocationInfo {
@XmlElement(name = "Location_X")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String locationX;
@XmlElement(name = "Location_Y")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String locationY;
@XmlElement(name = "Scale")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String scale;
@XmlElement(name = "Label")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String label;
@XmlElement(name = "Poiname")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String poiname;
public String getLocationX() {
return locationX;
}
public void setLocationX(String locationX) {
this.locationX = locationX;
}
public String getLocationY() {
return locationY;
}
public void setLocationY(String locationY) {
this.locationY = locationY;
}
public String getScale() {
return scale;
}
public void setScale(String scale) {
this.scale = scale;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getPoiname() {
return poiname;
}
public void setPoiname(String poiname) {
this.poiname = poiname;
}
}
}

View File

@@ -0,0 +1,32 @@
package me.chanjar.weixin.cp.bean;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import me.chanjar.weixin.cp.api.WxCpConsts;
import me.chanjar.weixin.common.util.xml.MediaIdMarshaller;
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class WxCpXmlOutImageMessage extends WxCpXmlOutMessage {
@XmlElement(name="Image")
@XmlJavaTypeAdapter(MediaIdMarshaller.class)
private String mediaId;
public WxCpXmlOutImageMessage() {
this.msgType = WxCpConsts.XML_MSG_IMAGE;
}
public String getMediaId() {
return mediaId;
}
public void setMediaId(String mediaId) {
this.mediaId = mediaId;
}
}

View File

@@ -0,0 +1,124 @@
package me.chanjar.weixin.cp.bean;
import me.chanjar.weixin.common.util.xml.AdapterCDATA;
import me.chanjar.weixin.cp.api.WxCpConfigStorage;
import me.chanjar.weixin.cp.bean.outxmlbuilder.*;
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;
import me.chanjar.weixin.cp.util.xml.XmlTransformer;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class WxCpXmlOutMessage {
@XmlElement(name="ToUserName")
@XmlJavaTypeAdapter(AdapterCDATA.class)
protected String toUserName;
@XmlElement(name="FromUserName")
@XmlJavaTypeAdapter(AdapterCDATA.class)
protected String fromUserName;
@XmlElement(name="CreateTime")
protected Long createTime;
@XmlElement(name="MsgType")
@XmlJavaTypeAdapter(AdapterCDATA.class)
protected String msgType;
public String getToUserName() {
return toUserName;
}
public void setToUserName(String toUserName) {
this.toUserName = toUserName;
}
public String getFromUserName() {
return fromUserName;
}
public void setFromUserName(String fromUserName) {
this.fromUserName = fromUserName;
}
public Long getCreateTime() {
return createTime;
}
public void setCreateTime(Long createTime) {
this.createTime = createTime;
}
public String getMsgType() {
return msgType;
}
public void setMsgType(String msgType) {
this.msgType = msgType;
}
protected String toXml() {
try {
return XmlTransformer.toXml((Class)this.getClass(), this);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* 转换成加密的xml格式
* @return
*/
public String toEncryptedXml(WxCpConfigStorage wxCpConfigStorage) {
String plainXml = toXml();
WxCpCryptUtil pc = new WxCpCryptUtil(wxCpConfigStorage);
return pc.encrypt(plainXml);
}
/**
* 获得文本消息builder
* @return
*/
public static TextBuilder TEXT() {
return new TextBuilder();
}
/**
* 获得图片消息builder
* @return
*/
public static ImageBuilder IMAGE() {
return new ImageBuilder();
}
/**
* 获得语音消息builder
* @return
*/
public static VoiceBuilder VOICE() {
return new VoiceBuilder();
}
/**
* 获得视频消息builder
* @return
*/
public static VideoBuilder VIDEO() {
return new VideoBuilder();
}
/**
* 获得图文消息builder
* @return
*/
public static NewsBuilder NEWS() {
return new NewsBuilder();
}
}

View File

@@ -0,0 +1,100 @@
package me.chanjar.weixin.cp.bean;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import me.chanjar.weixin.cp.api.WxCpConsts;
import me.chanjar.weixin.common.util.xml.AdapterCDATA;
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class WxCpXmlOutMewsMessage extends WxCpXmlOutMessage {
@XmlElement(name = "ArticleCount")
protected int articleCount;
@XmlElementWrapper(name="Articles")
@XmlElement(name = "item")
protected final List<Item> articles = new ArrayList<Item>();
public WxCpXmlOutMewsMessage() {
this.msgType = WxCpConsts.XML_MSG_NEWS;
}
public int getArticleCount() {
return articleCount;
}
public void addArticle(Item item) {
this.articles.add(item);
this.articleCount = this.articles.size();
}
public List<Item> getArticles() {
return articles;
}
@XmlRootElement(name = "Item")
@XmlAccessorType(XmlAccessType.FIELD)
public static class Item {
@XmlElement(name = "Title")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String Title;
@XmlElement(name = "Description")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String Description;
@XmlElement(name="PicUrl")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String PicUrl;
@XmlElement(name="Url")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String Url;
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getPicUrl() {
return PicUrl;
}
public void setPicUrl(String picUrl) {
PicUrl = picUrl;
}
public String getUrl() {
return Url;
}
public void setUrl(String url) {
Url = url;
}
}
}

View File

@@ -0,0 +1,33 @@
package me.chanjar.weixin.cp.bean;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import me.chanjar.weixin.cp.api.WxCpConsts;
import me.chanjar.weixin.common.util.xml.AdapterCDATA;
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class WxCpXmlOutTextMessage extends WxCpXmlOutMessage {
@XmlElement(name="Content")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String content;
public WxCpXmlOutTextMessage() {
this.msgType = WxCpConsts.XML_MSG_TEXT;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}

View File

@@ -0,0 +1,90 @@
package me.chanjar.weixin.cp.bean;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import me.chanjar.weixin.cp.api.WxCpConsts;
import me.chanjar.weixin.common.util.xml.AdapterCDATA;
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class WxCpXmlOutVideoMessage extends WxCpXmlOutMessage {
@XmlElement(name = "Video")
protected final Video video = new Video();
public WxCpXmlOutVideoMessage() {
this.msgType = WxCpConsts.XML_MSG_VIDEO;
}
public String getMediaId() {
return video.getMediaId();
}
public void setMediaId(String mediaId) {
video.setMediaId(mediaId);
}
public String getTitle() {
return video.getTitle();
}
public void setTitle(String title) {
video.setTitle(title);
}
public String getDescription() {
return video.getDescription();
}
public void setDescription(String description) {
video.setDescription(description);
}
@XmlRootElement(name = "Video")
@XmlAccessorType(XmlAccessType.FIELD)
private static class Video {
@XmlElement(name = "MediaId")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String mediaId;
@XmlElement(name = "Title")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String title;
@XmlElement(name = "Description")
@XmlJavaTypeAdapter(AdapterCDATA.class)
private String description;
public String getMediaId() {
return mediaId;
}
public void setMediaId(String mediaId) {
this.mediaId = mediaId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
}

View File

@@ -0,0 +1,32 @@
package me.chanjar.weixin.cp.bean;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import me.chanjar.weixin.cp.api.WxCpConsts;
import me.chanjar.weixin.common.util.xml.MediaIdMarshaller;
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class WxCpXmlOutVoiceMessage extends WxCpXmlOutMessage {
@XmlElement(name="Voice")
@XmlJavaTypeAdapter(MediaIdMarshaller.class)
private String mediaId;
public WxCpXmlOutVoiceMessage() {
this.msgType = WxCpConsts.XML_MSG_VOICE;
}
public String getMediaId() {
return mediaId;
}
public void setMediaId(String mediaId) {
this.mediaId = mediaId;
}
}

View File

@@ -0,0 +1,42 @@
package me.chanjar.weixin.cp.bean.messagebuilder;
import me.chanjar.weixin.cp.bean.WxCpMessage;
public class BaseBuilder<T> {
protected String msgType;
protected String agentId;
protected String toUser;
protected String toParty;
protected String toTag;
public T agentId(String agentId) {
this.agentId = agentId;
return (T) this;
}
public T toUser(String toUser) {
this.toUser = toUser;
return (T) this;
}
public T toParty(String toParty) {
this.toParty = toParty;
return (T) this;
}
public T toTag(String toTag) {
this.toTag = toTag;
return (T) this;
}
public WxCpMessage build() {
WxCpMessage m = new WxCpMessage();
m.setAgentId(this.agentId);
m.setMsgType(this.msgType);
m.setToUser(this.toUser);
m.setToParty(this.toParty);
m.setToTag(this.toTag);
return m;
}
}

View File

@@ -0,0 +1,31 @@
package me.chanjar.weixin.cp.bean.messagebuilder;
import me.chanjar.weixin.cp.api.WxCpConsts;
import me.chanjar.weixin.cp.bean.WxCpMessage;
/**
* 获得消息builder
* <pre>
* 用法: WxCustomMessage m = WxCustomMessage.FILE().mediaId(...).toUser(...).build();
* </pre>
* @author Daniel Qian
*
*/
public final class FileBuilder extends BaseBuilder<FileBuilder> {
private String mediaId;
public FileBuilder() {
this.msgType = WxCpConsts.CUSTOM_MSG_FILE;
}
public FileBuilder mediaId(String media_id) {
this.mediaId = media_id;
return this;
}
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setMediaId(this.mediaId);
return m;
}
}

View File

@@ -0,0 +1,31 @@
package me.chanjar.weixin.cp.bean.messagebuilder;
import me.chanjar.weixin.cp.api.WxCpConsts;
import me.chanjar.weixin.cp.bean.WxCpMessage;
/**
* 获得消息builder
* <pre>
* 用法: WxCustomMessage m = WxCustomMessage.IMAGE().mediaId(...).toUser(...).build();
* </pre>
* @author Daniel Qian
*
*/
public final class ImageBuilder extends BaseBuilder<ImageBuilder> {
private String mediaId;
public ImageBuilder() {
this.msgType = WxCpConsts.CUSTOM_MSG_IMAGE;
}
public ImageBuilder mediaId(String media_id) {
this.mediaId = media_id;
return this;
}
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setMediaId(this.mediaId);
return m;
}
}

View File

@@ -0,0 +1,36 @@
package me.chanjar.weixin.cp.bean.messagebuilder;
import java.util.ArrayList;
import java.util.List;
import me.chanjar.weixin.cp.api.WxCpConsts;
import me.chanjar.weixin.cp.bean.WxCpMessage;
/**
* 图文消息builder
* <pre>
* 用法:
* WxCustomMessage m = WxCustomMessage.NEWS().addArticle(article).toUser(...).build();
* </pre>
* @author Daniel Qian
*
*/
public final class NewsBuilder extends BaseBuilder<NewsBuilder> {
private List<WxCpMessage.WxArticle> articles = new ArrayList<WxCpMessage.WxArticle>();
public NewsBuilder() {
this.msgType = WxCpConsts.CUSTOM_MSG_NEWS;
}
public NewsBuilder addArticle(WxCpMessage.WxArticle article) {
this.articles.add(article);
return this;
}
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setArticles(this.articles);
return m;
}
}

View File

@@ -0,0 +1,31 @@
package me.chanjar.weixin.cp.bean.messagebuilder;
import me.chanjar.weixin.cp.api.WxCpConsts;
import me.chanjar.weixin.cp.bean.WxCpMessage;
/**
* 文本消息builder
* <pre>
* 用法: WxCustomMessage m = WxCustomMessage.TEXT().content(...).toUser(...).build();
* </pre>
* @author Daniel Qian
*
*/
public final class TextBuilder extends BaseBuilder<TextBuilder> {
private String content;
public TextBuilder() {
this.msgType = WxCpConsts.CUSTOM_MSG_TEXT;
}
public TextBuilder content(String content) {
this.content = content;
return this;
}
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setContent(this.content);
return m;
}
}

View File

@@ -0,0 +1,58 @@
package me.chanjar.weixin.cp.bean.messagebuilder;
import me.chanjar.weixin.cp.api.WxCpConsts;
import me.chanjar.weixin.cp.bean.WxCpMessage;
/**
* 视频消息builder
* <pre>
* 用法: WxCustomMessage m = WxCustomMessage.VOICE()
* .mediaId(...)
* .title(...)
* .thumbMediaId(..)
* .description(..)
* .toUser(...)
* .build();
* </pre>
* @author Daniel Qian
*
*/
public final class VideoBuilder extends BaseBuilder<VideoBuilder> {
private String mediaId;
private String title;
private String description;
private String thumbMediaId;
public VideoBuilder() {
this.msgType = WxCpConsts.CUSTOM_MSG_VIDEO;
}
public VideoBuilder mediaId(String mediaId) {
this.mediaId = mediaId;
return this;
}
public VideoBuilder title(String title) {
this.title = title;
return this;
}
public VideoBuilder description(String description) {
this.description = description;
return this;
}
public VideoBuilder thumbMediaId(String thumb_media_id) {
this.thumbMediaId = thumb_media_id;
return this;
}
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setMediaId(this.mediaId);
m.setTitle(title);
m.setDescription(description);
m.setThumbMediaId(thumbMediaId);
return m;
}
}

View File

@@ -0,0 +1,31 @@
package me.chanjar.weixin.cp.bean.messagebuilder;
import me.chanjar.weixin.cp.api.WxCpConsts;
import me.chanjar.weixin.cp.bean.WxCpMessage;
/**
* 语音消息builder
* <pre>
* 用法: WxCustomMessage m = WxCustomMessage.VOICE().mediaId(...).toUser(...).build();
* </pre>
* @author Daniel Qian
*
*/
public final class VoiceBuilder extends BaseBuilder<VoiceBuilder> {
private String mediaId;
public VoiceBuilder() {
this.msgType = WxCpConsts.CUSTOM_MSG_VOICE;
}
public VoiceBuilder mediaId(String media_id) {
this.mediaId = media_id;
return this;
}
public WxCpMessage build() {
WxCpMessage m = super.build();
m.setMediaId(this.mediaId);
return m;
}
}

View File

@@ -0,0 +1,29 @@
package me.chanjar.weixin.cp.bean.outxmlbuilder;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
public abstract class BaseBuilder<BuilderType, ValueType> {
protected String toUserName;
protected String fromUserName;
public BuilderType toUser(String touser) {
this.toUserName = touser;
return (BuilderType) this;
}
public BuilderType fromUser(String fromusername) {
this.fromUserName = fromusername;
return (BuilderType) this;
}
public abstract ValueType build();
public void setCommon(WxCpXmlOutMessage m) {
m.setToUserName(this.toUserName);
m.setFromUserName(this.fromUserName);
m.setCreateTime(System.currentTimeMillis() / 1000l);
}
}

View File

@@ -0,0 +1,25 @@
package me.chanjar.weixin.cp.bean.outxmlbuilder;
import me.chanjar.weixin.cp.bean.WxCpXmlOutImageMessage;
/**
* 图片消息builder
* @author Daniel Qian
*/
public final class ImageBuilder extends BaseBuilder<ImageBuilder, WxCpXmlOutImageMessage> {
private String mediaId;
public ImageBuilder mediaId(String media_id) {
this.mediaId = media_id;
return this;
}
public WxCpXmlOutImageMessage build() {
WxCpXmlOutImageMessage m = new WxCpXmlOutImageMessage();
setCommon(m);
m.setMediaId(this.mediaId);
return m;
}
}

View File

@@ -0,0 +1,31 @@
package me.chanjar.weixin.cp.bean.outxmlbuilder;
import java.util.ArrayList;
import java.util.List;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMewsMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMewsMessage.Item;
/**
* 图文消息builder
* @author Daniel Qian
*/
public final class NewsBuilder extends BaseBuilder<NewsBuilder, WxCpXmlOutMewsMessage> {
protected final List<Item> articles = new ArrayList<Item>();
public NewsBuilder addArticle(Item item) {
this.articles.add(item);
return this;
}
public WxCpXmlOutMewsMessage build() {
WxCpXmlOutMewsMessage m = new WxCpXmlOutMewsMessage();
for(Item item : articles) {
m.addArticle(item);
}
setCommon(m);
return m;
}
}

View File

@@ -0,0 +1,24 @@
package me.chanjar.weixin.cp.bean.outxmlbuilder;
import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage;
/**
* 文本消息builder
* @author Daniel Qian
*
*/
public final class TextBuilder extends BaseBuilder<TextBuilder, WxCpXmlOutTextMessage> {
private String content;
public TextBuilder content(String content) {
this.content = content;
return this;
}
public WxCpXmlOutTextMessage build() {
WxCpXmlOutTextMessage m = new WxCpXmlOutTextMessage();
setCommon(m);
m.setContent(this.content);
return m;
}
}

View File

@@ -0,0 +1,38 @@
package me.chanjar.weixin.cp.bean.outxmlbuilder;
import me.chanjar.weixin.cp.bean.WxCpXmlOutVideoMessage;
/**
* 视频消息builder
* @author Daniel Qian
*
*/
public final class VideoBuilder extends BaseBuilder<VideoBuilder, WxCpXmlOutVideoMessage> {
private String mediaId;
private String title;
private String description;
public VideoBuilder title(String title) {
this.title = title;
return this;
}
public VideoBuilder description(String description) {
this.description = description;
return this;
}
public VideoBuilder mediaId(String mediaId) {
this.mediaId = mediaId;
return this;
}
public WxCpXmlOutVideoMessage build() {
WxCpXmlOutVideoMessage m = new WxCpXmlOutVideoMessage();
setCommon(m);
m.setTitle(title);
m.setDescription(description);
m.setMediaId(mediaId);
return m;
}
}

View File

@@ -0,0 +1,25 @@
package me.chanjar.weixin.cp.bean.outxmlbuilder;
import me.chanjar.weixin.cp.bean.WxCpXmlOutVoiceMessage;
/**
* 语音消息builder
* @author Daniel Qian
*/
public final class VoiceBuilder extends BaseBuilder<VoiceBuilder, WxCpXmlOutVoiceMessage> {
private String mediaId;
public VoiceBuilder mediaId(String mediaId) {
this.mediaId = mediaId;
return this;
}
public WxCpXmlOutVoiceMessage build() {
WxCpXmlOutVoiceMessage m = new WxCpXmlOutVoiceMessage();
setCommon(m);
m.setMediaId(mediaId);
return m;
}
}