微信帮助类

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;


import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
import org.zttx.bert.wx.parser.WxMsgKit;
import org.zttx.bert.wx.vo.recv.WxRecvMsg;
import org.zttx.bert.wx.vo.send.WxSendMsg;


/**
 * @ClassName: WeiXin.java
 * @Description: 微信帮助类
 * @author Bert
 * 2014-3-31 下午10:10:13
 */
public final class WeiXin {

/**
* 认证接口
* @param token
* @param signature
* @param timestamp
* @param nonce
* @return
*/
public static boolean access(String token, String signature, String timestamp, String nonce) {
List<String> ss = new ArrayList<String>();
ss.add(timestamp);
ss.add(nonce);
ss.add(token);
Collections.sort(ss);
StringBuilder builder = new StringBuilder();
for (String s : ss) {
builder.append(s);
}
return signature.equalsIgnoreCase(HashKit.sha1(builder.toString()));
}


/**
* 解析微信请求
* @param in
* @return
* @throws JDOMException
* @throws IOException
*/
public static WxRecvMsg recv(InputStream in) throws JDOMException, IOException {
return WxMsgKit.parse(in);
}


public static void send(WxSendMsg msg, OutputStream out) throws JDOMException, IOException {
Document doc = WxMsgKit.parse(msg);
if (null != doc) {
new XMLOutputter().output(doc, out);
} else {
Logger.getAnonymousLogger().warning("发送消息时,解析出dom为空 msg :" + msg);
}
}


/**
* 绑定xml数据
* @param msg
* @return
*/
public static WxSendMsg builderSendByRecv(WxRecvMsg msg) {
WxRecvMsg m = new WxRecvMsg(msg);
String from = m.getFromUser();
m.setFromUser(m.getToUser());
m.setToUser(from);
m.setCreateDt((System.currentTimeMillis() / 1000) + "");
return new WxSendMsg(m);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值