当前位置:网站首页>微信小程序实现lot开发09 接入微信登录
微信小程序实现lot开发09 接入微信登录
2022-08-02 22:30:00 【林春Ax】
微信登录的流程一般查看微信小程序开发文档就能解决,我们来梳理一下:首先我们需要通过调取wx.login接口获取临时交换openId的凭证码code,然后拿这个临时凭证码code去访问指定的某个小程序APPID的openId和登录态记录session_key,同一个小程序每一个用户的openId都是固定且唯一的,我们可以将这个openid存入开发者服务器实现登录验证的业务。
主要的问题出现在:AppID与sercet对应错误,主要的错误码为41008与40029,分别是code没获取到以及AppID验证失败。
我们看一下代码:
Page({
/**
* 页面的初始数据
*/
data: {
// code存放变量
js_code:"",
// 用户非隐蔽信息
rawData:"",
// 用户加密签名
signature:"",
// 封装用户信息
userInfo:"",
// 获取的登录唯一凭证
openid:"",
// 临时登录的会话密钥,可以作为登录状态的记录信号
session_key:""
},
login(){
var th = this;
// 授权获取当前用户信息
wx.getUserProfile({
desc: '用于登录校验',
success(res){
console.log(res);
// 接收这个响应信息里的用户信息
var userInfo = res.userInfo;
th.setData({
userInfo: userInfo,
rawData: res.rawData,
signature: res.signature
})
}
})
// 调用login接口获取临时交换凭证code
wx.login({
success: function(res){
console.log(res.code);
// 将获取的临时凭证存放起来
th.setData({
js_code: res.code
});
}
});
// 设置延时是先进行授权结束后再回调这个微信登录的openId换取函数
setTimeout(function(){
let code = th.data.js_code;
wx.request({
// 注意反引号
url: `https://api.weixin.qq.com/sns/jscode2session?appid=你的AppID值&secret=你的小程序密钥&js_code=${code}&grant_type=authorization_code`,
success(res){
console.log(res);
// 将获取的两个关键信息存储
th.setData({
openid: res.data.openid,
session_key: res.data.session_key
})
console.log(th.data.openid);
console.log(th.data.session_key);
}
})
},2000);
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
},
..........省略下面.......
}页面代码就一个按钮:
<button type="primary" open-type="getUserInfo" bindtap="login">
授权微信登录
</button>
<image src="{
{userInfo.avatarUrl}}" style="height: 40px;width: 40px;"></image>
<text>{
{userInfo.nickName}}</text>每一个小程序在测试环境时都会有一个project.config.json文件,里面具备配置的小程序id,我们需要将你设置的AppID在这里也更改一下。
边栏推荐
猜你喜欢

centos7安装mysql5.7

目前为止 DAO靠什么盈利?

【斯坦福计网CS144项目】Lab5: NetworkInterface

软件测试笔试题1(附答案)

在软件测试行业近20年的我,再来和大家谈谈今日的软件测试

CWE4.8:2022年危害最大的25种软件安全问题

学习基因富集工具DAVID(2)

mysql根据多字段分组——group by带两个或多个参数

【C语言】带头双向循环链表(list)详解(定义、增、删、查、改)

【使用pyside2遇到的问题】This application failed to start because no Qt platform plugin could be initialized.
随机推荐
Ruoyi integrates minio to realize distributed file storage
无代码开发平台表单样式设置步骤入门课程
刚安装完win10专业工作站版,系统变量中Path默认值有哪些?重新建一个“PATH”变量名,会覆盖掉原先的“Path”。
用于中文文本分类的中文停用词
gdb调试简要总结
软件测试笔试题1(附答案)
go 反射 reflect 包
Software testing pen questions 1 (with answers)
Week 7 - Distributional Representations
CentOS7 安装MySQL 图文详细教程
No code development platform data ID introductory tutorial
ROS2初级知识(9):bag记录过程数据和重放
创建型模式 - 单例模式Singleton
I have been in the software testing industry for nearly 20 years, let me talk to you about today's software testing
Jmeter二次开发实现rsa加密
CodeTON Round 2 A - D
In-depth study TypeScript TypeScript 】 【 class (under)
VMware workstation 程序启动慢
MySQL 与InnoDB 下的锁做朋友 (四)行锁/记录锁
多租户的多种实现方案