当前位置:网站首页>uniapp实现授权登录
uniapp实现授权登录
2022-06-10 10:39:00 【wcdunf】
<template>
<view>
<!-- #ifdef MP-WEIXIN -->
<view v-if="isCanUse">
<view>
<view class='header'>
<image src='../../static/pen.png'></image>
</view>
<view class='content'>
<view>申请获取以下权限</view>
<text>获得你的公开信息(昵称,头像、地区等)</text>
</view>
<button class="bottom" type='primary' @click="getUserProfile">
登录授权
</button>
</view>
</view>
<view v-if="isCanUse2">
<view>
<view class='header'>
<image src='../../static/pen.png'></image>
</view>
<view class='content'>
<view>申请获取以下权限</view>
<text>获得你的手机号用于绑定登录</text>
</view>
<button class='bottom' type='primary' open-type="getPhoneNumber" @getphonenumber="PhoneNumber">
授权手机登录
</button>
</view>
</view>
<!-- #endif -->
</view>
</template>
<script>
import basesevers from '../../common/basesevers.js'
export default {
data() {
return {
code: "",
SessionKey: '',
OpenId: '',
avatar: '',
nick_name: "",
avatarUrl: null,
isCanUse: false, //默认为true
isCanUse2: false //默认为true
};
},
methods: {
//第一授权获取用户信息===》按钮触发
// 调用wx.getUserProfile接口
getUserProfile() {
var that = this
// #ifdef MP-WEIXIN
uni.getProvider({
service: 'oauth',
success: function(res) {
if (~res.provider.indexOf('weixin')) {
wx.login({
success: (res) => {
that.code = res.code
console.log('获取信息code', res.code);
}
})
uni.getUserProfile({
desc: '登录',
success: async (res) => {
uni.setStorageSync('isCanUse', false);
that.isCanUse = false
that.isCanUse2 = true
// 后端接口
console.log('获取信息getUserProfile', res);
that.avatar = res.userInfo.avatarUrl
that.nick_name = res.userInfo.nickName
// 这里应该读接口,暂时从前端读
uni.setStorageSync('avatar', res.userInfo.avatarUrl);
uni.setStorageSync('nick_name', res.userInfo.nickName);
},
fail: (res) => {
console.log(res)
}
});
} else {
uni.showToast({
title: '请先安装微信或升级版本',
icon: "none"
});
}
}
});
//#endif
},
//登录
login() {
let _this = this;
uni.showLoading({
title: '登录中...'
});
},
PhoneNumber(e) {
var _this = this
var encry = e.detail.encryptedData;
var iv = e.detail.iv;
// 不允许授权
if (e.detail.errMsg !== "getPhoneNumber:ok") {
console.log("没有授权")
_this.isCanUse = true;
_this.isCanUse2 = false;
return;
}
console.log('获取信息手机', e);
uni.request({
url: basesevers.request2 + '/connect/token',
//url: 'http://192.192.192.183:7003/connect/token',
method: 'post',
dateType: "json",
header: {
'content-Type': 'application/x-www-form-urlencoded' //自定义请求头信息
},
data: {
grant_type: 'wechat_mini_code', //你的小程序的APPID
client_id: 'client.custom', //你的小程序秘钥secret,
code: _this.code, //wx.login 登录成功后的code
username: encry,
iv: iv,
avatar: _this.avatar,
nick_name: _this.avatar
},
success: (cts) => {
if (cts.statusCode == 200) {
if (!("access_token" in cts.data)) {
console.log('获取失败'); //
} else {
uni.setStorageSync('access_token', cts.data.access_token);
console.log('获取成功'); //
// 请求头像开始
uni.request({
url: basesevers.request2 + '/api/identity/my-profile',
method: 'get',
dateType: "json",
header: {
'content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
"Authorization": 'Bearer ' + cts.data.access_token
},
data: {
// grant_type: 'wechat_mini_code', //你的小程序的APPID
},
success: (cts2) => {
if (cts.statusCode == 200) {
console.log("获取后端返回数据", cts2)
// uni.setStorageSync('sub', cts2.data.sub); //写入id
// uni.setStorageSync('name', cts2.data.name); //写入name
uni.reLaunch({ //信息更新成功后跳转到小程序首页
url: '/pages/index/index'
});
} else {
console.log("失败")
}
}
});
}
} else {
console.log("没有授权")
_this.isCanUse = true;
_this.isCanUse2 = false
}
}
});
},
},
onLoad() { //默认加载
// this.login();
console.log(this.isCanUse)
console.log(uni.getStorageSync('isCanUse'))
if (uni.getStorageSync('access_token') != "") {
uni.reLaunch({ //信息更新成功后跳转到小程序首页
url: '/pages/index/index'
});
} else {
this.isCanUse = true
}
}
}
</script>
<style>
.header {
margin: 90rpx 0 90rpx 50rpx;
border-bottom: 1px solid #ccc;
text-align: center;
width: 650rpx;
height: 300rpx;
line-height: 450rpx;
}
.header image {
width: 200rpx;
height: 200rpx;
}
.content {
margin-left: 50rpx;
margin-bottom: 90rpx;
}
.content text {
display: block;
color: #9d9d9d;
margin-top: 40rpx;
}
.bottom {
border-radius: 80rpx;
margin: 70rpx 50rpx;
font-size: 35rpx;
}
</style>
边栏推荐
- 音质出色的降噪旗舰,女毒必选,贝壳王子MO3体验
- Pat class a 1134 vertex coverage
- 【无标题】
- 子网划分不会?瑞哥带你深入理解IP地址,手把手教你子网划分!
- 2022 examination questions and answers of labor worker post skills (labor worker)
- 塔米狗分享|房地产企业并购只增不减,那么上哪儿找这些项目呢?
- No subnet partition? Rego will help you understand IP addresses and teach you how to divide subnets!
- Pat class a 1126 Euler path
- Phpstorm configuration database connection
- 股票获取最大利润
猜你喜欢

