优化代码

This commit is contained in:
jy.hu
2014-11-05 11:09:30 +08:00
parent c4eb68ebcb
commit bbd12a8f2a
82 changed files with 683 additions and 688 deletions
@@ -71,7 +71,7 @@ public class XmlResult implements Serializable {
}
public XmlResult() {
this("success", "");
this(SUCCESS.toLowerCase(), "");
}
public XmlResult(String returnCode, String returnMsg) {
@@ -0,0 +1,62 @@
package com.foxinmy.weixin4j.model;
import java.io.Serializable;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**
* 普通消息基类
* <p>
* <font color="red">回复图片等多媒体消息时需要预先上传多媒体文件到微信服务器,
* 假如服务器无法保证在五秒内处理并回复,可以直接回复空串,微信服务器不会对此作任何处理,并且不会发起重试</font>
* </p>
*
* @className BaseMessage
* @author jy.hu
* @date 2014年4月6日
* @since JDK 1.7
*/
public class BaseMsg implements Serializable {
private static final long serialVersionUID = 7761192742840031607L;
@XStreamAlias("ToUserName")
private String toUserName; // 开发者微信号
@XStreamAlias("FromUserName")
private String fromUserName; // 发送方帐号(一个OpenID
@XStreamAlias("CreateTime")
private long createTime = System.currentTimeMillis(); // 消息创建时间 (整型)
public BaseMsg() {
}
public BaseMsg(String toUserName, String fromUserName) {
this.toUserName = toUserName;
this.fromUserName = fromUserName;
}
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;
}
}
@@ -1,8 +1,8 @@
package com.foxinmy.weixin4j.msg;
import java.io.Serializable;
import java.io.Writer;
import com.foxinmy.weixin4j.model.BaseMsg;
import com.foxinmy.weixin4j.type.MessageType;
import com.foxinmy.weixin4j.util.ClassUtil;
import com.foxinmy.weixin4j.xml.XStream;
@@ -23,7 +23,7 @@ import com.thoughtworks.xstream.io.json.JsonWriter;
* @date 2014年4月6日
* @since JDK 1.7
*/
public class BaseMessage implements Serializable {
public class BaseMessage extends BaseMsg {
private static final long serialVersionUID = 7761192742840031607L;
private final static XStream xmlStream = XStream.get();
@@ -34,12 +34,6 @@ public class BaseMessage implements Serializable {
}
});
@XStreamAlias("ToUserName")
private String toUserName; // 开发者微信号
@XStreamAlias("FromUserName")
private String fromUserName; // 发送方帐号(一个OpenID
@XStreamAlias("CreateTime")
private long createTime = System.currentTimeMillis(); // 消息创建时间 (整型)
@XStreamAlias("MsgType")
private MessageType msgType; // 消息类型
@XStreamAlias("MsgId")
@@ -68,33 +62,8 @@ public class BaseMessage implements Serializable {
public BaseMessage(MessageType msgType, String toUserName,
String fromUserName) {
super(toUserName, fromUserName);
this.msgType = msgType;
this.toUserName = toUserName;
this.fromUserName = fromUserName;
}
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 MessageType getMsgType() {
@@ -16,25 +16,25 @@ import com.foxinmy.weixin4j.util.ConfigUtil;
public abstract class AbstractTokenHolder implements TokenHolder {
protected final String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
protected final HttpRequest request = new HttpRequest();
private final String appid;
private final String appsecret;
private final WeixinConfig weixinConfig;
public AbstractTokenHolder() {
WeixinConfig weixinConfig = ConfigUtil.getWeixinConfig();
this.appid = weixinConfig.getAppId();
this.appsecret = weixinConfig.getAppSecret();
this.weixinConfig = ConfigUtil.getWeixinConfig();
}
public AbstractTokenHolder(String appid, String appsecret) {
this.appid = appid;
this.appsecret = appsecret;
this.weixinConfig = new WeixinConfig(appid, appsecret);
}
protected String getAppid() {
return this.appid;
return this.weixinConfig.getAppId();
}
protected String getAppsecret() {
return this.appsecret;
return this.weixinConfig.getAppSecret();
}
public WeixinConfig getConfig() {
return this.weixinConfig;
}
}
@@ -5,6 +5,7 @@ import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisException;
import com.alibaba.fastjson.TypeReference;
import com.foxinmy.weixin4j.exception.WeixinException;
@@ -74,7 +75,7 @@ public class RedisTokenHolder extends AbstractTokenHolder {
}
token.setTime(System.currentTimeMillis());
token.setOpenid(appid);
} catch (Exception e) {
} catch (JedisException e) {
jedisPool.returnBrokenResource(jedis);
} finally {
jedisPool.returnResource(jedis);
@@ -2,6 +2,7 @@ package com.foxinmy.weixin4j.token;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.model.WeixinConfig;
/**
* 获取Token接口
@@ -16,5 +17,7 @@ import com.foxinmy.weixin4j.model.Token;
* @see com.foxinmy.weixin4j.token.RedisTokenHolder
*/
public interface TokenHolder {
public WeixinConfig getConfig();
public Token getToken() throws WeixinException;
}
@@ -1,5 +1,6 @@
package com.foxinmy.weixin4j.util;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -13,9 +14,18 @@ import java.util.Date;
* @see
*/
public class DateUtil {
private static final String YYYYMMDD = "yyyyMMdd";
private static final String yyyyMMdd = "yyyyMMdd";
private static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";
public static String fortmatYYYYMMDD(Date date) {
return new SimpleDateFormat(YYYYMMDD).format(date);
public static String fortmat2yyyyMMdd(Date date) {
return new SimpleDateFormat(yyyyMMdd).format(date);
}
public static String fortmat2yyyyMMddHHmmss(Date date) {
return new SimpleDateFormat(yyyyMMddHHmmss).format(date);
}
public static String format2fee(double fee) {
return new DecimalFormat("#").format(fee * 100);
}
}
@@ -23,8 +23,8 @@ public class Map2ObjectConverter extends MapConverter {
@Override
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) {
Map<String,String> map = new HashMap<String, String>();
while(reader.hasMoreChildren()){
Map<String, String> map = new HashMap<String, String>();
while (reader.hasMoreChildren()) {
reader.moveDown();
map.put(reader.getNodeName(), reader.getValue());
reader.moveUp();
@@ -37,12 +37,13 @@ public class Map2ObjectConverter extends MapConverter {
MarshallingContext context) {
Map<?, ?> map = (Map<?, ?>) source;
for (Entry<?, ?> entry : map.entrySet()) {
if (StringUtils.isBlank((String) entry.getValue())) {
String value = (String) entry.getValue();
if (StringUtils.isBlank(value)) {
continue;
}
ExtendedHierarchicalStreamWriterHelper.startNode(writer, entry
.getKey().toString(), entry.getClass());
writer.setValue(entry.getValue().toString());
writer.setValue(value);
writer.endNode();
}
}