当前位置:网站首页>uniapp授权登陆获取用户信息和code
uniapp授权登陆获取用户信息和code
2022-07-18 01:01:00 【@小朱呀路】

<template>
<view class="content">
<view class="text-area" @click="handleGetUserInfo" style="background-color: #007AFF;padding: 20rpx;color: #FFFFFF;">微信授权登陆</text>
</view>
</view>
</template>
<script>
export default {
data() {
return {
title: 'Hello',
formData:null
}
},
onLoad() {
},
methods: {
handleGetUserInfo() {
this.onGetUserInfo().catch(error => {
console.log(error)
})
},
// 微信登录
async onGetUserInfo() {
const {
options
} = this;
// 获取用户信息
const userInfo = await this.onwechatInfos();
// 获取code
const code = await this.onwechatcodes();
// 拿到用户信息和code调用后端接口
// const data = await store.dispatch('loginAuthWechat', {
// code,
// oauth: 'MP-WEIXIN',
// userInfo
// });
},
// 获取用户信息
async onwechatInfos() {
return new Promise((resolve, reject) => { // 判断应用的 API,回调,参数,组件等是否在当前版本可用。 if (!uni.canIUse('getUserProfile')) return null;
uni.getUserProfile({
lang: 'zh_CN',
desc: '获取用户相关信息',
success: res => {
console.log('用户同意了授权',res);
this.formData = res.userInfo;
return resolve(res.userInfo);
},
fail: err => {
uni.showToast({
title: '用户拒绝了授权',
icon: 'fail'
});
return reject(err);
},
});
})
},
// 获取code
async onwechatcodes() {
// 获取服务供应商
const [providerErr, providerData] = await uni.getProvider({
service: 'oauth'
});
if (providerErr) return uni.showToast({
title: '没有获取到服务商',
icon: "none"
})
const provider = providerData.provider;
if (provider.includes('weixin')) {
const options = {
provider: provider[0] //'weixin'
};
const logodata = await uni.login(options)
const [loginErr, loginData] = logodata;
console.log(loginData.code,'获取code')
if (loginErr) return uni.showToast({
title: loginErr,
icon: 'none'
}); //判断是否支持微信登录。
return loginData.code;
}
return null;
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>
边栏推荐
- Offline installation of MariaDB
- [MCU simulation project] alarm light (Proteus schematic diagram +keil code)
- Qt | 控件之QComboBox
- Detailed explanation of the life cycle of arkui development framework components
- Open source WPF control library adonisui
- Advanced customization of uni app [day13]
- 【FPGA】:ip核-----CIC滤波器
- [MCU simulation project] LED flashing light (Proteus schematic +keil code)
- Identity server 4 uses openid connect to add user authentication (3)
- 读书笔记:程序员的自我修养---第二章
猜你喜欢

排序子序列与倒置字符串

Liquibase learning 2 - Introduction to the extension plug-in liquibase data

Opencv image transparent area clipping imagecroppingtrn

Financial banking software testing super large strategy, the most popular financial banking big secret cover questions

阿叶的新发型

ES6 deconstruction and assignment can be learned as soon as possible

Through JMeter pressure measurement surging

使用ZVS驱动无线充电线圈

Griddlyjs: Web ide based on Reinforcement Learning

Learning notes of JUC source code 2 - AQS sharing and semaphore, countdownlatch
随机推荐
遥远的距离
Identity server 4 uses openid connect to add user authentication (3)
Pat b-b1005 continue (3n+1) guess (25) [array]
Codeforce:b. difference array [optimization of prefix 0]
Offline installation of MariaDB
OpenCV-图像透明区裁剪ImageCroppingTRN
JVM personal learning notes
Online dice rolling dice tool
ffifdyop绕过MD5进行sql注入
Design of the multi live architecture in different places of the king glory mall
Is it safe for xinjimin to buy funds in a stock account
hcia--OSPF实验报告
wpa_ supplicant @authenticate
保持淡然的心情
Next stop, embossed AI
[MCU simulation project] LED flashing light (Proteus schematic +keil code)
Case study on data management of low code building design company
[MCU simulation project] alarm light (Proteus schematic diagram +keil code)
codeforce:B. Difference Array【前缀0的优化】
Liquibase learning 3 - loglevel log level, common commands