New file |
| | |
| | | package com.panzhihua.applets.api; |
| | | |
| | | import java.io.PrintWriter; |
| | | import java.security.MessageDigest; |
| | | import java.util.Arrays; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.panzhihua.common.utlis.StringUtils; |
| | | import com.panzhihua.common.utlis.WxUtil; |
| | | import com.panzhihua.common.utlis.WxXCXTempSend; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | @Slf4j |
| | | @RestController |
| | | @RequestMapping("/wx/push/") |
| | | public class WxMessagePushApi { |
| | | |
| | | private String token = "01A9CB2234D7CBD0AC61B75EB1263805"; |
| | | |
| | | private String url = "www.taobao.com";// 图文跳转地址 |
| | | |
| | | private String thumbUrl = "https://www.psciio.com//idcard/f986ba3ae7a241d9bce5cb568adec7da.jpg";// 图片地址 |
| | | |
| | | @Resource |
| | | private WxXCXTempSend wxXCXTempSend; |
| | | |
| | | /** |
| | | * 微信接口配置信息认证接口<br> |
| | | * 需要正确响应微信发送的Token验证。 加密/校验流程如下:<br> |
| | | * 1. 将token、timestamp、nonce三个参数进行字典序排序<br> |
| | | * 2. 将三个参数字符串拼接成一个字符串进行sha1加密<br> |
| | | * 3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信 |
| | | */ |
| | | @RequestMapping("/cgi") |
| | | public void cgi(HttpServletRequest request, HttpServletResponse response) { |
| | | boolean isGet = request.getMethod().toLowerCase().equals("get"); |
| | | // 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。 |
| | | try { |
| | | if (isGet) { |
| | | String signature = request.getParameter("signature"); |
| | | // 时间戳 |
| | | String timestamp = request.getParameter("timestamp"); |
| | | // 随机数 |
| | | String nonce = request.getParameter("nonce"); |
| | | // 随机字符串 |
| | | String echostr = request.getParameter("echostr"); |
| | | log.info("signature = " + signature + " , timestamp = " + timestamp + " , nonce = " + nonce |
| | | + " , echostr = " + echostr); |
| | | String[] strArray = new String[] {token, timestamp, nonce}; |
| | | Arrays.sort(strArray); |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (String str : strArray) { |
| | | sb.append(str); |
| | | } |
| | | // SHA1签名生成 |
| | | MessageDigest md = MessageDigest.getInstance("SHA-1"); |
| | | md.update(sb.toString().getBytes()); |
| | | byte[] digest = md.digest(); |
| | | |
| | | StringBuffer hexstr = new StringBuffer(); |
| | | String shaHex = ""; |
| | | for (int i = 0; i < digest.length; i++) { |
| | | shaHex = Integer.toHexString(digest[i] & 0xFF); |
| | | if (shaHex.length() < 2) { |
| | | hexstr.append(0); |
| | | } |
| | | hexstr.append(shaHex); |
| | | } |
| | | |
| | | if (hexstr.toString().equals(signature)) { |
| | | response.getOutputStream().write(echostr.getBytes()); |
| | | } |
| | | |
| | | } else { |
| | | // 进入POST聊天处理 |
| | | // 将请求、响应的编码均设置为UTF-8(防止中文乱码) |
| | | request.setCharacterEncoding("UTF-8"); |
| | | response.setCharacterEncoding("UTF-8"); |
| | | // 接收消息并返回消息 |
| | | String result = acceptMessage(request, response); |
| | | |
| | | // 响应消息 |
| | | PrintWriter out = response.getWriter(); |
| | | out.print(result); |
| | | out.close(); |
| | | } |
| | | } catch (Exception ex) { |
| | | log.error("微信帐号接口配置失败!", ex); |
| | | ex.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 接受到微信接口数据 |
| | | * |
| | | * @param request |
| | | * @param response |
| | | * @return |
| | | */ |
| | | private String acceptMessage(HttpServletRequest request, HttpServletResponse response) { |
| | | String respMessage = ""; |
| | | String inputLine = ""; |
| | | String notityXml = ""; |
| | | try { |
| | | // 接收数据 |
| | | while ((inputLine = request.getReader().readLine()) != null) { |
| | | notityXml += inputLine; |
| | | } |
| | | |
| | | // xml请求解析 |
| | | JSONObject requestJson = JSON.parseObject(notityXml); |
| | | log.info(">>>>>>>>>>>>>" + requestJson.toString()); |
| | | // 发送方帐号(open_id) |
| | | String fromUserName = requestJson.get("FromUserName").toString(); |
| | | // 公众帐号 |
| | | String toUserName = requestJson.get("ToUserName").toString(); |
| | | // 消息类型 |
| | | String msgType = requestJson.get("MsgType").toString(); |
| | | // String Event = requestJson.get("Event").toString(); //SCAN 为扫描信息 VIEW 公众号底部点击事件 |
| | | log.info("fromUserName = " + fromUserName + " , ToUserName = " + toUserName + " , msgType = " + msgType); |
| | | |
| | | String access_token = wxXCXTempSend.getAccessToken();// 获取access_token |
| | | |
| | | // 公众号关注事件消息 |
| | | if (msgType.equals("event")) { |
| | | // log.info("公众号被关注事件.........."); |
| | | } else if (msgType.equals("text")) { |
| | | // if(StringUtils.isNotEmpty(access_token)){ |
| | | // String mediaId = wxXCXTempSend.getMediaId(access_token);//获取mediaId |
| | | // WxUtil.sendKfImagesMessage(fromUserName,access_token,mediaId); |
| | | // } |
| | | // log.info("公众号接受文字.........."); |
| | | } else if (msgType.equals("image")) { |
| | | // log.info("公众号接受图片.........."); |
| | | } else if (msgType.equals("miniprogrampage")) { |
| | | if (StringUtils.isNotEmpty(access_token)) { |
| | | String mediaId = wxXCXTempSend.getMediaId(access_token);// 获取mediaId |
| | | WxUtil.sendKfImagesMessage(fromUserName, access_token, mediaId); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return respMessage; |
| | | } |
| | | |
| | | } |