import React, { useEffect } from 'react';
import { View, } from '@tarojs/components';
import { check_login_status } from '@/services/loginService';
import { useUserActions } from '@/store/userStore';
import Taro from '@tarojs/taro';

const HomePage: React.FC = () => {
    const { fetchUserInfo } = useUserActions();

    useEffect(() => {
        const handleLoginRedirect = async () => {
            const login_status = check_login_status();

            if (login_status) {
                try {
                    // 先获取用户信息
                    await fetchUserInfo();
                    // 用户信息获取成功后跳转到列表页
                    Taro.redirectTo({ url: '/game_pages/list/index' });
                } catch (error) {
                    console.error('获取用户信息失败:', error);
                    // 如果获取用户信息失败，跳转到登录页
                    Taro.redirectTo({ url: '/login_pages/login/index/index' });
                }
            } else {
                Taro.redirectTo({ url: '/login_pages/login/index/index' });
            }
        };

        handleLoginRedirect();
    }, [fetchUserInfo]);

    return (
        <View className="home_page">
            <View>加载中...</View>
        </View>
    );
}

export default HomePage;