优化了代码
This commit is contained in:
parent
d447775d3e
commit
01e68ec835
@ -20,8 +20,7 @@ import com.foxinmy.weixin4j.xml.XStream;
|
|||||||
* @see com.foxinmy.weixin4j.action.WeixinAction
|
* @see com.foxinmy.weixin4j.action.WeixinAction
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public abstract class AbstractAction<M extends BaseMsg> implements
|
public abstract class AbstractAction<M extends BaseMsg> implements WeixinAction {
|
||||||
WeixinAction {
|
|
||||||
|
|
||||||
public abstract ResponseMessage execute(M inMessage);
|
public abstract ResponseMessage execute(M inMessage);
|
||||||
|
|
||||||
@ -29,11 +28,7 @@ public abstract class AbstractAction<M extends BaseMsg> implements
|
|||||||
public ResponseMessage execute(String msg) throws DocumentException {
|
public ResponseMessage execute(String msg) throws DocumentException {
|
||||||
BaseMsg message = MessageUtil.xml2msg(msg);
|
BaseMsg message = MessageUtil.xml2msg(msg);
|
||||||
if (message == null) {
|
if (message == null) {
|
||||||
Class<M> messageClass = getGenericType();
|
return execute(XStream.get(msg, getGenericType()));
|
||||||
XStream xstream = XStream.get();
|
|
||||||
xstream.processAnnotations(messageClass);
|
|
||||||
xstream.alias("xml", messageClass);
|
|
||||||
return execute(xstream.fromXML(msg, messageClass));
|
|
||||||
}
|
}
|
||||||
return execute((M) message);
|
return execute((M) message);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
消息处理接口,与weixin4j-*-server配合使用
|
消息处理接口,与weixin4j-*-server配合使用
|
||||||
|
|
||||||
如果只使用API包,则可以不关注
|
如果单纯只使用API包,则可以不关注
|
||||||
@ -2,39 +2,21 @@ package com.foxinmy.weixin4j.exception;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 调用微信支付抛出的异常
|
* 调用微信支付抛出的异常
|
||||||
|
*
|
||||||
* @className PayException
|
* @className PayException
|
||||||
* @author jy
|
* @author jy
|
||||||
* @date 2014年10月28日
|
* @date 2014年10月28日
|
||||||
* @since JDK 1.7
|
* @since JDK 1.7
|
||||||
* @see
|
* @see
|
||||||
*/
|
*/
|
||||||
public class PayException extends Exception {
|
public class PayException extends WeixinException {
|
||||||
|
|
||||||
private static final long serialVersionUID = 7148145661883468514L;
|
private static final long serialVersionUID = 7148145661883468514L;
|
||||||
|
|
||||||
private String errorCode;
|
public PayException(String errorMsg) {
|
||||||
private String errorMsg;
|
super(errorMsg);
|
||||||
|
}
|
||||||
|
|
||||||
public PayException(String errorCode, String errorMsg) {
|
public PayException(String errorCode, String errorMsg) {
|
||||||
this.errorCode = errorCode;
|
super(errorCode, errorMsg);
|
||||||
this.errorMsg = errorMsg;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PayException(String errorMsg) {
|
|
||||||
this.errorCode = "-1";
|
|
||||||
this.errorMsg = errorMsg;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getErrorCode() {
|
|
||||||
return this.errorCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getErrorMsg() {
|
|
||||||
return this.errorMsg;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getMessage() {
|
|
||||||
return String.format("%s,%s", getErrorMsg(), getErrorCode());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,7 +22,7 @@ public class WeixinException extends Exception {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public WeixinException(String errorMsg) {
|
public WeixinException(String errorMsg) {
|
||||||
this.errorCode = "";
|
this.errorCode = "-1";
|
||||||
this.errorMsg = errorMsg;
|
this.errorMsg = errorMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,12 @@
|
|||||||
package com.foxinmy.weixin4j.model;
|
package com.foxinmy.weixin4j.model;
|
||||||
|
/**
|
||||||
|
* 常量类
|
||||||
|
* @className Consts
|
||||||
|
* @author jy
|
||||||
|
* @date 2014年12月3日
|
||||||
|
* @since JDK 1.7
|
||||||
|
* @see
|
||||||
|
*/
|
||||||
public final class Consts {
|
public final class Consts {
|
||||||
public static final String SUCCESS = "SUCCESS";
|
public static final String SUCCESS = "SUCCESS";
|
||||||
public static final String FAIL = "FAIL";
|
public static final String FAIL = "FAIL";
|
||||||
|
|||||||
@ -14,7 +14,8 @@ import com.thoughtworks.xstream.annotations.XStreamAlias;
|
|||||||
* @date 2014年4月5日
|
* @date 2014年4月5日
|
||||||
* @since JDK 1.7
|
* @since JDK 1.7
|
||||||
* @see <a
|
* @see <a
|
||||||
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96access_token">获取token</a>
|
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96access_token">微信公众平台获取token</a>
|
||||||
|
* @see <a href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E5%8A%A8%E8%B0%83%E7%94%A8">微信企业号的主动模式</a>
|
||||||
*/
|
*/
|
||||||
@XStreamAlias("app-token")
|
@XStreamAlias("app-token")
|
||||||
public class Token implements Serializable {
|
public class Token implements Serializable {
|
||||||
|
|||||||
@ -103,7 +103,7 @@ public abstract class WeixinAccount implements Serializable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "WeixinAccount [id=" + id + ", secret=" + secret + ", token="
|
return "id=" + id + ", secret=" + secret + ", token=" + token
|
||||||
+ token + ", encodingAesKey=" + encodingAesKey + "]";
|
+ ", encodingAesKey=" + encodingAesKey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -171,8 +171,6 @@ public class WeixinMpAccount extends WeixinAccount {
|
|||||||
+ partnerKey + ", mchId=" + mchId + ", deviceInfo="
|
+ partnerKey + ", mchId=" + mchId + ", deviceInfo="
|
||||||
+ deviceInfo + ", version=" + version + ", isAlive=" + isAlive
|
+ deviceInfo + ", version=" + version + ", isAlive=" + isAlive
|
||||||
+ ", isService=" + isService + ", isSubscribe=" + isSubscribe
|
+ ", isService=" + isService + ", isSubscribe=" + isSubscribe
|
||||||
+ ", getId()=" + getId() + ", getSecret()=" + getSecret()
|
+ ", " + super.toString() + "]";
|
||||||
+ ", getToken()=" + getToken() + ", getEncodingAesKey()="
|
|
||||||
+ getEncodingAesKey() + "]";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,9 +26,6 @@ public class WeixinQyAccount extends WeixinAccount {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "WeixinQyAccount [getTokenUrl()=" + getTokenUrl() + ", getId()="
|
return "WeixinQyAccount [" + super.toString() + "]";
|
||||||
+ getId() + ", getSecret()=" + getSecret() + ", getToken()="
|
|
||||||
+ getToken() + ", getEncodingAesKey()=" + getEncodingAesKey()
|
|
||||||
+ "]";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,17 +44,8 @@ public class LocationEventMessage extends EventMessage {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
return "LocationEventMessage [latitude=" + latitude + ", longitude="
|
||||||
sb.append("[LocationEventMessage ,toUserName=").append(
|
+ longitude + ", precision=" + precision + ", "
|
||||||
super.getToUserName());
|
+ super.toString() + "]";
|
||||||
sb.append(" ,fromUserName=").append(super.getFromUserName());
|
|
||||||
sb.append(" ,msgType=").append(super.getMsgType());
|
|
||||||
sb.append(" ,eventType=").append(super.getEventType().name());
|
|
||||||
sb.append(" ,longitude=").append(longitude);
|
|
||||||
sb.append(" ,latitude=").append(latitude);
|
|
||||||
sb.append(" ,precision=").append(precision);
|
|
||||||
sb.append(" ,createTime=").append(super.getCreateTime());
|
|
||||||
sb.append(" ,msgId=").append(super.getMsgId()).append("]");
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -582,7 +582,7 @@ public class PayApi extends MpApi {
|
|||||||
String param = map2xml(map);
|
String param = map2xml(map);
|
||||||
response = request.post(refundquery_uri, param);
|
response = request.post(refundquery_uri, param);
|
||||||
}
|
}
|
||||||
return new RefundConverter().fromXML(response.getAsString());
|
return RefundConverter.fromXML(response.getAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -28,7 +28,7 @@ public class TmplApi extends MpApi {
|
|||||||
/**
|
/**
|
||||||
* 发送模板消息
|
* 发送模板消息
|
||||||
*
|
*
|
||||||
* @param message
|
* @param message 消息对象
|
||||||
* @return 发送结果
|
* @return 发送结果
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
* @see <a
|
* @see <a
|
||||||
@ -40,9 +40,8 @@ public class TmplApi extends MpApi {
|
|||||||
throws WeixinException {
|
throws WeixinException {
|
||||||
Token token = tokenHolder.getToken();
|
Token token = tokenHolder.getToken();
|
||||||
String template_send_uri = getRequestUri("template_send_uri");
|
String template_send_uri = getRequestUri("template_send_uri");
|
||||||
String para = JSON.toJSONString(tplMessage);
|
|
||||||
Response response = request.post(
|
Response response = request.post(
|
||||||
String.format(template_send_uri, token.getAccessToken()), para);
|
String.format(template_send_uri, token.getAccessToken()), JSON.toJSONString(tplMessage));
|
||||||
|
|
||||||
return response.getAsJsonResult();
|
return response.getAsJsonResult();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -56,7 +56,7 @@ public class PayUtil {
|
|||||||
return createPayJsRequestJsonV3((PayPackageV3) payPackage,
|
return createPayJsRequestJsonV3((PayPackageV3) payPackage,
|
||||||
weixinAccount);
|
weixinAccount);
|
||||||
}
|
}
|
||||||
throw new PayException("-1", "unknown pay");
|
throw new PayException("unknown pay");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -31,25 +31,26 @@ import com.thoughtworks.xstream.mapper.Mapper;
|
|||||||
*/
|
*/
|
||||||
public class RefundConverter {
|
public class RefundConverter {
|
||||||
private final static XStream xStream = XStream.get();
|
private final static XStream xStream = XStream.get();
|
||||||
private final Mapper mapper;
|
private final static Mapper mapper;
|
||||||
private final ReflectionProvider reflectionProvider;
|
private final static ReflectionProvider reflectionProvider;
|
||||||
|
private final static Pattern pattern = Pattern.compile("(_\\d)$");
|
||||||
public RefundConverter() {
|
static {
|
||||||
xStream.processAnnotations(Refund.class);
|
xStream.processAnnotations(Refund.class);
|
||||||
xStream.registerConverter(new RefundConverter.$());
|
xStream.processAnnotations(RefundDetail.class);
|
||||||
this.mapper = xStream.getMapper();
|
xStream.registerConverter(new $());
|
||||||
this.reflectionProvider = xStream.getReflectionProvider();
|
mapper = xStream.getMapper();
|
||||||
|
reflectionProvider = xStream.getReflectionProvider();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toXML(Refund refund) {
|
public static String toXML(Refund refund) {
|
||||||
return xStream.toXML(refund);
|
return xStream.toXML(refund);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Refund fromXML(String xml) {
|
public static Refund fromXML(String xml) {
|
||||||
return xStream.fromXML(xml, Refund.class);
|
return xStream.fromXML(xml, Refund.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class $ implements Converter {
|
private static class $ implements Converter {
|
||||||
@Override
|
@Override
|
||||||
public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) {
|
public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) {
|
||||||
return clazz.equals(Refund.class);
|
return clazz.equals(Refund.class);
|
||||||
@ -67,7 +68,6 @@ public class RefundConverter {
|
|||||||
public Object unmarshal(HierarchicalStreamReader reader,
|
public Object unmarshal(HierarchicalStreamReader reader,
|
||||||
UnmarshallingContext context) {
|
UnmarshallingContext context) {
|
||||||
Refund refund = new Refund();
|
Refund refund = new Refund();
|
||||||
Pattern pattern = Pattern.compile("(_\\d)$");
|
|
||||||
Matcher matcher = null;
|
Matcher matcher = null;
|
||||||
Map<String, Map<String, String>> outMap = new HashMap<String, Map<String, String>>();
|
Map<String, Map<String, String>> outMap = new HashMap<String, Map<String, String>>();
|
||||||
while (reader.hasMoreChildren()) {
|
while (reader.hasMoreChildren()) {
|
||||||
@ -108,9 +108,8 @@ public class RefundConverter {
|
|||||||
detailXml.append("</").append(detailCanonicalName).append(">");
|
detailXml.append("</").append(detailCanonicalName).append(">");
|
||||||
}
|
}
|
||||||
detailXml.append("</list>");
|
detailXml.append("</list>");
|
||||||
xStream.processAnnotations(RefundDetail.class);
|
|
||||||
refund.setDetails(xStream.fromXML(detailXml.toString(), List.class));
|
refund.setDetails(xStream.fromXML(detailXml.toString(), List.class));
|
||||||
return refund;
|
return refund;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user