当前位置:网站首页>【微信授权登录】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~
边栏推荐
猜你喜欢

GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速

What does open loop and closed loop mean?

Leetcode skimming: stack and queue 01 (realizing queue with stack)

Leetcode skimming: binary tree 02 (middle order traversal of binary tree)

Leetcode96 different binary search trees

SQL Server 安装指南

2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details

Digital transformation has a long way to go, so how to take the key first step

Kuberntes cloud native combat high availability deployment architecture

Niuke - Practice 101 - reasoning clown
随机推荐
LeetCode 0241. Design priority for arithmetic expressions - DFS
Review data desensitization system
启牛商学院给的证券账户安不安全?哪里可以开户
Is the securities account given by qiniu business school safe? Where can I open an account
Cmake engineering related
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
在证券账户上买基金安全吗?哪里可以买基金
[QT] test whether QT can connect to the database
Niuke - Practice 101 - reasoning clown
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
微信小程序缓存过期时间的相关设置(推荐)
When installing mysql, there are two packages: Perl (data:: dumper) and Perl (JSON)
Learn online case practice
Promise和模块块化编程
Node -- egg implements the interface of uploading files
What is ThreadLocal memory leak and how to solve it
The origin of usb-if Association and various interfaces
【opencv】train&test HOG+SVM
Difficult to get up syndrome (bit by bit greed)
I would like to ask, which securities is better for securities account opening? Is it safe to open a mobile account?