index.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { View, Text, Button, Image } from '@tarojs/components'
  2. import { useLoad, useDidShow } from '@tarojs/taro'
  3. import { useState } from 'react'
  4. import Taro from '@tarojs/taro'
  5. import { authManager, User } from '../../utils/auth'
  6. import './index.css'
  7. export default function Index() {
  8. const [user, setUser] = useState<User | null>(null)
  9. const [loading, setLoading] = useState(false)
  10. useLoad(() => {
  11. console.log('Index Page loaded.')
  12. checkLoginStatus()
  13. })
  14. useDidShow(() => {
  15. checkLoginStatus()
  16. })
  17. const checkLoginStatus = async () => {
  18. if (authManager.isLoggedIn()) {
  19. const userInfo = authManager.getUserInfo()
  20. setUser(userInfo)
  21. } else {
  22. setUser(null)
  23. }
  24. }
  25. const handleLogin = () => {
  26. Taro.navigateTo({ url: '/pages/login/index' })
  27. }
  28. const handleLogout = async () => {
  29. setLoading(true)
  30. try {
  31. await authManager.logout()
  32. setUser(null)
  33. Taro.showToast({
  34. title: '退出成功',
  35. icon: 'success'
  36. })
  37. } catch (error) {
  38. console.error('Logout error:', error)
  39. } finally {
  40. setLoading(false)
  41. }
  42. }
  43. const handleProfile = () => {
  44. Taro.navigateTo({ url: '/pages/profile/index' })
  45. }
  46. const goToRegister = () => {
  47. Taro.navigateTo({ url: '/pages/register/index' })
  48. }
  49. return (
  50. <View className="index-container">
  51. {user ? (
  52. <View className="user-welcome">
  53. <View className="user-avatar">
  54. <Image
  55. className="avatar-image"
  56. src={user.avatar || 'https://via.placeholder.com/100x100'}
  57. mode="aspectFill"
  58. />
  59. </View>
  60. <Text className="welcome-text">欢迎回来,{user.username}!</Text>
  61. <View className="user-info">
  62. <Text className="info-item">ID: {user.id}</Text>
  63. {user.email && <Text className="info-item">邮箱: {user.email}</Text>}
  64. <Text className="info-item">注册时间: {new Date(user.createdAt).toLocaleDateString()}</Text>
  65. </View>
  66. <View className="action-buttons">
  67. <Button className="action-button" type="primary" onClick={handleProfile}>
  68. 查看资料
  69. </Button>
  70. <Button
  71. className="action-button"
  72. type="warn"
  73. loading={loading}
  74. onClick={handleLogout}
  75. >
  76. 退出登录
  77. </Button>
  78. </View>
  79. </View>
  80. ) : (
  81. <View className="login-prompt">
  82. <View className="welcome-section">
  83. <Text className="welcome-title">欢迎使用小程序</Text>
  84. <Text className="welcome-subtitle">请先登录以使用完整功能</Text>
  85. </View>
  86. <View className="login-buttons">
  87. <Button
  88. className="login-button"
  89. type="primary"
  90. onClick={handleLogin}
  91. >
  92. 立即登录
  93. </Button>
  94. <Button
  95. className="register-button"
  96. plain
  97. onClick={goToRegister}
  98. >
  99. 注册新账号
  100. </Button>
  101. </View>
  102. <View className="features-section">
  103. <Text className="features-title">功能特色</Text>
  104. <View className="features-list">
  105. <Text className="feature-item">✅ 安全的用户认证</Text>
  106. <Text className="feature-item">✅ 完整的用户管理</Text>
  107. <Text className="feature-item">✅ 响应式界面设计</Text>
  108. <Text className="feature-item">✅ 实时数据同步</Text>
  109. </View>
  110. </View>
  111. </View>
  112. )}
  113. </View>
  114. )
  115. }