<template>
	<view class="bg">
		<view class="title">智慧旅游小程序正在使用智慧旅游平台登陆提供的服务，平台登陆将使用:</view>
		<view class="info">
			<text class="icon"></text>
			<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 {
			item: {},
			first: true
		};
	},
	onLoad() {},
	methods: {
		getUserProfile() {
			//获取用户信息
			uni.getUserProfile({
				desc: '用于完善会员资料',
				lang: 'zh_CN',
				provider: 'weixin',
				success: info => {
					console.log('登陆：' + JSON.stringify(info));
					uni.login({
						provider: 'weixin',
						success: res => {
							console.log("res:"+JSON.stringify(res));
							this.$http.request({
								url: this.$http.urls.wxLogin+res.code,
								data:info,
								method:'POST',
								success: res => {
									res.data.data.memberInfo.apiToken=res.data.data.apiToken;
									uni.setStorageSync('user', res.data.data.memberInfo);
									uni.navigateBack({ delta: 1 });
								}
							});
						}
					});
				}
			});
		},
		// 绑定用户手机号
		getPhoneNumber(e) {
			uni.login({
				provider: 'weixin',
				success: res => {
					this.item.code = res.code;
					this.item.encryptedData = e.target.encryptedData;
					this.item.iv = e.target.iv;
					this.$http.request({
						url: this.$http.urls.bindPhone,
						data: this.item,
						success: res => {
							uni.setStorageSync('user', res.data.data);
							uni.$emit('update');
							uni.navigateBack({ delta: 1 });
						}
					});
				}
			});
		},
		cancel() {
			uni.navigateBack();
		}
	}
};
</script>

<style lang="less">
page {
	background-color: white;
}
.bg {
	padding: 30px;
	.title {
		font-weight: bold;
	}
	.info {
		color: #8b918d;
		margin-top: 10px;
	}
	.btn {
		margin-top: 20px;
	}
}
</style>
