您的位置:首页 > 汽车 > 新车 > 徐州网页公司_我想建网站_友情链接源码_2345浏览器

徐州网页公司_我想建网站_友情链接源码_2345浏览器

2024/11/19 0:46:14 来源:https://blog.csdn.net/sxp2558/article/details/142797917  浏览:    关键词:徐州网页公司_我想建网站_友情链接源码_2345浏览器
徐州网页公司_我想建网站_友情链接源码_2345浏览器

前言

使用第三方插件
<dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-mp</artifactId><version>4.6.0</version>
</dependency>

准备APPID和appSecet

扫码登录

公众号回调

  • 回调代码块实现

    /*** 验证微信服务器 此接口不调用*/@ApiOperation(value = "验证微信服务器 此接口不调用")@GetMapping(value = "/callback")public String checkSign(HttpServletRequest request) {String signature = request.getParameter("signature");String timestamp = request.getParameter("timestamp");String nonce = request.getParameter("nonce");String echostr = request.getParameter("echostr");log.info("\n接收到来自微信服务器的认证消息:[signature:{}, timestamp:{}, nonce:{}, echostr:{}]", signature, timestamp, nonce, echostr);if (!wxMpService.checkSignature(timestamp, nonce, signature)) {log.error("【无效的请求】");throw new ServiceException("无效的请求", -1);}return echostr;}/*** 响应微信,这一步是关注微信后,响应微信获取信息*/@ApiOperation(value = "微信扫码响应微信服务器")@PostMapping("/callback")public String scanQRCodesCallBack(HttpServletRequest request, @RequestBody String requestBody) {String signature = request.getParameter("signature");String timestamp = request.getParameter("timestamp");String nonce = request.getParameter("nonce");String echostr = request.getParameter("echostr");String openid = request.getParameter("openid");String encType = request.getParameter("encType");String msgSignature = request.getParameter("msgSignature");log.info("\n接收到来自微信服务器的认证消息:[signature:{}, timestamp:{}, nonce:{}, echostr:{},encType:{},msgSignature:{}]", signature, timestamp, nonce, echostr, encType, msgSignature);if (!wxMpService.checkSignature(timestamp, nonce, signature)) {log.error("【无效的请求】");throw new ServiceException("无效的请求", -1);}if (encType == null) {// 明文传输的消息WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);log.debug("\n消息内容为:\n{} ", inMessage.toString());return weiXinService.scanQRCodesCallBack(inMessage);} else if ("aes".equalsIgnoreCase(encType)) {// aes加密的消息WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, wxMpService.getWxMpConfigStorage(),timestamp, nonce, msgSignature);log.debug("\n消息解密后内容为:\n{} ", inMessage.toString());return weiXinService.scanQRCodesCallBack(inMessage);}return "";}
    
    复制@Overridepublic String scanQRCodesCallBack(WxMpXmlMessage message) {log.info("总的message:" + JSON.toJSONString(message));// content 公众号回复用户的文本内容String content = "欢迎关注公众号!NatCross是内网穿透工具,也是免费的端口映射软件。解决80被封/动态IP/无公网ip问题;适用于发布网站、访问局域网服务器和应用服务。";String messageType = message.getMsgType();                                //消息类型String messageEvent = message.getEvent();                                    //消息事件String fromUser = message.getFromUser();                                 //发送者帐号String toUser = message.getToUser();                                       //开发者微信号String text = message.getContent();                                        //文本消息  文本内容String eventKey = message.getEventKey();                                    //二维码参数JSONObject businessParams = JSON.parseObject(eventKey);                    //从二维码参数中获取uuid通过该uuid可通过websocket前端传数据log.info("消息类型:{},消息事件:{},发送者账号:{},接收者微信:{},文本消息:{},二维码参数:{}", messageType, messageEvent, fromUser, toUser, text, eventKey);WxMpUser wxMpUser = null;try {wxMpUser = wxMpService.getUserService().userInfo(fromUser);} catch (WxErrorException e) {e.printStackTrace();}log.info("通过用户openid获取用户信息:" + JSON.toJSONString(wxMpUser));if (StringUtils.equalsAnyIgnoreCase(WechatEventEnum.unsubscribe.getType(), messageEvent)) {//取消订阅log.info("取消订阅");return this.msgStr(message, "取消关注成功!");}String uuid = businessParams.containsKey("uuid") ? businessParams.getString("uuid") : null;if (StringUtils.isEmpty(uuid)) {//未知关注公众号,默认提示log.info("未知关注公众号,默认提示");return this.msgStr(message, content);}String redisData = Objects.toString(stringRedisTemplate.opsForValue().get(RedisKey.WECHAT_SCAN_UUID_FLAG + uuid), null);if (StringUtils.isNotEmpty(redisData) && wxMpUser != null) {if (StringUtils.equalsAnyIgnoreCase(WechatEventEnum.SCAN.getType(), messageEvent)) {String eventType = businessParams.getString("eventType");if (StringUtils.equalsAnyIgnoreCase(MsgEventTypeEnum.ADD_WARNING_RECEIVER.getEventType(), eventType)) {content = DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD_HH_MM_SS) + "添加告警接收人成功!";} else if (StringUtils.equalsAnyIgnoreCase(MsgEventTypeEnum.USER_LOGIN.getEventType(), eventType)) {content = DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD_HH_MM_SS) + "扫描登录成功!";}JSONObject dataMap = JSON.parseObject(redisData);dataMap.put("openid", wxMpUser.getOpenId());dataMap.put("unionid", wxMpUser.getUnionId());dataMap.put("nickName", wxMpUser.getNickname());dataMap.put("headImgUrl", wxMpUser.getHeadImgUrl());stringRedisTemplate.opsForValue().set(RedisKey.WECHAT_SCAN_UUID_RESULT_FLAG + uuid, dataMap.toString());}}// 根据来时的信息格式,重组返回。(注意中间不能有空格)final String msgStr = this.msgStr(message, content);return msgStr;}@Overridepublic NatMemberWechat getScanResult(String uuid) {String redisData = Objects.toString(stringRedisTemplate.opsForValue().get(RedisKey.WECHAT_SCAN_UUID_RESULT_FLAG + uuid), null);if (StringUtils.isEmpty(redisData)) {log.debug("redisData is null");return null;}JSONObject jsonObject = JSONObject.parseObject(redisData);NatMemberWechat natMemberWechat = natMemberWechatService.selectNatMemberWechatByOpenId(jsonObject.getString("openid"));if(natMemberWechat==null){natMemberWechat=new NatMemberWechat();natMemberWechat.setOpenId(jsonObject.getString("openid"));natMemberWechat.setUserId(null);}return natMemberWechat;}private String msgStr(WxMpXmlMessage message, String content) {// 根据来时的信息格式,重组返回。(注意中间不能有空格)final String msgStr = "<xml>"+ "<ToUserName><![CDATA[" + message.getFromUser() + "]]></ToUserName>"+ "<FromUserName><![CDATA[" + message.getToUser() + "]]></FromUserName>"+ "<CreateTime>" + new Date().getTime() + "</CreateTime>"+ "<MsgType><![CDATA[text]]></MsgType>"+ "<Content><![CDATA[" + content + "]]></Content>"+ "</xml>";return msgStr;}
    
  • 回调日志​​​​​​​测试通过后,如果是vue前后端分离,可以把接口给前端调用。
    以上是JAVA实现公众号扫码登录和关注的开发流程。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com