<template>
	<view class="main">
		<view class="one">
			<image class="logo" :src="logoUrl"></image>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				logoUrl: ''
			};
		},
		onShow: async function() {
			const that = this;
			await that.search();
			await that.searchConfig();
		},
		methods: {
			search() {
				const that = this;
				uni.getStorage({
					key: 'openid',
					success: function(res) {
						that.searchOpenid(res.data)
					},
					fail: function(err) {
						uni.login({
							success: async function(res) {
								if (res.code) {
									const aee = await that.$app('/wechat/api/login/app', 'GET', {
										js_code: res.code,
										config: that.$config.wx_projectkey
									})
									if (aee.errcode == '0') {
										uni.setStorage({
											key: "openid",
											data: aee.data.openid
										})
										that.searchOpenid(aee.data.openid)
									} else {
										uni.showToast({
											title: aee.errmsg,
											icon: 'none'
										})
									}
								} else {
									uni.showToast({
										title: res.errMsg,
										icon: 'none'
									})
								}
							}
						});
					}
				})
			},
			// 查询基本设置
			async searchConfig() {
				const that = this;
				const res = await that.$api(`/config`, 'GET', {});
				if (res.errcode == '0') {
					that.$set(that, `logoUrl`, res.data.logo_url[0].url);
					uni.setStorage({
						key: 'config',
						data: res.data,
						success: function(res) {},
						fail: function(err) {}
					})
				}
			},
			async searchOpenid(openid) {
				const that = this;
				const aee = await that.$api(`/login/wxapp/${openid}`, 'POST', {})
				if (aee.errcode == '0') {
					uni.reLaunch({
						url: `/pages/home/index`
					})
				} else {
					uni.showToast({
						title: aee.errmsg,
						icon: 'none'
					})
				}
				uni.reLaunch({
					url: `/pages/home/index`
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.main {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100vw;
		height: 100vh;

		.one {
			.logo {
				width: 50vw;
				height: 50vw;
				border-radius: 90px;
				box-shadow: 0 0 5px var(--f1Color);
			}
		}
	}
</style>