2021 ciscn PWN preliminary
![[first hpm6750 evaluation] RT thread development environment construction and hello world](/img/c0/0f4f8fc5b3d3a726fce5a47ce7e783.png)
[first hpm6750 evaluation] RT thread development environment construction and hello world

Extending visual transformer to gigapixel images based on hierarchical self supervised learning

Leetcode 2000. Reverse word prefix

基于分层自监督学习将视觉Transformer扩展到千兆像素图像

Leetcode 1991. Find the middle position of the array (brute force enumeration)

Working principle and application of single chip trigger or not gate

MySQL architecture

Transfer of 30% equity of Zhuhai Gaoyuan Electric Energy Technology Co., Ltd., shared by tamigou

保温热水更方便,日常泡奶的小神器,七彩叮当无线便携式调奶器上手
随机推荐
切换npm的淘宝镜像
Colorui color matching details
PV operation daily question - orange apple question (Preliminary Edition)
Implementation code of several recent good papers (with source code download)
Review the growth evaluation of central enterprises and listed companies
杰理之BLE 动态调节功率【篇】
fragment实现底部导航栏不刷新切换
Test question bank and simulation test for operation certificate of ordinary scaffolder (special type of construction work) in 2022
It is very convenient to make a data analysis crosstab with one line of code
MATLAB 学习笔记(3)MATLAB 矩阵的进阶操作
PV operation daily question - orange apple question (advanced version)
Leetcode 2000. 反转单词前缀
fcpx插件:PremiumVFX Animation Presets(动画循环预设) v1.0.1特别版
MySQL architecture
PV操作每日一题-橘子苹果问题(高阶版变式)
Pat class a 1134 vertex coverage
Mixer: an indispensable component of video conference recording
The winning list of "talking tom cat family - time light chaser" 3D digital collection was announced
"Great freehand flower and bird painting master Mr. likuchan" blockbuster digital collection launched in the whole network
2023王道C语言训练营(线索二叉树)