<commit_before>package com.java110.front.controller;


import com.java110.core.base.controller.BaseController;
import com.java110.core.factory.AuthenticationFactory;
import com.java110.core.factory.WechatFactory;
import com.java110.utils.constant.WechatConstant;
import com.java110.utils.util.StringUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

/**
 * @ClassName WechatController
 * @Description TODO 微信 对接类
 * @Author wuxw
 * @Date 2020/6/13 21:48
 * @Version 1.0
 * add by wuxw 2020/6/13
 **/
@RestController
@RequestMapping(path = "/app/wechat")
public class WechatGatewayController extends BaseController {

    private final static Logger logger = LoggerFactory.getLogger(WechatGatewayController.class);


    /**
     * 微信登录接口
     *
     * @param request
     */
    @RequestMapping(path = "/gateway", method = RequestMethod.GET)
    public ResponseEntity<String> gateway(HttpServletRequest request) {

        String token = WechatConstant.TOKEN;
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        String responseStr = "";
        logger.debug("token = " + token + "||||" + "signature = " + signature + "|||" + "timestamp = "
                + timestamp + "|||" + "nonce = " + nonce + "|||" + "echostr = " + echostr);
        String sourceString = "";
        String[] ss = new String[]{token, timestamp, nonce};
        Arrays.sort(ss);
        for (String s : ss) {
            sourceString += s;
        }
        String signature1 = AuthenticationFactory.SHA1Encode(sourceString).toLowerCase();
        logger.debug("sourceString = " + sourceString + "||||" + "signature1 = " + signature1);
        try {
            if (signature1.equals(signature)) {
                if (echostr == null) {
                    String postStr = this.readStreamParameter(request.getInputStream());
                    if (StringUtil.isEmpty(postStr)) {
                        responseStr = "未输入任何内容";
                    } else {
                    /*if (postStr.equals(new String(postStr.getBytes("ISO-8859-1"), "ISO-8859-1"))) {
                        logger.debug(" This type is  iso-8859-1");
                        postStr = new String(postStr.getBytes("ISO-8859-1"), "UTF-8");

                    }
                    if (postStr.equals(new String(postStr.getBytes("GB2312"), "GB2312"))) {
                        logger.debug(" This type is  GB2312" + postStr);
                        postStr = new String(postStr.getBytes("GB2312"), "UTF-8");
                        logger.debug(" change postStr to utf-8 " + postStr);
                    }
                    if (postStr.equals(new String(postStr.getBytes("GBK"), "GBK"))) {
                        logger.debug(" This type is  GBK");
                        postStr = new String(postStr.getBytes("GBK"), "UTF-8");

                    }
                    postStr = new String(postStr.getBytes("GB2312"), "UTF-8");*/
                        Document document = DocumentHelper.parseText(postStr);
                        Element root = document.getRootElement();
                        String fromUserName = root.elementText("FromUserName");
                        String toUserName = root.elementText("ToUserName");
                        String keyword = root.elementTextTrim("Content");
                        String msgType = root.elementTextTrim("MsgType");
                        String event = root.elementText("Event");
                        String eventKey = root.elementText("EventKey");
                        if (WechatConstant.MSG_TYPE_TEXT.equals(msgType)) {
                            responseStr = textResponseHandler(fromUserName, toUserName, keyword);
                        } else if (WechatConstant.MSG_TYPE_EVENT.equals(msgType)) {
                            responseStr = eventResponseHandler(fromUserName, toUserName, keyword, event, eventKey);
                        } else {
                            responseStr = eventResponseHandler(fromUserName, toUserName, keyword, event, eventKey);
                        }

                    }
                } else {
                    responseStr = echostr;
                }
                logger.debug(">>>>>>>>>>>>>..responseStr>>>>>>>>>>>>>>>" + responseStr);
            } else {
                responseStr = "亲，非法访问，签名失败";
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error("处理失败", e);
            responseStr = "亲，网络超时，请稍后重试";
        }

        return new ResponseEntity<String>(responseStr, HttpStatus.OK);
    }

    /**
     * 微信登录接口
     *
     * @param request
     */
    @RequestMapping(path = "/gateway", method = RequestMethod.POST)
    public ResponseEntity<String> gateway(@RequestBody String param, HttpServletRequest request) {

        String token = WechatConstant.TOKEN;
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String responseStr = "";
        logger.debug("token = " + token + "||||" + "signature = " + signature + "|||" + "timestamp = "
                + timestamp + "|||" + "nonce = " + nonce);
        String sourceString = "";
        String[] ss = new String[]{token, timestamp, nonce};
        Arrays.sort(ss);
        for (String s : ss) {
            sourceString += s;
        }
        String signature1 = AuthenticationFactory.SHA1Encode(sourceString).toLowerCase();
        logger.debug("sourceString = " + sourceString + "||||" + "signature1 = " + signature1);
        try {
            if (signature1.equals(signature)) {
                String postStr = param;//this.readStreamParameter(request.getInputStream());
                if (StringUtil.isEmpty(postStr)) {
                    responseStr = "未输入任何内容";
                } else {
                    /*if (postStr.equals(new String(postStr.getBytes("ISO-8859-1"), "ISO-8859-1"))) {
                        logger.debug(" This type is  iso-8859-1");
                        postStr = new String(postStr.getBytes("ISO-8859-1"), "UTF-8");

                    }
                    if (postStr.equals(new String(postStr.getBytes("GB2312"), "GB2312"))) {
                        logger.debug(" This type is  GB2312" + postStr);
                        postStr = new String(postStr.getBytes("GB2312"), "UTF-8");
                        logger.debug(" change postStr to utf-8 " + postStr);
                    }
                    if (postStr.equals(new String(postStr.getBytes("GBK"), "GBK"))) {
                        logger.debug(" This type is  GBK");
                        postStr = new String(postStr.getBytes("GBK"), "UTF-8");

                    }
                    postStr = new String(postStr.getBytes("GB2312"), "UTF-8");*/
                    Document document = DocumentHelper.parseText(postStr);<commit_msg>优化 微信对接代码<commit_after>package com.java110.front.controller;


import com.java110.core.base.controller.BaseController;
import com.java110.core.factory.AuthenticationFactory;
import com.java110.core.factory.WechatFactory;
import com.java110.utils.constant.WechatConstant;
import com.java110.utils.util.StringUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

/**
 * @ClassName WechatController
 * @Description TODO 微信 对接类
 * @Author wuxw
 * @Date 2020/6/13 21:48
 * @Version 1.0
 * add by wuxw 2020/6/13
 **/
@RestController
@RequestMapping(path = "/app/wechat")
public class WechatGatewayController extends BaseController {

    private final static Logger logger = LoggerFactory.getLogger(WechatGatewayController.class);


    /**
     * 微信登录接口
     *
     * @param request
     */
    @RequestMapping(path = "/gateway", method = RequestMethod.GET)
    public ResponseEntity<String> gateway(HttpServletRequest request) {

        String token = WechatConstant.TOKEN;
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        String responseStr = "";
        logger.debug("token = " + token + "||||" + "signature = " + signature + "|||" + "timestamp = "
                + timestamp + "|||" + "nonce = " + nonce + "|||" + "echostr = " + echostr);
        String sourceString = "";
        String[] ss = new String[]{token, timestamp, nonce};
        Arrays.sort(ss);
        for (String s : ss) {
            sourceString += s;
        }
        String signature1 = AuthenticationFactory.SHA1Encode(sourceString).toLowerCase();
        logger.debug("sourceString = " + sourceString + "||||" + "signature1 = " + signature1);
        try {
            if (signature1.equals(signature)) {
                if (echostr == null) {
                    String postStr = this.readStreamParameter(request.getInputStream());
                    if (StringUtil.isEmpty(postStr)) {
                        responseStr = "未输入任何内容";
                    } else {
                    /*if (postStr.equals(new String(postStr.getBytes("ISO-8859-1"), "ISO-8859-1"))) {
                        logger.debug(" This type is  iso-8859-1");
                        postStr = new String(postStr.getBytes("ISO-8859-1"), "UTF-8");

                    }
                    if (postStr.equals(new String(postStr.getBytes("GB2312"), "GB2312"))) {
                        logger.debug(" This type is  GB2312" + postStr);
                        postStr = new String(postStr.getBytes("GB2312"), "UTF-8");
                        logger.debug(" change postStr to utf-8 " + postStr);
                    }
                    if (postStr.equals(new String(postStr.getBytes("GBK"), "GBK"))) {
                        logger.debug(" This type is  GBK");
                        postStr = new String(postStr.getBytes("GBK"), "UTF-8");

                    }
                    postStr = new String(postStr.getBytes("GB2312"), "UTF-8");*/
                        Document document = DocumentHelper.parseText(postStr);
                        Element root = document.getRootElement();
                        String fromUserName = root.elementText("FromUserName");
                        String toUserName = root.elementText("ToUserName");
                        String keyword = root.elementTextTrim("Content");
                        String msgType = root.elementTextTrim("MsgType");
                        String event = root.elementText("Event");
                        String eventKey = root.elementText("EventKey");
                        if (WechatConstant.MSG_TYPE_TEXT.equals(msgType)) {
                            responseStr = textResponseHandler(fromUserName, toUserName, keyword);
                        } else if (WechatConstant.MSG_TYPE_EVENT.equals(msgType)) {
                            responseStr = eventResponseHandler(fromUserName, toUserName, keyword, event, eventKey);
                        } else {
                            responseStr = eventResponseHandler(fromUserName, toUserName, keyword, event, eventKey);
                        }

                    }
                } else {
                    responseStr = echostr;
                }
                logger.debug(">>>>>>>>>>>>>..responseStr>>>>>>>>>>>>>>>" + responseStr);
            } else {
                responseStr = "亲，非法访问，签名失败";
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error("处理失败", e);
            responseStr = "亲，网络超时，请稍后重试";
        }

        return new ResponseEntity<String>(responseStr, HttpStatus.OK);
    }

    /**
     * 微信登录接口
     *
     * @param request
     */
    @RequestMapping(path = "/gateway", method = RequestMethod.POST)
    public ResponseEntity<String> gateway(@RequestBody String param, HttpServletRequest request) {

        String token = WechatConstant.TOKEN;
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String openId = request.getParameter("openid");
        String responseStr = "";
        logger.debug("token = " + token + "||||" + "signature = " + signature + "|||" + "timestamp = "
                + timestamp + "|||" + "nonce = " + nonce + "|||| param = " + param + "|||| openId= " + openId);
        String sourceString = "";
        String[] ss = new String[]{token, timestamp, nonce};
        Arrays.sort(ss);
        for (String s : ss) {
            sourceString += s;
        }
        String signature1 = AuthenticationFactory.SHA1Encode(sourceString).toLowerCase();
        logger.debug("sourceString = " + sourceString + "||||" + "signature1 = " + signature1);
        try {
            if (signature1.equals(signature)) {
                String postStr = param;//this.readStreamParameter(request.getInputStream());
                if (StringUtil.isEmpty(postStr)) {
                    responseStr = "未输入任何内容";
                } else {
                    /*if (postStr.equals(new String(postStr.getBytes("ISO-8859-1"), "ISO-8859-1"))) {
                        logger.debug(" This type is  iso-8859-1");
                        postStr = new String(postStr.getBytes("ISO-8859-1"), "UTF-8");

                    }
                    if (postStr.equals(new String(postStr.getBytes("GB2312"), "GB2312"))) {
                        logger.debug(" This type is  GB2312" + postStr);
                        postStr = new String(postStr.getBytes("GB2312"), "UTF-8");
                        logger.debug(" change postStr to utf-8 " + postStr);
                    }
                    if (postStr.equals(new String(postStr.getBytes("GBK"), "GBK"))) {
                        logger.debug(" This type is  GBK");
                        postStr = new String(postStr.getBytes("GBK"), "UTF-8");

                    }
                    postStr = new String(postStr.getBytes("GB2312"), "UTF-8");*/
                    Document document = DocumentHelper.parseText(postStr);
{'fraction_non_alphanumeric': 0.12733817574082407, 'fraction_numerical': 0.01838400719933149, 'mean_word_length': 2.0151162790697676, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3637684', 'n_tokens_mistral': 4379, 'n_tokens_neox': 4142, 'n_words': 791}