uniapp 微信小程序 用户授权代码

<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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值