<template>
	<view class="content">
		<image class="logo" :src="config?.logo[0]?.url||'/static/logo.jpg'"></image>
	</view>
</template>

<script setup lang="ts">
	import { inject, ref } from 'vue';
	//该依赖已内置不需要单独安装
	import { onLoad } from "@dcloudio/uni-app";
	// 请求接口
	const $api = inject('$api');
	const $app = inject('$app');
	const $config = inject('$config');
	// 基本信息
	const config = ref({ logo: [] });
	onLoad(async () => {
		await searchConfig();
		await search();
	})
	// config信息
	const searchConfig = async () => {
		let res = await $api(`config`, 'GET', {});
		if (res.errcode == 0) {
			config.value = res.data[0] || {}
			uni.setStorage({
				key: 'config',
				data: res.data[0] || {},
			})
		}
	};
	// 查询
	const search = async () => {
		uni.getStorage({
			key: 'openid',
			success: async function (res) {
				const user = await $api('token/openid', 'POST', {
					openid: res.data
				})
				if (user.errcode == '0' && user.data?._id) {
					uni.setStorage({
						key: "user",
						data: user.data
					})
				} else {
					uni.removeStorage({
						key: 'user',
						success: function (res) {
							console.log('success');
						}
					});
				}
				uni.reLaunch({
					url: `/pages/home/index`
				})
			},
			fail: function (err) {
				uni.login({
					success: async function (res) {
						if (res.code) {
							uni.reLaunch({
								url: `/pages/home/index`
							})
							const aee = await $app('/wechat/api/login/app', 'GET', {
								js_code: res.code,
								config: $config.wx_projectkey
							})
							if (aee.errcode == '0') {
								uni.setStorage({
									key: "openid",
									data: aee.data.openid
								})
								const user = await $api('token/openid', 'POST', {
									openid: aee.data.openid
								})
								if (user.errcode == '0' && user.data?._id) {
									uni.setStorage({
										key: "user",
										data: user.data
									})
								} else {
									uni.removeStorage({
										key: 'user',
										success: function (res) {
											console.log('success');
										}
									});
								}
								uni.reLaunch({
									url: `/pages/home/index`
								})
							} else {
								uni.showToast({
									title: aee.errmsg,
									icon: 'none'
								})
							}
						} else {
							uni.showToast({
								title: res.errMsg,
								icon: 'none'
							})
						}
					}
				});
			}
		})
	};
</script>
<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width: 100vw;
		height: 100vh;

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