参考文章
http://www.cnblogs.com/txw1958/p/weixin-qrcode-with-parameters.html
http://tuposky.iteye.com/blog/2082230
https://github.com/caspar-chen/WechatSDK
http://www.cnblogs.com/txw1958/p/weixin-qrcode-with-parameters.html
http://tuposky.iteye.com/blog/2082230
https://github.com/caspar-chen/WechatSDK
@Action("wxAction")
public class WeixinAction extends ActionSupport {
public static final Logger logger = Logger.getLogger(WeixinAction.class);
@Autowired
private WXUserService wxUserService;
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
HttpServletRequest req = ServletActionContext.getRequest();
PrintWriter out = ServletActionContext.getResponse().getWriter();
String method = req.getMethod();
if (WXConstants.METHOD_GET.equals(method)) {
// 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数
String signature = req.getParameter("signature");
// 时间戳
String timestamp = req.getParameter("timestamp");
// 随机数
String nonce = req.getParameter("nonce");
// 随机字符串
String echostr = req.getParameter("echostr");
// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
if (SignUtil.checkSignature(signature, timestamp, nonce)) {
out.print(echostr);
}
} else if (WXConstants.METHOD_POST.equals(method)) {
// xml请求解析
Map<String, String> reqMap = MessageUtil.parseXml(req);
// 消息类型
String msgType = reqMap.get("MsgType");
if (msgType.equals(WXConstants.REQ_MESSAGE_TYPE_EVENT)) {
String eventType = reqMap.get("Event");
String openID = reqMap.get("FromUserName");
String ticket = reqMap.get("Ticket");
// 获取用户基本信息
String nickname = WXInterfaceUtil.getNickname(openID);
// 用户未关注时
if (eventType.equals(WXConstants.EVENT_TYPE_SUBSCRIBE)) {
logger.info("新关注用户openID:" + openID);
}
// 用户已关注
else if (eventType.equals(WXConstants.EVENT_TYPE_SCAN)) {
logger.info("已关注用户openID:" + openID);
}
// 更新数据库
wxUserService.updateUserInfoByTicket(ticket, nickname, openID);
}
}
out.close();
out = null;
return null;
}
}
@Action("qrcodeCreateAction")
public class QrcodeCreateAction extends ActionSupport {
@Autowired
private WXUserService wxUserService;
/**
*
*/
private static final long serialVersionUID = -7930634700220791363L;
@Override
public String execute() throws Exception {
PrintWriter out = ServletActionContext.getResponse().getWriter();
// 获取传输的参数SN
String sn = ServletActionContext.getRequest().getParameter("sn");
// 生成临时二维码,每次生成二维码时ticket不一样,当用户扫描二维码时,可根据ticket更新关联的记录
// 不能用永久二维码,因为永久二维码每次产生的ticket每次都一样
String accessToken = WXInterfaceUtil.getAccessToken();
String ticket = WXInterfaceUtil.createTempQrcode(accessToken);
if(null == ticket){
WXInterfaceUtil.access_token = null;
accessToken = WXInterfaceUtil.getAccessToken();
ticket = WXInterfaceUtil.createTempQrcode(accessToken);
}
//将当前请求的数据存入数据库,如果当前sn在数据库中已有记录,更新ticket,如果没有,新增一条记录
wxUserService.saveOrUpdateUserInfo(sn, ticket);
JSONObject obj = new JSONObject();
//如果取ticket失败
if(null == ticket){
obj.put("errcode", "99");
obj.put("errmsg", "failure");
}else{
//组装生成二维码URL,get请求
String qrCodeUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket;
obj.put("qrCodeUrl", qrCodeUrl);
obj.put("errcode", "0");
obj.put("errmsg", "success");
}
out.print(obj.toString());
return null;
}
}
@Service
public class WXUserServiceImpl implements WXUserService {
@Autowired
private WXUserDao wxUserDao;
/**
* 当安卓客户端请求二维码时,如果当前请求的盒子信息没有保存在数据库中,则新增数据至数据库。
* 如果当前盒子已经请求过二维码,将ticket更新,并且清空用户信息
*
* @param sn
* @param ticket
*/
public void saveOrUpdateUserInfo(String sn,String ticket){
WXUser user = wxUserDao.getBySN(sn);
//如果用户不存在
if(null == user){
WXUser u = new WXUser();
u.setSn(sn);
u.setTicket(ticket);
u.setCreateTime(new Date());
wxUserDao.save(u);
}
//用户已存在,更新ticket,清空用户信息
else{
user.setTicket(ticket);
user.setCreateTime(new Date());
user.setNickname("");
user.setOpenId("");
user.setLastLoginTime(null);
user.setFlag(0);
wxUserDao.update(user);
}
}
/**
* 用户扫描二维码,用户根据ticket更新用户信息
*
* @param ticket
* @param nickname
* @param openId
*/
public void updateUserInfoByTicket(String ticket,String nickname,String openId){
WXUser user = wxUserDao.getByTicket(ticket);
user.setNickname(nickname);
user.setOpenId(openId);
user.setLastLoginTime(new Date());
wxUserDao.update(user);
}
}
public class MessageUtil {
/**
* 解析微信发来的请求(XML)
*
* @param request
* @return
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
// 将解析结果存储在HashMap中
Map<String, String> map = new HashMap<String, String>();
// 从request中取得输入流
InputStream inputStream = request.getInputStream();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList)
map.put(e.getName(), e.getText());
// 释放资源
inputStream.close();
inputStream = null;
return map;
}
}
public class ReqUtil {
private static final Logger logger = Logger.getLogger(ReqUtil.class);
/**
* POST 请求
*
* @param reqUrl
* @param reqParams
* @return String
*/
public static String post(String reqUrl, String params) {
logger.info("POST Request,Url:" + reqUrl);
DataOutputStream outStream = null;
HttpURLConnection conn = null;
String result = "";
try {
byte[] data = params.toString().getBytes();
URL realUrl = new URL(reqUrl);
conn = (HttpURLConnection) realUrl
.openConnection();
conn.setDoOutput(true);// 发送POST请求必须设置允许输出
conn.setUseCaches(false);// 不使用Cache
conn.setRequestMethod(WXConstants.METHOD_POST);
conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Length",
String.valueOf(data.length));
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
outStream = new DataOutputStream(
conn.getOutputStream());
outStream.write(data);
outStream.flush();
if (conn.getResponseCode() == 200) {
result = readData(conn.getInputStream(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally{
if(null != outStream){
try {
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != conn){
conn.disconnect();
}
}
return result;
}
/**
* Get 请求
*
* @param reqUrl
* @return String
*/
public static String get(String reqUrl) {
logger.info("GET Request,Url:" + reqUrl);
InputStream is = null;
HttpURLConnection conn = null;
String result = "";
try {
URL url = new URL(reqUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);// 设置连接超时
conn.setRequestMethod(WXConstants.METHOD_GET);// 以get方式发起请求
if (conn.getResponseCode() != 200)
throw new RuntimeException("请求url失败");
is = conn.getInputStream();// 得到网络返回的输入流
result = readData(is, "UTF-8");
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
} finally{
if(null != is){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != conn){
conn.disconnect();
}
}
return result;
}
/**
* 第一个参数为输入流,第二个参数为字符集编码
*
* @param inSream
* @param charsetName
* @return String
* @throws Exception
*/
private static String readData(InputStream inSream, String charsetName)
throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inSream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inSream.close();
return new String(data, charsetName);
}
}
public class SignUtil {
// Token可由开发者任意填写,用作生成签名
private static final String token = "test";
/**
* 检验signature对请求进行校验,确认此次GET请求来自微信服务器
*
* @param signature
* @param token
* @param timestamp
* @param nonce
* @return boolean
*/
public static boolean checkSignature(String signature,String timestamp,String nonce){
String[] arr = new String[]{token, timestamp, nonce};
// 将token、timestamp、nonce三个参数进行字典序排序
Arrays.sort(arr);
// 拼装成字符串
StringBuilder content = new StringBuilder();
for(String str : arr){
content.append(str);
}
// sha1加密
MessageDigest md = null;
String tmpStr = null;
try {
md = MessageDigest.getInstance("SHA-1");
// 将三个参数字符串拼接成一个字符串进行sha1加密
byte[] digest = md.digest(content.toString().getBytes());
tmpStr = byteToStr(digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
content = null;
// 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
}
/**
* 将字节数组转换为十六进制字符串
*
* @param byteArray
* @return String
*/
private static String byteToStr(byte[] byteArray) {
StringBuilder strDigest = new StringBuilder();
for (int i = 0; i < byteArray.length; i++) {
strDigest.append(byteToHexStr(byteArray[i]));
}
return strDigest.toString();
}
/**
* 将字节转换为十六进制字符串
*
* @param mByte
* @return String
*/
private static String byteToHexStr(byte mByte) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char[] tempArr = new char[2];
tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
tempArr[1] = Digit[mByte & 0X0F];
String s = new String(tempArr);
return s;
}
}
public class WXInterfaceUtil {
private static final Logger logger = Logger
.getLogger(WXInterfaceUtil.class);
/**
* access_token有效期为7200秒,不需要每次都去调用
*/
public static String access_token = null;
/**
* 获取access_token
*
* @return String
*/
public static String getAccessToken() {
if (null == access_token) {
// 获取ACCESS_TOKEN
String reqUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+ WXConstants.APPID + "&secret=" + WXConstants.APPSECRET;
String result = ReqUtil.get(reqUrl);
logger.info("get access token,result:" + result);
JSONObject obj = JSONObject.fromObject(result);
access_token = (String) obj.get("access_token");
logger.info("access_token: " + access_token);
}
return access_token;
}
/**
* 获取用户呢称
*
* @param openID
* @return String
*/
public static String getNickname(String openID) {
String reqUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="
+ getAccessToken() + "&openid=" + openID + "&lang=zh_CN";
String result = ReqUtil.get(reqUrl);
logger.info("get nickname,result:" + result);
JSONObject obj = JSONObject.fromObject(result);
String nickname = (String) obj.get("nickname");
logger.info("nickname: " + nickname);
return nickname;
}
/**
* 创建临时二维码
*
* @return String
*/
public static String createTempQrcode(String accessToken) {
String reqUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="
+ accessToken;
String params = "{\"expire_seconds\": " + WXConstants.EXPIRE_SECONDS
+ ", \"action_name\": \"" + WXConstants.ACTION_NAME
+ "\", \"action_info\": {\"scene\": {\"scene_id\": "
+ WXConstants.SCENE_ID + "}}}";
String result = ReqUtil.post(reqUrl, params);
logger.info("create temp qrcode,result:" + result);
JSONObject obj = JSONObject.fromObject(result);
String ticket = (String)obj.get("ticket");
logger.info("ticket: " + ticket);
return ticket;
}
}