<template>
	<view class="content">
		<!-- #ifndef H5 -->
		<!-- <image class="bg-img" :src="baseURL+urlFile+'/img/img09.png'" mode=" scaleToFill"></image> -->
		<view class="logo-img-box">
			<!-- <image class="logo-img" src="https://zhxc2030.com/img/logo.jpg" mode=" aspectFit"></image> -->
			<button class="userInfo" type="warn" open-type="getUserInfo" @getuserinfo='userInfoData'>
				<text class="iconfont iconweixin"></text>
				<text>
				微信授权登录
				</text>
			</button>
		</view>
		<!-- #endif -->
	</view>
</template>

<script>
	import { getUserInfo } from '@/api/login.js';
// #ifdef H5
import { loginWinxin } from '@/utils/wxAuthorized';
// #endif
// #ifdef MP-WEIXIN
import { loginWinxinMp } from '@/utils/wxMinProgram';
import { wechatMpAuth } from '@/api/wx';
// #endif
import { mapMutations,mapState } from 'vuex';
export default {
	data() {
		return {
			userInfo:{},//授权用户信息
			code:''//授权code
		};
	},
	onLoad(option) {
		this.loadData();
	},
	computed: {
		...mapState(['baseURL','urlFile']) 
	},
	methods: {
		...mapMutations('user',['login', 'setUserInfo']),
		loadData() {
			let obj = this;
			// #ifdef H5
			loginWinxin();
			// #endif
			// #ifdef MP-WEIXIN
			loginWinxinMp().then(() => {
					wx.login({
						success(e) {
							console.log(e);
							obj.code = e.code;
						},
						fill:function (e) {
							console.log(e)
						}
					})
			});
			// #endif
		},
		// 用户确认授权
		userInfoData(e){
			this.userInfo = e;
			console.log(e)
			this.loadMp()	
		},
		// #ifdef MP-WEIXIN
		loadMp() {
			let obj = this;
			// 获取登录授权页数据
			let user = obj.userInfo;
			console.log(user)
			// 获取推广人id
			let spread_spid = uni.getStorageSync('spread') || '';
			// #ifdef MP
			let spread_code = uni.getStorageSync('spread_code') || '';
			// #endif
			wechatMpAuth({
				code: obj.code,
				iv: user.target.iv,
				encryptedData: user.target.encryptedData,
				spread_spid: spread_spid,
				// #ifdef MP
				spread_code: spread_code,
				// #endif
			}).then(({ data }) => {
				obj.wchatAuth(data);
				console.log(data)
			});
		},
		// #endif
		wchatAuth(data) {
			let obj = this;
			// 保存token
			uni.setStorageSync('token', data.token);
			// 获取用户基础信息
			getUserInfo({}).then(e => {
				obj.login();
				// 保存返回用户数据
				obj.setUserInfo(e.data);
				let ur = uni.getStorageSync('present') || '/pages/index/index';
				// 用于处理缓存bug
				if (ur=='pages/shop/product') {
					ur = '/pages/index/index'
				}
				uni.navigateTo({
					url: ur,
					fail(e) {
						uni.navigateTo({
							url: ur,
							fail(e) {
								uni.navigateTo({
									url: '/pages/index/index',
								});
							}
						});
					}
				});
			});
		}
	}
};
</script>

<style lang="scss">
page,
.content {
	height: 100%;
}
.bg-img,
.logo-img-box {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
}
.logo-img {
	margin-top: 20vh;
	margin-left: 176rpx;
	width: 385rpx;
	height: 394rpx;
}
.userInfo {
	margin: 0 100rpx;
	margin-top: 50rpx;
	color: #FFFFFF;
	border-radius: 99rpx;
	background-color: $base-color !important;
}
</style>
