<template>
	<view style="text-align: center;">
		<!--pages/user/login/login.wxml-->
		<image class="logo" src="/static/images/ui/KXMH_icon.png" />
		<!-- #ifndef MP-ALIPAY || MP-BAIDU -->
		<button class="login" @tap="getUserInfo">授权登录</button>
		<!-- #endif -->
		<!-- #ifdef MP-BAIDU -->
		<button class="login" open-type="getUserInfo" @getuserinfo="getUserInfo">授权登录</button>
		<!-- #endif -->
		<!-- #ifdef MP-ALIPAY -->
		<button class="login" open-type="getAuthorize" @getAuthorize="getUserInfo" onError="onAuthError"
			scope='userInfo'>
			授权登录
		</button>
		<!-- #endif -->

		<uModalExtra :show="show" showCancelButton @getphonenumber='getPhoneNumber' @onGetAuthorize="onGetAuthorize"
			confirm-button-open-type="getPhoneNumber" @cancel="show=false">
			<view class="slot-content dialog_content">
				<view class="flex jc-c">
					<u-icon name="checkmark-circle" color="green" size="120rpx" />
				</view>
				<view class="_l">用户信息授权成功</view>
				<view>授权绑定你的手机号</view>
			</view>
		</uModalExtra>
	</view>
</template>

<script>
	// pages/user/login/login.js
	import uModalExtra from '@/components/u-modal-extra/u-modal-extra';
	const app = getApp();
	import {
		httpUtil,
		http,
		getUserInfoUtil,
		tokenAndLogin
	} from '../../../utils/util';
	export default {
		components: {
			uModalExtra
		},
		data() {
			return {
				code: '',
				userInfo: null,
				show: false
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {

		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},
		methods: {
			async getUserInfo(e) {
				let res = await getUserInfoUtil(e)
				if (res.code) {
					let code = res.code
					app.globalData.code = code;
					this.userInfoX = res.userInfo
					this.show = true
				}
			},
			async getPhoneNumber(e) {
				this.setData({
					show: false
				})
				if (!getApp().globalData.code) {
					uni.showToast({
						title: '获取手机号被拒'
					})
					return
				}
				let res = null
				// #ifndef MP-ALIPAY
				if (!e.detail.encryptedData || !e.detail.iv) {
					uni.showToast({
						title: '请绑定手机号'
					})
					return
				}
				res = tokenAndLogin(this.userInfoX, getApp().globalData.code, e.detail.encryptedData, e.detail.iv)
			
			// #endif

			// #ifdef MP-ALIPAY
			let res1 = await uni.getPhoneNumber()
			if (!res1) {
				uni.showToast({
					title: '手机号获取失败'
				})
				return
			}
			res = await tokenAndLogin(this.userInfoX, getApp().globalData.code, res1[1].response)
			// #endif
			if(!res){
				uni.showToast({
					title: '登录接口失效！'
				});
				return
			}
			setTimeout(() => {
				uni.showToast({
					title: '登录成功！'
				});
				uni.$emit('initData');
				uni.navigateBack();
				var pages = getCurrentPages(); //获取页面栈
				if (pages.length > 1) {
					//上一个页面实例对象
					var prePage = pages[pages.length - 2];
					prePage.onLoad(prePage.options);
				}
			}, 1000);
		},

	}
	};
</script>
<style>
	/* pages/user/login/login.wxss */
	.logo {
		width: 100px;
		height: 100px;
		border-radius: 50%;
		margin: 100px 0 50px 0;
	}

	.login {
		width: 500rpx;
		margin: auto;
		height: 80rpx;
		background: linear-gradient(90deg, #ff4284 0%, #ff1d42 100%);
		border-radius: 44rpx;
		color: #fff;
		font-size: 36rpx;
		line-height: 80rpx;
	}
</style>
