| | |
| | | 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; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.PrintWriter; |
| | | import java.security.MessageDigest; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.util.Arrays; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | @Slf4j |
| | | @RestController |
| | |
| | | |
| | | private String token = "01A9CB2234D7CBD0AC61B75EB1263805"; |
| | | |
| | | private String url = "www.taobao.com";//图文跳转地址 |
| | | private String url = "www.taobao.com";// 图文跳转地址 |
| | | |
| | | private String thumbUrl = "https://www.psciio.com//idcard/f986ba3ae7a241d9bce5cb568adec7da.jpg";//图片地址 |
| | | private String thumbUrl = "https://www.psciio.com//idcard/f986ba3ae7a241d9bce5cb568adec7da.jpg";// 图片地址 |
| | | |
| | | @Resource |
| | | private WxXCXTempSend wxXCXTempSend; |
| | |
| | | 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 }; |
| | | 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) { |
| | |
| | | response.getOutputStream().write(echostr.getBytes()); |
| | | } |
| | | |
| | | }else{ |
| | | } else { |
| | | // 进入POST聊天处理 |
| | | // 将请求、响应的编码均设置为UTF-8(防止中文乱码) |
| | | request.setCharacterEncoding("UTF-8"); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 接受到微信接口数据 |
| | | * |
| | | * @param request |
| | | * @param response |
| | | * @return |
| | | */ |
| | | private String acceptMessage(HttpServletRequest request, HttpServletResponse response) { |
| | | // {CreateTime=1548042266, Event=user_enter_tempsession, ToUserName=gh_e6198220cbff, |
| | | // FromUserName=oZvme4q2Oi7Dz3FChXc43kqw28, MsgType=event, SessionFrom=wxapp} |
| | | String respMessage = ""; |
| | | String inputLine = ""; |
| | | String notityXml = ""; |
| | | try { |
| | | //接收数据 |
| | | while((inputLine = request.getReader().readLine()) != null){ |
| | | // 接收数据 |
| | | while ((inputLine = request.getReader().readLine()) != null) { |
| | | notityXml += inputLine; |
| | | } |
| | | |
| | | // xml请求解析 |
| | | JSONObject requestJson = JSON.parseObject(notityXml); |
| | | log.info(">>>>>>>>>>>>>"+requestJson.toString()); |
| | | 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 Event = requestJson.get("Event").toString(); //SCAN 为扫描信息 VIEW 公众号底部点击事件 |
| | | log.info("fromUserName = " + fromUserName + " , ToUserName = " + toUserName + " , msgType = " + msgType); |
| | | |
| | | String access_token = wxXCXTempSend.getAccessToken();//获取access_token |
| | | String access_token = wxXCXTempSend.getAccessToken();// 获取access_token |
| | | |
| | | //公众号关注事件消息 |
| | | if(msgType.equals("event")){ |
| | | // log.info("公众号被关注事件.........."); |
| | | // if(StringUtils.isNotEmpty(access_token)){ |
| | | // WxUtil.sendKfLinkMessage(fromUserName,url,thumbUrl,access_token); //把封装好的信息推送给用户 |
| | | // } |
| | | }else if(msgType.equals("text")){ |
| | | // log.info("公众号接受文字.........."); |
| | | // if(StringUtils.isNotEmpty(access_token)){ |
| | | // WxUtil.sendKfLinkMessage(fromUserName,url,thumbUrl,access_token); |
| | | // } |
| | | }else if(msgType.equals("image")){ |
| | | // log.info("公众号接受图片.........."); |
| | | // WxUtil.sendKfLinkMessage(fromUserName,url,thumbUrl,access_token); |
| | | }else if(msgType.equals("miniprogrampage")){ |
| | | WxUtil.sendKfLinkMessage(fromUserName,url,thumbUrl,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(); |