技术标签: 微信公众平台 前端 javascript uni-app
首先我们进行网页授权的需求是,获取用户信息、最主要是获取openid唯一值,可以用于用户登录、支付等功能,这时候就需要进行网页授权获取用户的信息以及openid。
在操作之前可以先提前看看网页授权官方文档
网页授权之前,需要先到公众平台官网中的“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名。
appid:公众号的唯一标识
redirect_uri :授权后重定向的回调链接地址
scope:授权方式
const appid = "xxxxxxxxxxx";//公众号的唯一标识
const redirect_uri = "http://xxx.xxxx.xxx";//授权后重定向的回调链接地址
const scope = "snsapi_userinfo";//非静默授权:snsapi_userinfo 静默授权:snsapi_base
在onLaunch生命周期操作
onLaunch(e){
const openid = uni.getStorageSync(OPENID);
//不存在code
if(!openid){
//获取code
document.location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${
appid}&redirect_uri=${
redirect_uri}&response_type=code&scope=${
scope}&state=STATE#wechat_redirect`);
}
}
执行了第一步的获取code后就会跳转至 redirect_uri/?code=CODE&state=STATE,此时url地址会带有code参数
拿到code后调用就后端的getWxAuthorize接口获取
async onLaunch(e){
const openid = uni.getStorageSync('OPENID');
if((e&&e.query&&e.query.code)&&!openid){
//请求后端接口获取用户的信息
let result = await getWxAuthorize({
code:e.query.code,scope:"snsapi_userinfo"});
//保存用户的openid
uni.setStorageSync('OPENID',result.data.openid);
}
}
尤其注意:由于公众号的secret和获取到的access_token安全级别都非常高,必须只保存在服务器,不允许传给客户端。后续刷新access_token、通过access_token获取用户信息等步骤,也必须从服务器发起。
如果只需要了解前端的步骤,此步骤可以忽略
appid:公众号的唯一标识
secret:公众号的appsecret
大概的流程如下:
const axios = require("axios");
const appid = "xxxxxxxxxxx";//公众号的唯一标识
const secret = "xxxxxxxxxxx";//公众号的appsecret
function setAuthorize(params){
return new Promise(async(resolve)=>{
let userinfo = {
};
//通过code换取网页授权access_token
let result = await axios(`https://api.weixin.qq.com/sns/oauth2/access_token?appid=${
appid}&secret=${
secret}&code=${
params.code}&grant_type=authorization_code`);
result = result.data;
console.log("access_token结果",result )
if(result.errcode)resolve({
code:403,msg:''})
userinfo.openid = result.openid;
// scope=snsapi_userinfo弹出网页授权
if(params.scope=='snsapi_userinfo'){
// 获取用户信息
let info = await axios(`https://api.weixin.qq.com/sns/userinfo?access_token=${
result.access_token}&openid=${
result.openid}&lang=zh_CN`);
info = info.data;
console.log("用户信息",info)
if(info.errcode)resolve({
code:403,msg:''});
userinfo = info;
}
//----------------------------------------
//对数据库进行新增用户或者更新用户信息操作
//----------------------------------------
resolve({
code:0,msg:'',data:userinfo})
})
}
async onLaunch(e){
const openid = uni.getStorageSync('OPENID');
if(!(e&&e.query&&e.query.code)!openid){
//不存在code 不存在openid
//获取code
document.location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${
appid}&redirect_uri=${
redirect_uri}&response_type=code&scope=${
scope}&state=STATE#wechat_redirect`);
}else if((e&&e.query&&e.query.code)&&!openid){
//存在code 不存在openid
//请求后端接口获取用户的信息
let result = await getWxAuthorize({
code:e.query.code,scope:"snsapi_userinfo"});
//保存用户的openid
uni.setStorageSync('OPENID',result.data.openid);
}
}
授权后回调页面,此处如果再返回,会跳转到空白页
1.初次授权时先存储页面历史列表的长度
// 第一次进入页面
uni.setStorageSync("historyLength",history.length);
2.在code回调页面之后,进行history.go返回到1.初始页面
const historyLength = uni.getStorageSync("historyLength");
history.go(-(history.length -historyLength));
此处需要留意通过code获取用户信息这个步骤最好放在回到初始页面的时候进行。
import {
getWxAuthorize} from "@/api/wx.js";
export default {
async onLaunch(e){
const openid = uni.getStorageSync('OPENID');
const code = uni.getStorageSync('CODE');
if(!code&&!(e&&e.query&&e.query.code)!openid){
//不存在存储的code 不存在地址参数code 不存在openid
//存储当前初始页面历史列表数量
uni.setStorageSync("historyLength",history.length);
//获取code
document.location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${
appid}&redirect_uri=${
redirect_uri}&response_type=code&scope=${
scope}&state=STATE#wechat_redirect`);
}else if((e&&e.query&&e.query.code)&&!openid){
//存在地址参数code 不存在openid
uni.setStorageSync('CODE',e.query.code);
const historyLength = uni.getStorageSync("historyLength");
//跳转回初始页面
history.go(-(history.length -historyLength));
}else if(code&&!openid){
//存在存储的code 不存在openid
uni.removeStorageSync('CODE');
//请求后端接口获取用户的信息
let result = await getWxAuthorize({
code,scope:"snsapi_userinfo"});
//保存用户的openid
uni.setStorageSync('OPENID',result.data.openid);
}
}
}
文章浏览阅读871次,点赞11次,收藏3次。*单片机设计介绍, 基于51单片机冰箱温度控制器设计。_基于51单片机的智能冰箱控制系统设计
文章浏览阅读4.8k次。一、sftp服务器进入root模式(下面的操作默认都是在root用户下)#安装openssh-serverapt-get install -y openssh-server创建sftp的组和用户#创建sftp-users组groupadd sftp-users#创建sftp用户目录alicemkdir /home/alice#创建sftp用户alice,并且绑定其主目..._ubuntu sftp服务器查看用户和密码
文章浏览阅读5.9k次,点赞9次,收藏16次。解决了在simulink中使用s-function遇到的报错:State derivatives returned by S-function 'demo' in 'test/S-Function' during flag=1 call must be a real vector of length 2 _state derivatives returned by s-function 'pmsm' in 'ipmsm/ipmsm/s-function1
文章浏览阅读3.1k次。1. 打开配置文件Mac 如下图2. 在文件内部添加这段文字,就可以了:"update_check":false _mac sublime text 取消更新提示
文章浏览阅读548次,点赞10次,收藏6次。Linux系统下DNS配置指南_linux 服务器修改网络dns
文章浏览阅读779次,点赞19次,收藏24次。springboot微信小程序的小疾病问诊服务系统的设计与实现。springboot基于spring的物业管理系统的设计与实现。springboot基于Java的高校学生请假系统。ssm基于Android的购物商场APP设计与实现。springboot基于微信小程序的智慧校园系统。ssm基于Android的英语词典的设计与开发。ssm基于SSM+Vue的学生实践管理平台开发。ssm基于android的企业员工考勤系统。ssm基于web的暗香小店系统的设计与实现。ssm基于Web的高等学校公费医疗管理系统。
文章浏览阅读7.7k次。反序列化无法找到程序集提示找不到程序集. 原因是序列化时把序列化类的命名空间等信息保存了,但应用程序和类库的命名空间可能是不一样的,所以提示找不到程序集. 解决方法如下: 方法1.将dll加入强名称,注册到全局程序集缓存中 方法2.在反序列化使用的IFormatter 对象加入Binder 属性,使其获取要反序列化的对象所在的程序集_未能找到程序集“g:\c#%5cc#%20%e4%b8%8a%e4%bd%8d%e6%9c%ba%5c%e7%a9%ba%e5%8e%8
文章浏览阅读234次。Jet-Lube EZY-Turn Kopr-Kote Deacon Moly-Lith 12|34 21 (Oilfield) 21 (Water Well) 21 Arctic (Oilfield) 21 Arctic (Water Well) 550 Extreme 550 Extreme All Weather 713-670-5700 769 Lubricant 930 Whitmore Drive Alco-EP Alco-EP ECF Alco-EP-73 Plus AP-1 AP-1W _dpezy
文章浏览阅读511次。本文从本人简书博客同步过来在上一篇中我们简要地介绍了 mpi4py 中的单边通信概念,下面我们将介绍单边通信的相关操作。创建/释放窗口对象注意:在使用单边通信操作之前,所有进程都须通过共同参与的创建窗口操作公开声明自己可供访问的内存空间。创建和释放窗口对象的方法(MPI.Win 类方法)接口如下:Create(type cls, memory, int disp_unit=..._win.fence() win.free()
文章浏览阅读551次,点赞23次,收藏7次。mit6.824的lab1,实现MapReduce_mitmapreduce实现csdn
文章浏览阅读6.6k次,点赞10次,收藏82次。在刚开始学Android开发时,下载好了Android studio,但是不知道如何下手,现在就通过一个简单的小项目熟悉如何使用这个软件进行Android开发。前提:下载好Android studio并配置好相关环境。首先介绍一下Android开发过程中需要修改的三类文件:位于java包下的各类activity文件:实现了用户与软件的交互,主要为java代码实现。位于res包下的.xml文件:在layout包里的为布局文件,即Android界面显示的视图,而drawable里则放置了某个控件的_android studio简单项目
文章浏览阅读3.5k次。一、Bootstrap 弹出框弹出框控件类似于提示框,它在鼠标点击到元素后显示,与提示框不同的是它可以显示更多的内容。注意: 弹出框要写在 jQuery 的初始化代码里: 然后在指定的元素上调用 popover() 方法。1.1 基本弹出框通过向元素添加 data-toggle=“popover” 来来创建弹出框。title 属性的内容为弹出框的标题,data-content 属性..._bootstrap 弹出框