index.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100">
  51. {user ? (
  52. <View className="p-6">
  53. <View className="bg-white rounded-2xl shadow-lg p-6 mb-6">
  54. <View className="flex items-center mb-6">
  55. <View className="w-20 h-20 rounded-full overflow-hidden mr-4">
  56. <Image
  57. className="w-full h-full"
  58. src={user.avatar || 'https://images.unsplash.com/photo-1494790108755-2616b612b786?w=160&h=160&fit=crop&crop=face'}
  59. // mode="aspectFill"
  60. />
  61. </View>
  62. <View>
  63. <Text className="text-gray-600 text-sm">欢迎回来</Text>
  64. <Text className="text-2xl font-bold text-gray-900">{user.username}</Text>
  65. </View>
  66. </View>
  67. <View className="space-y-4 mb-6">
  68. <View className="flex justify-between items-center py-3 border-b border-gray-100">
  69. <Text className="text-gray-600">用户ID</Text>
  70. <Text className="text-gray-900 font-mono">{user.id}</Text>
  71. </View>
  72. {user.email && (
  73. <View className="flex justify-between items-center py-3 border-b border-gray-100">
  74. <Text className="text-gray-600">邮箱</Text>
  75. <Text className="text-gray-900">{user.email}</Text>
  76. </View>
  77. )}
  78. <View className="flex justify-between items-center py-3">
  79. <Text className="text-gray-600">注册时间</Text>
  80. <Text className="text-gray-900">{new Date(user.createdAt).toLocaleDateString()}</Text>
  81. </View>
  82. </View>
  83. <View className="flex space-x-3">
  84. <Button className="flex-1" type="primary" onClick={handleProfile}>
  85. 查看资料
  86. </Button>
  87. <Button
  88. className="flex-1"
  89. loading={loading}
  90. onClick={handleLogout}
  91. >
  92. 退出登录
  93. </Button>
  94. </View>
  95. </View>
  96. <View className="grid grid-cols-3 gap-4">
  97. <View className="bg-white rounded-xl shadow-lg p-4 text-center">
  98. <Text className="text-3xl font-bold text-blue-600 mb-1">1</Text>
  99. <Text className="text-gray-600 text-sm">今日访问</Text>
  100. </View>
  101. <View className="bg-white rounded-xl shadow-lg p-4 text-center">
  102. <Text className="text-3xl font-bold text-green-600 mb-1">7</Text>
  103. <Text className="text-gray-600 text-sm">本周活跃</Text>
  104. </View>
  105. <View className="bg-white rounded-xl shadow-lg p-4 text-center">
  106. <Text className="text-3xl font-bold text-purple-600 mb-1">30</Text>
  107. <Text className="text-gray-600 text-sm">本月使用</Text>
  108. </View>
  109. </View>
  110. </View>
  111. ) : (
  112. <View className="p-6">
  113. <View className="text-center mb-8">
  114. <Image
  115. className="w-32 h-32 rounded-full mx-auto mb-4"
  116. src="https://images.unsplash.com/photo-1551650975-87deedd944c3?w=192&h=192&fit=crop"
  117. mode="aspectFit"
  118. />
  119. <Text className="text-3xl font-bold text-gray-900 mb-2">欢迎使用小程序</Text>
  120. <Text className="text-gray-600 text-lg">请先登录以使用完整功能</Text>
  121. </View>
  122. <View className="bg-white rounded-2xl shadow-lg p-6 mb-6">
  123. <View className="space-y-4">
  124. <Button
  125. className="w-full"
  126. type="primary"
  127. onClick={handleLogin}
  128. >
  129. 立即登录
  130. </Button>
  131. <Button
  132. className="w-full"
  133. plain
  134. onClick={goToRegister}
  135. >
  136. 注册新账号
  137. </Button>
  138. </View>
  139. </View>
  140. <View>
  141. <Text className="text-xl font-bold text-gray-900 mb-4 block">功能特色</Text>
  142. <View className="grid grid-cols-2 gap-4">
  143. <View className="bg-white rounded-xl shadow-lg p-4">
  144. <View className="text-2xl mb-2">🔒</View>
  145. <Text className="font-bold text-gray-900 mb-1 block">安全认证</Text>
  146. <Text className="text-gray-600 text-sm">多重加密保护账户安全</Text>
  147. </View>
  148. <View className="bg-white rounded-xl shadow-lg p-4">
  149. <View className="text-2xl mb-2">👤</View>
  150. <Text className="font-bold text-gray-900 mb-1 block">用户管理</Text>
  151. <Text className="text-gray-600 text-sm">完整的用户信息管理</Text>
  152. </View>
  153. <View className="bg-white rounded-xl shadow-lg p-4">
  154. <View className="text-2xl mb-2">📱</View>
  155. <Text className="font-bold text-gray-900 mb-1 block">响应式设计</Text>
  156. <Text className="text-gray-600 text-sm">完美适配各种设备</Text>
  157. </View>
  158. <View className="bg-white rounded-xl shadow-lg p-4">
  159. <View className="text-2xl mb-2">⚡</View>
  160. <Text className="font-bold text-gray-900 mb-1 block">实时同步</Text>
  161. <Text className="text-gray-600 text-sm">数据实时更新同步</Text>
  162. </View>
  163. </View>
  164. </View>
  165. </View>
  166. )}
  167. </View>
  168. )
  169. }