<template>
	<view class="flex-column">
		<!-- 标题 -->
		<view class="top">
			<view class="YS-title" style="color: #4169E1;">欢迎您的使用</view>
			<view class="YS-subtitle">醍摩豆家长助您孩子成长</view>
		</view>
		<view class="detail-image" :style="{backgroundImage:`url(${image})`}"></view>
		<button class="btn" plain="true" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取您的孩子信息</button>
		<!-- 动画 -->
		<view class="ocean"></view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	//引入解密工具
	import WXBizDataCrypt from "@/utils/WXBizDataCrypt.js";
	export default {
		computed: {
			...mapState('m_parent', ['token', 'parentInfo'])
		},
		data() {
			return {
				image: 'https://image.meiye.art/pic_1631411821366jJzYRG2jdJOxwXZk_jz7o',
				isClick: false
			}
		},
		methods: {
			...mapMutations('m_parent', ['updateChildrenData', 'updateUserData', 'updatePhoneNumber']),
			//获取用户手机号
			async getPhoneNumber(e) {
				if (this.isClick === false) {
					this.isClick = true
					if (e.detail.errMsg !== "getPhoneNumber:ok") {
						return uni.showToast({
							title: '您已拒绝授权',
							icon: 'error'
						});
					} else {
						try {
							if (this.token) {
								let proof = await new WXBizDataCrypt('wx5705da8747c77cfe',
									this.token.miniappData.session_key)
								let numData = await proof.decryptData(e.detail.encryptedData, e.detail.iv)
								this.updatePhoneNumber(numData.phoneNumber)
								console.log(numData.phoneNumber);
								//初始化App
								// await this.$initStart(numData.phoneNumber)
								this.$api.getChildrenInfo({
									mobile: '17711533106'
								}).then(res => {
									console.log('返回数据', res);
									this.updateChildrenData(res.data.guardian.students)
								}).catch(err => {
									uni.showToast({
										title: '加载数据失败~',
										icon: 'error',
									})
								})
								this.$api.getUserData({
									phoneNumber: numData.phoneNumber
								}).then(res => {
									console.log('返回数据', res);
									this.updateUserData(res.data)
								}).catch(err => {
									uni.showToast({
										title: '加载数据失败~',
										icon: 'error',
									})
								})
								uni.navigateTo({
									url: '/subpkg/startup/options'
								})
							} else {
								this.$logOut()
								return uni.showToast({
									title: '获取不到您的信息请重新登录',
									icon: 'error'
								});
							}
						} catch (e) {
							this.$logOut()
							return uni.showToast({
								title: '身份验证已过期请重新登录',
								icon: 'error'
							});
						}
					}
					this.isClick = false
				}
			},
		},
	}
</script>

<style lang="scss">
	@import 'startup_pages.scss';

	.detail-image {
		height: 600rpx;
		margin: 400rpx 0 0 0;
		background-size: 100% 100%;
	}
</style>
