当前位置:网站首页>【微信授权登录】uniapp开发小程序,实现获取微信授权登录功能
【微信授权登录】uniapp开发小程序,实现获取微信授权登录功能
2022-07-02 00:28:00 【马小跳的Coding进阶之路】
一、解题思路:
微信授权登录(获取用户信息)
1.先获取用户信息——用户授权允许后,通过调用uni.login 可以获取到code。
2.拿着获取到的code去调用——登录接口,可以获取到token。
3.把token存入缓存。就可以在页面判断是否登录了。
二、代码
<view @click="getUserInfo()">点击登录</view>
<script>
export default {
data() {
return {
localtoken: ''
}
},
onLoad() {
},
onHide() {
},
onShow() {
this.localtoken = uni.getStorageSync('localtoken');
},
methods: {
getUserInfo() {
var that = this;
uni.showLoading({
// 展示加载框
title: '加载中',
});
uni.getUserProfile({
//获取到用户信息
desc: '登录后可同步数据',
success: async (obj) => {
console.log('obj', obj);
that.nickName = obj.userInfo.nickName //用户名
that.avatarUrl = obj.userInfo.avatarUrl //用户头像
// 调用 action ,请求登录接口
uni.login({
provider: 'weixin',
success: (res) => {
console.log('res-login', res);
//获取到code
that.code = res.code;
// console.log('code', res.code);
//请求登录接口
if (res.errMsg == 'login:ok') {
var params = {
code: that.code,
nickname: that.nickName,
avatar: that.avatarUrl
}
that.$api.appPlateForm('POST', 'auth/login', params, function(
res) {
if (res.code == 200) {
uni.showToast({
title: '登录成功',
icon: 'success',
mask: true,
});
//获取到token 存入缓存。通过有无token来判断是否登录
// console.log('登录接口',res)
uni.setStorageSync('localtoken', res.data.data.access_token)
that.myProfile() //用户信息接口
that.getHistoryList() //足迹接口
}
}, function(err) {
uni.showToast({
icon: 'none',
title: err.msg
})
});
}
},
});
},
fail: () => {
uni.showToast({
title: '授权已取消',
icon: 'error',
mask: true,
});
},
complete: () => {
// 隐藏loading
uni.hideLoading();
},
});
},
}
}
</script>
三、效果展示



ending~
边栏推荐
- Leetcode medium question sharing (5)
- Take the enclave Park as a sample to see how Yuhua and Shaoshan play the song of Chang Zhu Tan integrated development
- leetcode96不同的二叉搜索树
- Kuberntes cloud native combat high availability deployment architecture
- 【mysql 07】GPG key retrieval failed: “Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022“
- ThreadLocal内存泄漏是什么,怎么解决
- Qt5.12.9 migration tutorial based on Quanzhi H3
- 【模板】自适应辛普森积分
- Intelligent operation and maintenance practice: banking business process and single transaction tracking
- Record the accidental success and failure of uploading large files
猜你喜欢
![[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler](/img/35/e458fd437a0bed4bace2d6d65c9ec8.png)
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler

LeetCode中等题题分享(5)

【QT】Qt 使用MSVC2017找不到编译器的解决办法

Example explanation: move graph explorer to jupyterlab
![Jielizhi, production line assembly link [chapter]](/img/1d/d1736fad33c428e61f450aad512ce0.png)
Jielizhi, production line assembly link [chapter]
![Flow control statement of SQL data analysis [if, case... When detailed]](/img/7b/eabb0700936d34a3a145737580be88.png)
Flow control statement of SQL data analysis [if, case... When detailed]

实例讲解将Graph Explorer搬上JupyterLab

基于全志H3的QT5.12.9移植教程

BPR (Bayesian personalized sorting)

GCC compilation
随机推荐
leetcode96不同的二叉搜索树
使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
Linux CentOS7安装Oracle11g的超完美新手教程
Promise和模块块化编程
I want to ask, which is the better choice for securities companies? I don't understand. Is it safe to open an account online now?
Leetcode skimming: binary tree 03 (post order traversal of binary tree)
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
牛客-练习赛101-推理小丑
Operate database transactions with jpatractionmanager
[cmake] cmake configuration in QT Creator
JS——图片转base码 、base转File对象
求逆序数的三个方法
Node——添加压缩文件
Guide d'installation du serveur SQL
Talents come from afar, and Wangcheng district has consolidated the intellectual base of "strengthening the provincial capital"
Leetcode skimming: stack and queue 02 (realizing stack with queue)
SQL数据分析之子查询的综合用法和案例题【耐心整理】
Intelligent operation and maintenance practice: banking business process and single transaction tracking
【模板】自适应辛普森积分
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电