大家好,我是小武。在开发微信小程序或公众号时,难免需要用户关注或取消关注关闭公众号后,只需几步处理业务逻辑需求。1、配置首先我们需要在微信后台配置服务器公众号,登录公众号后台->开发->基础配置->服务器配置,如下:服务器地址是我们写的一个controller(代码如下),用于验证微信,接收微信消息。地址错误或者token错误,提交信息时会报错2.代码实现SignUtil/***@description*/publicclassSignUtil{/***verifysignature*@paramtoken*@paramsignaturesignature用于验证最终结果是否一致*@paramtimestamptimestamp*@paramnonce随机数标记*@return一个布尔值判断最终加密是否与签名一致*/publicstaticbooleancheckSignature(Stringtoken,Stringsignature,Stringtimestamp,Stringnonce){//将传入的参数变成String数组然后对字典进行排序String[]arr=newString[]{token,timestamp,nonce};//对token、timestamp、nonce三个参数进行排序Arrays.sort(arr);//创建一个对象存储三个String排序后的组合StringBuildercontent=newStringBuilder();for(inti=0;i>>4)&0X0F];tempArr[1]=数字[mByte&0X0F];//获取二进制码字符串Strings=newString(tempArr);返回s;}}MsgUtil/***@description*/publicclassMsgUtil{publicstaticfinalStringMSGTYPE_EVENT="event";//消息类型--事件publicstaticfinalStringMESSAGE_SUBSCIBE="subscribe";//消息事件类型--订阅eventpublicstaticfinalStringMESSAGE_UNSUBSCIBE="unsubscribe";//消息事件类型--取消订阅事件publicstaticfinalStringMESSAGE_TEXT="text";//消息类型--文本消息/***组装文本消息*/publicstaticStringtextMsg(StringtoUserName,StringfromUserName,Stringcontent){TextMsgtext=newTextMsg();text.setFromUserName(toUserName);text.setToUserN我(来自用户名);text.setMsgType(MESSAGE_TEXT);text.setCreateTime(newDate().getTime());text.setContent(内容);返回XmlUtil.textMsgToxml(文本);}/***响应订阅事件--回复短信*/publicstaticStringsubscribeForText(StringtoUserName,StringfromUserName,Stringcontent){returntextMsg(toUserName,fromUserName,content);}/***响应取消订阅事件*/publicstaticStringunsubscribeForText(StringtoUserName,StringfromUserName,Stringcontent){System.out.println("User:"+fromUserName+"Unfollow~");返回””;}}application.ymlparameter:#微信publicAppId:微信公众号AppIdpublicAppSecret:微信公众号AppSecretWxPublicController/***@description*/@Controller@RequestMapping(value="/message/weixin")publicclassWxPublicController{protectedstatic记录器logger=LoggerFactory.getLogger(WxPublicController.class);//填充和公众号同样在后台配置的tokenprivatestaticfinalStringTOKENN="";/***微信公众号appId*/@Value("${parameter.publicAppId}")privateStringpublicAppId;/***微信公众号appSecret*/@Value("${parameter.publicAppSecret}")privateStringpublicAppSecret;@RequestMapping(method=RequestMethod.GET)publicvoidget(HttpServletRequestrequest,HttpServletResponseresponse){//微信加密签名,签名结合开发者填写请求中的token参数和timestamp参数和nonce参数。Stringsignature=request.getParameter("签名");//时间戳Stringtimestamp=request.getParameter("timestamp");//随机数Stringnonce=request.getParameter("nonce");//随机字符串Stringechostr=request.getParameter("echostr");PrintWriterout=null;尝试{out=response.getWriter();//通过检查签名来验证请求,如果验证成功则原样返回echostr,否则访问失败if(SignUtil.checkSignature(TOKENN,signature,timestamp,nonce)){out.打印(回声);}}catch(IOExceptione){e.打印堆栈跟踪();}最后{if(out!=null){out.close();}}}@RequestMapping(method=RequestMethod.POST)publicvoidpost(HttpServletRequestrequest,HttpServletResponseresponse){//响应消息PrintWriterout=null;字符串resMessage="";尝试{request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");//将微信返回的xml信息转化为mapMapmap=XmlUtil.parseXml(请求);//消息源用户IDStringfromUserName=map.get("FromUserName");//消息目标用户IDStringtoUserName=map.get("ToUserName");//消息创建时间(整数)StringcreateTime=map.get("CreateTime");//消息类型StringmsgType=map.get("MsgType");//事件类型:订阅/取消订阅StringeventType=map.get("事件");//如果是事件类型if(MsgUtil.MSGTYPE_EVENT.equals(msgType)){//处理订阅事件if(MsgUtil.MESSAGE_SUBSCIBE.equals(eventType)){resMessage=MsgUtil.subscribeForText(toUserName,fromUserName,"Hello,感谢您的关注!!!”);//TODO业务逻辑//处理取消订阅消息}elseif(MsgUtil.MESSAGE_UNSUBSCIBE.equals(eventType)){//TODO业务逻辑}logger.info("eventType:"+eventType+",fromUserName:"+fromUserName+",toUserName:"+toUserName+",msgType:"+msgType+",createTime:"+createTime);}out=response.getWriter();out.println(resMessage);}catch(UnsupportedEncodingExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}赶上(异常e){e.printStackTrace();}finally{if(out!=null){out.close();}}}}您的一键三连,是我更新的最大动力。感谢您的阅读,让我们再相见,我手中的金箍棒,上可通天,下可探海