<template>
	<view class="content">
		<u-button type="success" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信一键登录</u-button>
	</view>
</template>

<script>
	import {
		login
	} from "@/api/login.js"
	export default {
		components: {},
		data() {
			return {
				path: '/pages/index/index',
				lanuch: true
			}
		},
		onShow() {
			
		},
		mounted() {},
		onLoad(option) {
			console.log(option)
			const {
				path,
				lanuch
			} = option;
			this.lanuch = (lanuch + '') === 'true';
			if (path) {
				this.path = decodeURIComponent(path);
			}
		},
		methods: {
			getPhoneNumber(e) {
				let _this = this
				uni.showLoading({
					mask: true,
					title: '加载中'
				});
				console.log(e.target.encryptedData,'eeeeeee')
				if (e.target.errMsg === 'getPhoneNumber:ok') {
					uni.login({
						success(res) {
							login({
								code: res.code,
								encryptedData: e.target.encryptedData,
								iv: e.target.iv
							}).then(res => {
								uni.hideLoading();
								console.log(res, 'login data')
								if (res.status == '200') {
									console.log(res.data, 'login data')
									getApp().globalData.token = res.data
									_this.$u.vuex('vuex_token',res.data)
									uni.setStorageSync('loginTimeOut', 'NO')
									if (_this.path === '/pages/index/main' || _this.lanuch) {
										uni.reLaunch({
											url: _this.path
										});
									} else {
										uni.redirectTo({
											url: _this.path
										});
									}
								} else {
									uni.showToast({
										title: res.message,
										icon: 'none',
										duration: 2500
									});
								}
							});
						}
					})
				} else {
					uni.showToast({
						title: '授权失败',
						icon: 'none',
						duration: 2500
					});
				}
			}
		}
	}
</script>

<style>
	.content {
		padding: 0;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>
