You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					59 lines
				
				1.8 KiB
			
		
		
			
		
	
	
					59 lines
				
				1.8 KiB
			| 
								 
											3 years ago
										 
									 | 
							
								package com.ynxbd.wx.wxfactory.utils;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import com.ynxbd.wx.wxfactory.bean.event.WxEvent;
							 | 
						||
| 
								 | 
							
								import lombok.extern.slf4j.Slf4j;
							 | 
						||
| 
								 | 
							
								import org.apache.commons.codec.digest.DigestUtils;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import javax.servlet.http.HttpServletRequest;
							 | 
						||
| 
								 | 
							
								import java.util.Arrays;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								@Slf4j
							 | 
						||
| 
								 | 
							
								public class WxEventHelper {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 过滤内容可以直接返回
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param wxEvent 事件
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public static boolean filter(WxEvent wxEvent) {
							 | 
						||
| 
								 | 
							
								        String event = wxEvent.getEvent();
							 | 
						||
| 
								 | 
							
								        if (event != null) {
							 | 
						||
| 
								 | 
							
								            event = event.toUpperCase();
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        String msgType = wxEvent.getMsgType();
							 | 
						||
| 
								 | 
							
								        if (msgType != null) {
							 | 
						||
| 
								 | 
							
								            msgType = msgType.toUpperCase();
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        if ("VIEW".equals(event) && "EVENT".equals(msgType)) { // 点击菜单
							 | 
						||
| 
								 | 
							
								            return true;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if ("TEMPLATESENDJOBFINISH".equals(event)) { // 模板发送作业完成-->直接返回success
							 | 
						||
| 
								 | 
							
								            log.warn("[公众号]模板消息推送 fromUserName={}, msgType={}, eventKey={}, event={}", wxEvent.getFromUserName(), msgType, wxEvent.getEventKey(), event);
							 | 
						||
| 
								 | 
							
								            return true;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return false;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 消息验签
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param request request
							 | 
						||
| 
								 | 
							
								     * @param wxToken wxToken
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public static boolean verifyEventSign(HttpServletRequest request, String wxToken) {
							 | 
						||
| 
								 | 
							
								        String nonce = request.getParameter("nonce");
							 | 
						||
| 
								 | 
							
								        String timestamp = request.getParameter("timestamp");
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        String[] array = new String[]{wxToken, timestamp, nonce};
							 | 
						||
| 
								 | 
							
								        Arrays.sort(array);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        StringBuilder sb = new StringBuilder();
							 | 
						||
| 
								 | 
							
								        for (String s : array) {
							 | 
						||
| 
								 | 
							
								            sb.append(s);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return DigestUtils.sha1Hex(sb.toString()).equals(request.getParameter("signature"));
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |