<template>
	<view class="content">
		<view class="one">
			<image class="logo" :src="logoUrl||'/static/logo.png'" />
		</view>
	</view>
</template>

<script setup lang="ts">
	import { getCurrentInstance, computed, ref } from 'vue';
	//该依赖已内置不需要单独安装
	import { onShow } from "@dcloudio/uni-app";
	// 请求接口
	const $api = getCurrentInstance()?.appContext.config.globalProperties.$api;
	const $app = getCurrentInstance()?.appContext.config.globalProperties.$app;
	const $config = getCurrentInstance()?.appContext.config.globalProperties.$config;
	// openid
	const openid = computed(() => {
		return uni.getStorageSync('openid');
	})
	// logo图片
	const logoUrl = ref('');
	onShow(() => {
		search();
		searchConfig();
	})
	// 用户信息
	const search = async () => {
		uni.login({
			success: async function (result) {
				if (!result.code) {
					uni.showToast({
						title: '登录失败请重进！',
						icon: 'fail',
					});
					return false;
				}
				if (!openid.value) {
					const res = await login(result.code)
					if (res) uni.setStorageSync('openid', res);
				}
				uni.redirectTo({
					url: '/pages/home/index'
				})
			}
		})
	};
	// 获取openid
	const login = async (js_code : string) => {
		const res = await $app('/wechat/api/login/app', 'GET', {
			js_code: js_code,
			config: $config.wx_projectkey
		})
		if (res.errcode === 0) return res.data.openid;
		else {
			uni.showToast({
				title: '登录失败请重进！',
				icon: 'fail',
			});
			return false;
		}
	};
	// config信息
	const searchConfig = async () => {
		const res = await $api('matchconfig/findOne', 'GET', {});
		if (res.code === 200) {
			if (res.data) {
				logoUrl.value = res.data.logoUrl
				uni.setStorageSync('config', res.data);
			}
		} else {
			uni.showToast({
				title: res.msg || '',
				icon: 'error',
			});
		}
	};
</script>
<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			text-align: center;
			margin: 50vw 0 0 0;

			.logo {
				width: 50vw;
				height: 50vw;
				border-radius: 90px;
				box-shadow: 0 0 5px #f1f1f1;
			}
		}
	}
</style>