<template>
<view class="pop-up" v-if="popUpShow">
<view class="pop-box">
<view class="pop-box-title">获取用户权限</view>
<view class="pop-bot">
<view class="btn-cancel" @click="cancel">取消</view>
<!-- <button open-type="getUserInfo" @getuserinfo="getUserInfo"class="btn-confirm">确定</button> -->
<text class="btn-confirm" @click="getUserInfo">确定</text>
</view>
</view>
</view>
</template>
<script>
export default{
data() {
return{
popUpShow: false,
code: "xxxxxxx",
encryptedData:"xxxxxxx",
iv:"xxxxxxxx"
}
},
mounted() {
let that = this;
// this.phone = session.getSession("phone");
let nickName = uni.getStorageSync("nickName") //存储token
if(!nickName) {
/* 判断token为空重新授权 */
that.popUpShow = true;
uni.hideLoading();// 结束其他接口的弹框交互
uni.login({
provider:'weixin',
success(getCode) {
that.code = getCode.code;
}
})
}
},
methods:{
getUserInfo(e) {
this.popUpShow = false;
let that = this;
// 获取用户信息接口改用wx.getUserProfile(Object object) 注:改用了之前button的方式通过后台解密的方式。
uni.getUserProfile({
desc:'Wexin', // 这个参数是必须的
success:res=>{
uni.setStorageSync('avatarUrl', res.userInfo.avatarUrl)
uni.setStorageSync('nickName', res.userInfo.nickName)
this.$emit('setSessionPerso')
},
fail:err=>{
console.log(err)
}
})
},
cancel() {
uni.showModal({
content:'取消授权将无法获取头像等信息。',
confirmText: "确认",
cancelText:'取消',
success: (res) => {
if(res.confirm){
uni.showToast({
title: '取消成功',
icon:'none',
duration:2000
})
this.popUpShow = false;
}else{
console.log('取消');
}
}
})
}
}
}
</script>
<style lang="less" scoped>
.pop-up{
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 10;
background-color: rgba(0, 0, 0, .6);
}
.pop-box{
background-color: #FFFFFF;
width: 622rpx;
border-radius: 32rpx;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
overflow: hidden;
}
.pop-box-title{
padding: 56rpx 48rpx 80rpx;
color: #323233;
font-size: 32rpx;
font-weight: bold;
text-align: center;
}
.pop-bot{
width: 100%;
height: 96rpx;
display: flex;
border-top: 2rpx solid #EBEDF0;
}
.btn-cancel{
color: #8A8A8A;
font-size: 32rpx;
flex: 1;
border-right: 2rpx solid #EBEDF0;
display: flex;
justify-content: center;
align-items: center;
}
.btn-confirm{
color: #376EF1;
font-size: 32rpx;
flex: 1;
display: flex;
justify-content: center;
align-items: center;
border: none;
background-color: #FFFFFF;
}
.btn-confirm::after{
display: none;
}
</style>
uniapp 微信小程序 用户授权代码
最新推荐文章于 2024-09-25 20:58:23 发布