微信官方文档 - 小程序

/**
 * 客服功能 - 自动回复小程序APP链接
 * Created by Lance on 2020/10/10 17:52
 */
@Slf4j
@Api(description = "客服功能")
@ApiPath("/api/wm-user/autoResponse")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AutoResponseApi {

    private final AutoResponseService autoResponseService;

    @Value("${token.access_token}")
    private final String access_token;

    private final CrmUtils crmUtils; // 用于获取微信的 tocken

    @ApiOperation("微信消息通知-GET请求校验(确认请求来自微信服务器)")
    @GetMapping("/getSignature")
    public String signature(HttpServletRequest request, HttpServletResponse response) {
        //微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");
        log.info("echostr>>>>>>>>>>" + echostr);
        //通过检验signature对请求进行校验,若校验成功则原样返回echostr,否则接入失败
        if (checkSignature(signature, timestamp, nonce)) {
            log.info("echostr>>>>>>>>>>>>" + echostr);
            return echostr;
        }
        log.info("客服消息验证url验证结果:{}", echostr);

        return null;
    }

    @ApiOperation("微信消息通知-POST请求校验(确认请求来自微信服务器)")
    @PostMapping("/postSignature")
    public String response(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 接收消息并返回消息
            String result = acceptMessage(request, response);
            log.info("接受消息并返回消息result>>>>>>" + result);
            // 响应消息
            PrintWriter out = response.getWriter();
            log.info("out>>>>>>>>" + out);
            out.print(result);
            out.close();
        } catch (Exception ex) {
            log.error("微信帐号接口配置失败!" + ex.getMessage());
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * 校验(确认请求来自微信服务器)
     *
     * @param signature
     * @param timestamp
     * @param nonce
     * @return
     */
    public boolean checkSignature(String signature, String timestamp, String nonce) {
        String[] params = {access_token, timestamp, nonce};
        log.info("params>>>>>>>>>>>>>" + Arrays.toString(params));
        //1、将token、timestamp、nonce三个参数进行字典序排序
        Arrays.sort(params);
        //2、将三个参数字符串拼接成一个字符串
        StringBuilder sb = new StringBuilder();
        for (String param : params) {
            sb.append(param);
        }
        log.info("字符串拼接>>>>>>>>>>>>>>>>>" + sb);
        MessageDigest messageDigest = null;
        String tpmStr = null;
        try {
            messageDigest = MessageDigest.getInstance("SHA-1");
            log.info("md>>>>>>>>>>>>" + messageDigest);
            //3、将三个参数字符串拼接成一个字符串进行sha1加密
            byte[] digest = messageDigest.digest(sb.toString().getBytes());
            log.info("digest>>>>>>>>>>>" + Arrays.toString(digest));
            tpmStr = StringUtils.byteToHex(digest);
            log.info("加密串>>>>>>>>>>>" + tpmStr);
        } catch (Exception e) {
            log.info("错误信息>>>>>>>>>>>>" + e.getMessage());
            e.printStackTrace();
        }
        //4、将sha1加密后的字符串可与signature对比,标识该请求来源于微信
        return tpmStr != null && tpmStr.equals(signature);
    }

    public String acceptMessage(HttpServletRequest request, HttpServletResponse response) {
        //返回值
        String result = "success";
        try {
            // 将请求、响应的编码均设置为UTF-8(防止中文乱码)
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            Map<String, String> requestMap = MessageUtils.parseXml(request);
            log.info("requestMap>>>>>>>>>>" + requestMap);
            // 发送者的openid
            String fromUserName = requestMap.get("FromUserName");
            // 小程序的原始ID
            String toUserName = requestMap.get("ToUserName");
            // 消息类型
            String msgType = requestMap.get("MsgType");
            // 文本消息内容
            String content = requestMap.get("Content");
            // 事件类型
            String event = requestMap.get("Event");
            log.info("fromUserName=" + fromUserName + ",toUserName=" + toUserName + ",msgType=" + msgType + ",content=" + content + ",event=" + event);
            StringBuffer contentMessage = new StringBuffer();
            if (msgType.equals("event")) {
                contentMessage.append("欢迎进入威马客服咨询!人工服务请输入0").append("\n");
                log.info(autoResponseService.sendCustomerTextMessage(fromUserName, contentMessage.toString(), crmUtils.getToken()));
            } else if (msgType.equals("text")) {//文本消息
                if (content.equals("0")) {
                    return autoResponseService.switchCustomerService(fromUserName, toUserName);
                } else {
                    contentMessage.append("欢迎进入威马客服咨询!人工服务请输入0").append("\n");
                    log.info(autoResponseService.sendCustomerTextMessage(fromUserName, contentMessage.toString(), crmUtils.getToken()));
                }
            } else {
                contentMessage.append("欢迎进入XXX客服咨询!人工服务请输入0").append("\n");
                log.info(autoResponseService.sendCustomerTextMessage(fromUserName, contentMessage.toString(), crmUtils.getToken()));
            }
        } catch (Exception e) {
            log.info("错误信息打印>>>>>>>>>>>" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }


}
/**
 * 客服功能 - 字符串转换
 * Created by Lance on 2020/10/12 10:52
 */
@Slf4j
public class StringUtils {
    /**
     * 十六进制字节数组转为字符串
     *
     * @param hash
     * @return
     */
    public static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }
}
/**
 * 客服功能 - 消息工具类
 * Created by Lance on 2020/10/10 17:52
 */
public class MessageUtils {

    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();
        return map;
    }
}
/**
 * 客服功能 - 消息发送请求工具类
 * Created by Lance on 2020/10/10 17:52
 */
public class HttpUtils {

    /**
     * Get 发送的请求
     * @param url
     * @return
     */
    public static String sendGetRequest(String url) {
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject(url, String.class);
    }

    public static String sendPostRequest(String url, HttpMethod method, HttpEntity<Map<String, Object>> httpEntity) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.exchange(url, method, httpEntity, String.class);
        return response.getBody();
    }
}
/**
 * 客服功能 - 自动回复小程序APP链接
 * Created by Lance on 2020/10/10 11:03
 */
