<template>
	<view class="wrap">
		<view class="tips">
			<image class="b-image" src="/static/imgs/loading_image.png" alt="" mode="aspectFit"></image>
			<view class="b-tit">尝试自动登录，请稍后....</view>
		</view>
	</view>
</template>

<script>
import { wechatAutoLogin } from '@/service/api/user.js';
import { smartLogin } from '@/service/request/main.js';
import { encodeRedirectUrl, decodeRedirectUrl } from '../../common/util.js';

export default {
	data() {
		return {};
	},
	onLoad(options) {
		console.log('onLoad autoLogin', options);
		// 微信小程序自动登录
		const returnPage = options.from ? decodeRedirectUrl(options.from) : 'pages/home/index';
		uni.login().then(([err, res]) => {
			console.log('uni.login', err, res);
			if (!err) {
				wechatAutoLogin(res.code).then(([err, res]) => {
					console.log('wechatAutoLogin', err, res);
					this.$store.commit('unlock', 'silentReload');
					if (!err) {
						console.warn('needLogin silentReload', 'success');
						this.$store.commit('setAccessToken', res.token);
						this.$store.commit('setActivityId', res.activity_id);
						// 返回之前页面
						uni.reLaunch({
							url: '/' + returnPage
						});
					} else {
						console.error('needLogin silentReload', 'fail');
						// uni.reLaunch({
						// 	url: '/answer_pages/user/login?from=' + encodeRedirectUrl(returnPage)
						// })
						smartLogin(returnPage);
					}
				});
			} else {
				console.error('needLogin silentReload', 'code fail');
				this.$store.commit('unlock', 'silentReload');
				// uni.reLaunch({
				// 	url: '/answer_pages/user/login?from=' + encodeRedirectUrl(returnPage)
				// })
				smartLogin(returnPage);
			}
		});
	},
	methods: {}
};
</script>

<style lang="scss">
page {
	padding-bottom: env(safe-area-inset-bottom);
	background: #ffffff;
	height: 100%;
}

.wrap {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 100%;
	height: 100%;
}

.tips {
	display: flex;
	flex-direction: column;
	align-items: center;

	.b-image {
		width: 300upx;
		height: 240upx;
	}

	.b-tit {
		margin-top: 40upx;
		color: #666;
		font-size: 28upx;
	}
}
</style>
