java微信公众号开发之各种事件推送
发表时间:2020-11-5
发布人:葵宇科技
浏览次数:52
1.配置微信公众服务器
2.提前下好公众号SHA1算法生成安全签名类
/**
* SHA1 class
*
* 计算公众平台的消息签名接口.
*/
public class SHA1 {
/**
* 用SHA1算法生成安全签名
* @param token 票据
* @param timestamp 时间戳
* @param nonce 随机字符串
* @param encrypt 密文
* @return 安全签名
*/
public static String getSHA1(String token, String timestamp, String nonce, String encrypt)
{
try {
String[] array = new String[] { token, timestamp, nonce, encrypt };
StringBuffer sb = new StringBuffer();
// 字符串排序
Arrays.sort(array);
for (int i = 0; i < 4; i++) {
sb.append(array[i]);
}
String str = sb.toString();
// SHA1签名生成
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(str.getBytes());
byte[] digest = md.digest();
StringBuffer hexstr = new StringBuffer();
String shaHex = "";
for (int i = 0; i < digest.length; i++) {
shaHex = Integer.toHexString(digest[i] & 0xFF);
if (shaHex.length() < 2) {
hexstr.append(0);
}
hexstr.append(shaHex);
}
return hexstr.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
3.创建一个接口,就是配置公众号服务的接口
//微信公众号验证
@RequestMapping("openWx/gzhVerification")
public String gzhVerification(HttpServletRequest request){
log.info("微信公众号验证");
String signature=request.getParameter("signature");
String timestamp=request.getParameter("timestamp");
String nonce=request.getParameter("nonce");
String echostr=request.getParameter("echostr");
String token="token";//这里填基本配置中的token
String jiami="";
try {
jiami= SHA1.getSHA1(token, timestamp, nonce,"");//这里是对三个参数进行加密
if(echostr!=null && jiami.equals(signature)){
log.info("第一次申请验证成功");
return echostr;
}
//判断是否是公众号发来的请求
if(jiami==null || !jiami.equals(signature)){
log.info("错误的请求");
return null;
}
//----------------------事件推送
Map<String, String> stringStringMap = parseXml(request);
//---按钮事件
if("CLICK".equalsIgnoreCase(stringStringMap.get("Event"))){
log.info("按钮事件");
//因为只有一个按钮所以不用判断key了
String fromUserName = toXML(stringStringMap.get("FromUserName"),stringStringMap.get("ToUserName"));
log.info("fromUserName:"+fromUserName);
//发送消息
return fromUserName;
//关注公众号数据
}else if("subscribe".equalsIgnoreCase(stringStringMap.get("Event")) && stringStringMap.get("EventKey")==null && stringStringMap.get("Ticket")==null){
log.info("关注公众号");
customerWxGzhService._add(stringStringMap.get("FromUserName"),null);
//取消关注
}else if("unsubscribe".equalsIgnoreCase(stringStringMap.get("Event"))) {
log.info("取消关注");
customerWxGzhService._delete(stringStringMap.get("FromUserName"));
//二维码关注
}else if("subscribe".equalsIgnoreCase(stringStringMap.get("Event")) && stringStringMap.get("EventKey")!=null && stringStringMap.get("Ticket")!=null){
log.info("二维码关注");
//获取二维码参数值
String eventKey =stringStringMap.get("EventKey");
eventKey= eventKey.substring(8);
customerWxGzhService._add(stringStringMap.get("FromUserName"),eventKey);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* dom4j 解析 xml 转换为 map
* @param request
* @return
* @throws Exception
*/
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();
inputStream = null;
return map;
}
//发送联系我们
public String toXML(String toUserName,String fromUserName){
StringBuilder sb = new StringBuilder();
sb.append("<xml>");
sb.append("<ToUserName><![CDATA["+toUserName+"]]></ToUserName>");
sb.append("<FromUserName><![CDATA["+fromUserName+"]]></FromUserName>");
sb.append("<CreateTime>"+System.currentTimeMillis()/1000+"</CreateTime>");
sb.append("<MsgType><![CDATA[text]]></MsgType>");
sb.append("<Content><![CDATA[您好]]></Content>");
sb.append("</xml>");
return sb.toString();
}
公众号事件的提示只有一个接口 就是不断判断Event参数来处理事件,