@Slf4j
@Service
public class AutoResponseService {

    /**
     * 人工服务
     *
     * @param fromUserName
     * @param toUserName
     * @param requestMap
     * @return
     */
    public String switchCustomerService(String fromUserName, String toUserName) {
        String returnXml = "<xml>\n" +
                "    <ToUserName><![CDATA[" + fromUserName + "]]></ToUserName>\n" +
                "    <FromUserName><![CDATA[" + toUserName + "]]></FromUserName>\n" +
                "    <CreateTime>" + System.currentTimeMillis() / 1000 + "</CreateTime>\n" +
                "    <MsgType><![CDATA[transfer_customer_service]]></MsgType>\n" +
                "</xml>";
        log.info("人工服务result>>>>>>>" + returnXml);
        return returnXml;
    }
    /**
     * 文本消息
     *
     * @param openid
     * @param text
     * @param accessToken
     * @return
     */
    private static String SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send";//固定发送地址

    public String sendCustomerTextMessage(String openid, String text, String accessToken) throws IOException {
        HashMap<String, Object> map_content = new HashMap<>();
        map_content.put("content", text);
        HashMap<String, Object> map = new HashMap<>();
        map.put("touser", openid);
        map.put("msgtype", "text");
        map.put("text", map_content);
        String content = JSON.toJSONString(map);
        log.info("文本消息content" + content);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity httpEntity = new HttpEntity(map, headers);
        String str = HttpUtils.sendPostRequest(SEND_URL + "?access_token=" + accessToken, HttpMethod.POST, httpEntity);
        log.info("文本消息str>>>>>>>>>>"+str);
        return str;
    }
}