微信公众号页面授权+消息推送

您所在的位置:网站首页 讲座的公众号推送内容怎么写 微信公众号页面授权+消息推送

微信公众号页面授权+消息推送

2024-07-12 09:28| 来源: 网络整理| 查看: 265

一、微信端配置

1、微信公众号进行微信认证。2、配置微信调用接口、按照微信要求需要进行页面授权获取用户的Openid,(这里注意一下、他会提示有个微信文件要放在项目的根目录下面,如:wx.qq.com/mp/MP_verify_7aS4leptvrYzJEsn.txt )。服务器一定要开通80端口,不要使用IP访问的地址,使用加了域名的地址。在这里插入图片描述3、配置微信公众号页面授权。在这里插入图片描述

4、配置调用接口的链接(项目链接:加域名后的。)。业务域名,JS接口安全域名,页面授权域名三个都要配置。 在这里插入图片描述 5、以上配置通过后、查看开发者文档、页面授权。来获取用户的codeid。在获取codeid前,先获取微信平台的AppID,AppSecret,填写IP白名单。(公众号开发信息) 在这里插入图片描述 6、打开公众号开发者文档、查看网页授权的步骤。第一步、更具维系提供的接口进行相对应参数的配置。 https://open.weixin.qq.com/connect/oauth2/authorize?appid=微信公众号的APPID&redirect_uri=需要授权的地址&response_type=code&scope=snsapi_userinfo&state=a-zA-Z0-9#wechat_redirect

scope分为两种授权, 1、以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面)

2、以snsapi_userinfo为scope发起的网页授权,是用来获取用户的基本信息的。但这种授权需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息。 在这里插入图片描述 7、微信公众号配置微信授权链接。 在这里插入图片描述

二、以上为微信公众号配置链接、已下为项目代码配置。 1、后台代码:

//页面授权后获得的code String code = request.getParameter("code"); request.setAttribute("code", code); RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); MultiValueMap postParameters = new LinkedMultiValueMap(); HttpEntity requestEntity = new HttpEntity(postParameters, headers); String resultMap = restTemplate .postForObject( "https://api.weixin.qq.com/sns/oauth2/access_token?appid=微信公众号的appid&secret=AppSecret(开发者秘钥)&code="+code+"&grant_type=authorization_code", requestEntity, String.class); request.setAttribute("resultMap", resultMap);

2、页面代码:

//注释掉的为记住密码。 /* function tijiao(){ //简单验证一下 var userName = document.getElementById("username").value; if(userName == ''){ alert("请输入用户名。"); return; } var userPass = document.getElementById("password").value; if(userPass == ''){ alert("请输入密码。"); return; } var objChk = document.getElementById("chkRememberPass"); if(objChk.checked){ //添加cookie addCookie("userName",userName,7,"/"); addCookie("userPass",userPass,7,"/"); } var data = { username: $("#username").val(), password: $("#password").val(), currpage:$("#currpage").val(), }; $.ajax({ type: "POST", contentType: 'application/json', url: "webQualityClear/getuser", data: JSON.stringify(data), success: function(reginlst){ if(reginlst.success) { window.location.href = "webQualityClear/getcontent/"+reginlst.gongqu+"/"+reginlst.quanx; }else{ alert("输入的账号密码错误!"); } } }); } */ /* function addCookie(name,value,days,path){ //添加设置cookie var name = escape(name); var value = escape(value); var expires = new Date(); expires.setTime(expires.getTime() + days * 3600000 * 24); //path=/,表示cookie能在整个网站下使用,path=/temp,表示cookie只能在temp目录下使用 path = path == "" ? "" : ";path=" + path; //GMT(Greenwich Mean Time)是格林尼治平时,现在的标准时间,协调世界时是UTC //参数days只能是数字型 var _expires = (typeof days) == "string" ? "" : ";expires=" + expires.toUTCString(); document.cookie = name + "=" + value + _expires + path; } */ /* function getCookieValue(name){ //获取cookie的值,根据cookie的键获取值 //用处理字符串的方式查找到key对应value var name = escape(name); //读cookie属性,这将返回文档的所有cookie var allcookies = document.cookie; //查找名为name的cookie的开始位置 name += "="; var pos = allcookies.indexOf(name); //如果找到了具有该名字的cookie,那么提取并使用它的值 if (pos != -1){ //如果pos值为-1则说明搜索"version="失败 var start = pos + name.length; //cookie值开始的位置 var end = allcookies.indexOf(";",start); //从cookie值开始的位置起搜索第一个";"的位置,即cookie值结尾的位置 if (end == -1) end = allcookies.length; //如果end值为-1说明cookie列表里只有一个cookie var value = allcookies.substring(start,end); //提取cookie的值 return (value); //对它解码 }else{ //搜索失败,返回空字符串 return ""; } } //实现功能,保存用户的登录信息到cookie中。当登录页面被打开时,就查询cookie window.onload = function(){ var userNameValue = getCookieValue("userName"); document.getElementById("username").value = userNameValue; var userPassValue = getCookieValue("userPass"); document.getElementById("password").value = userPassValue; } */ //此处为获取用户信息 var jsonData = ${resultMap}; function tijiao(){ var openid = jsonData.openid; var username = document.getElementById("username").value; var password = document.getElementById("password").value; if(username == ''){ alert("请输入绑定账号"); return; }else{ $.ajax({ type:"post", async:false, data:{openid:openid,username:username,password:password,}, url : "damaction.do?wxAjaxJson",// 请求的action路径 success : function(data) { var d = $.parseJSON(data); var con = d.msg; var ini = d.obj; if(ini = "1"){ messageBox.show(con, 1, 1000); }else{ messageBox.show(con, 1, 1000); } } }); } }  

3、微信消息推送。先在微信公众平台设置模板,获取模板ID。

//获取需要推送人的openid //获取当前时间 Date day=new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //查询模板ID RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); MultiValueMap postParameters = new LinkedMultiValueMap(); HttpEntity requestEntity = new HttpEntity


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3