<template>
	<view>
		<view class="ceng" @tap="hidePopupLogin"></view>
		<view class="popup-login">
			<view class="close" @tap="hidePopupLogin">
				<image class="image" src="/static/pages/images/close.png"></image>
			</view>
			<image :src="about.CONFIG_PROJECT_LOGO" mode="aspectFill" class="logo"></image>
			<view class="title">{{about.CONFIG_PROJECT_TITLE}}</view>
			<view class="l-btn" @click="getWXUserProfile()">授权登录</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	const req = require("../../utils/request");
	export default {
		data() {
			return {
				sessionKey: '',
				openid:''
			};
		},
		props: {
			about: Object,
		},
		watch: {},
		options: {},

		mounted() {
			app.globalData.getCheckSessoin(json => {
				this.sessionKey = json.session_key;
				this.openid = json.openid;
				if(json.unionid){
					this.unionid = json.unionid;
				}
			});
		},

		methods: {
			hidePopupLogin() {
				this.$emit('hidePopupLogin');
			},
			getWXUserProfile() {
				uni.getUserProfile({
					desc: '注册',
					success: res => {
						this.toSubmitLogin(res.iv, res.encryptedData);
					},
					fail: res => {
						req.msg(res);
					}
				});
			},
			toSubmitLogin(iv, encryptedData) {
				let that = this;
				if (!encryptedData || !iv) return false;
				let _params = {
					sessionKey: this.sessionKey,
					openId: this.openid,
					iv: iv,
					encryptedData: encryptedData
				};
				if (req.getStorage('pidCode')) {
					_params.parentId = req.getStorage('pidCode');
				}
				if (that.unionid) {
					_params.unionid = that.unionid;
				}
				req.postRequest('/api/login', _params, json => {
					let params = json;
					req.setStorage('userInfo', json);
					that.hidePopupLogin();
				});
			},
		}
	};
</script>

<style scoped>
	@import "./index.css";
</style>
