lidongdong
2022-11-18 97bb97c13ad63c40a8baf73b0f5e08d93c0e7559
springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/WxMessagePushApi.java
@@ -1,21 +1,23 @@
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
@@ -24,9 +26,9 @@
    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;
@@ -51,8 +53,9 @@
                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) {
@@ -77,7 +80,7 @@
                    response.getOutputStream().write(echostr.getBytes());
                }
            }else{
            } else {
                // 进入POST聊天处理
                // 将请求、响应的编码均设置为UTF-8(防止中文乱码)
                request.setCharacterEncoding("UTF-8");
@@ -96,9 +99,9 @@
        }
    }
    /**
     * 接受到微信接口数据
     *
     * @param request
     * @param response
     * @return
@@ -108,40 +111,40 @@
        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("公众号被关注事件..........");
            }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);
            // 公众号关注事件消息
            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) {