<template>
	<view class="bg">
		<view class="p"><image src="../../static/profile.jpg" class="pic" mode="widthFix"></image></view>
		<view class="title">智慧旅游小程序正在使用智慧旅游平台登陆提供的服务，平台登陆将使用:</view>
		<view class="info">
			<text v-if="first">您的公开信息（昵称，头像，性别等）</text>
			<text v-else>您的手机号</text>
		</view>
		<button class="btn" @click="getUserProfile()" v-if="first">允许使用</button>
		<button class="btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" style="background-color: #2196F3" v-else>绑定手机号</button>
		<button class="btn" style="background-color: #9E9E9E;" @click="cancel()">取消</button>
	</view>
</template>
<script>
export default {
	data() {
		return {
			first: true //是否第一次使用，第一次使用需要绑定手机号
		};
	},
	methods: {
		getUserProfile() {
			let code = '';
			uni.login({
				provider: 'weixin',
				success: res => {
					code = res.code;
				}
			});
			//获取用户信息
			uni.getUserProfile({
				desc: '用于完善会员资料',
				lang: 'zh_CN',
				provider: 'weixin',
				success: info => {
					this.$http.request({
						url: this.$http.urls.wxLogin + code,
						data: info,
						method: 'POST',
						success: res => {
							res.data.data.memberInfo.apiToken = res.data.data.apiToken;
							uni.setStorageSync('user', res.data.data.memberInfo);
							//未绑定手机号
							if (res.data.data.memberInfo.mobile == null || res.data.data.memberInfo.mobile == '') {
								this.first = false;
							} else {
								uni.navigateBack({ delta: 1 });
							}
						}
					});
				},
				fail: res => {
					this.click = true;
				}
			});
		},
		// 绑定用户手机号
		getPhoneNumber(e) {
			let item = {};
			uni.login({
				provider: 'weixin',
				success: res => {
					item.code = res.code;
					item.encryptedData = e.target.encryptedData;
					item.iv = e.target.iv;
					this.$http.request({
						url: this.$http.urls.bindWxMobile,
						data: item,
						method: 'POST',
						success: r => {
							uni.navigateBack({ delta: 1 });
						}
					});
				}
			});
		},
		cancel() {
			uni.navigateBack();
		}
	}
};
</script>

<style lang="less">
page {
	background-color: white;
}
.bg {
	padding: 30px;
	.p {
		text-align: center;
		padding: 15px;
		.pic {
			width: 100px;
			height: 100px;
			border-radius: 50%;
		}
	}
	.title {
		font-weight: bold;
		font-size: 16px;
	}
	.info {
		color: #8b918d;
		margin-top: 10px;
		.icon {
			padding-right: 3px;
		}
	}
	.btn {
		margin-top: 20px;
	}
}
</style>
