当前位置:网站首页>【微信授权登录】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~
边栏推荐
- SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
- Accelerator systems initiative is an independent non-profit organization
- Windows installation WSL (II)
- [QT] test whether QT can connect to the database
- js 公共库 cdn 推荐
- SQL Server Installation Guide
- Learn online case practice
- 【CTF】bjdctf_2020_babystack2
- Leetcode medium question sharing (5)
- Is it safe and reliable to open an account in Caixue school and make new debts?
猜你喜欢

Graduation season is both a farewell and a new beginning

【QT】對於Qt MSVC 2017無法編譯的問題解决

LeetCode中等题题分享(5)

SQL Server Installation Guide

PWN attack and defense world cgpwn2

SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】

Take the enclave Park as a sample to see how Yuhua and Shaoshan play the song of Chang Zhu Tan integrated development

Barbie q! How to analyze the new game app?

GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
![[cmake] cmake configuration in QT Creator](/img/e3/1cf76f88eaddb5d32184523dfb049c.png)
[cmake] cmake configuration in QT Creator
随机推荐
SQL Server 安裝指南
Leetcode skimming: stack and queue 04 (delete all adjacent duplicates in the string)
Asp .NetCore 微信订阅号自动回复之文本篇
When installing mysql, there are two packages: Perl (data:: dumper) and Perl (JSON)
在证券账户上买基金安全吗?哪里可以买基金
How to improve data quality
The difference between timer and scheduledthreadpoolexecutor
The new version of graphic network PDF will be released soon
Linux centos7 installation Oracle11g super perfect novice tutorial
[template] adaptive Simpson integral
九州云与英特尔联合发布智慧校园私有云框架,赋能教育新基建
Which securities company is safer to open a stock account
Asp . Text of automatic reply to NETCORE wechat subscription number
Dongge cashes in and the boss retires?
下载在线视频 m3u8使用教程
EMC circuit protection device for surge and impulse current protection
记录一下大文件上传偶然成功偶然失败问题
13 MySQL constraint
股票开户哪个证券公司比较安全
[embedded system course design] a single key controls the LED light