index.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { View, Text, Button, Image } from '@tarojs/components'
  2. import { useDidShow } from '@tarojs/taro'
  3. import { useState } from 'react'
  4. import Taro from '@tarojs/taro'
  5. import './index.css'
  6. import { useAuth } from '@/utils/auth'
  7. export default function Index() {
  8. const { user, isLoading, logout } = useAuth()
  9. const [logoutLoading, setLogoutLoading] = useState(false)
  10. useDidShow(() => {
  11. // 用户状态由useAuth自动管理,无需手动检查
  12. console.log('页面显示,当前用户状态:', user ? '已登录' : '未登录')
  13. })
  14. const handleLogin = () => {
  15. Taro.navigateTo({ url: '/pages/login/index' })
  16. }
  17. const handleLogout = async () => {
  18. setLogoutLoading(true)
  19. try {
  20. await logout()
  21. // 退出成功后,useAuth会自动更新user状态
  22. Taro.showToast({
  23. title: '退出成功',
  24. icon: 'success'
  25. })
  26. } catch (error) {
  27. console.error('Logout error:', error)
  28. } finally {
  29. setLogoutLoading(false)
  30. }
  31. }
  32. const handleProfile = () => {
  33. Taro.navigateTo({ url: '/pages/profile/index' })
  34. }
  35. const goToRegister = () => {
  36. Taro.navigateTo({ url: '/pages/register/index' })
  37. }
  38. // 显示加载状态
  39. if (isLoading) {
  40. return (
  41. <View className="min-h-screen flex items-center justify-center bg-gradient-to-br from-blue-50 to-indigo-100">
  42. <View className="text-center">
  43. <View className="w-12 h-12 border-4 border-blue-200 border-t-blue-600 rounded-full animate-spin mx-auto mb-4"></View>
  44. <Text className="text-gray-600">加载中...</Text>
  45. </View>
  46. </View>
  47. )
  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. />
  60. </View>
  61. <View>
  62. <Text className="text-gray-600 text-sm">欢迎回来</Text>
  63. <Text className="text-2xl font-bold text-gray-900">{user.username}</Text>
  64. </View>
  65. </View>
  66. <View className="space-y-4 mb-6">
  67. <View className="flex justify-between items-center py-3 border-b border-gray-100">
  68. <Text className="text-gray-600">用户ID</Text>
  69. <Text className="text-gray-900 font-mono">{user.id}</Text>
  70. </View>
  71. {user.email && (
  72. <View className="flex justify-between items-center py-3 border-b border-gray-100">
  73. <Text className="text-gray-600">邮箱</Text>
  74. <Text className="text-gray-900">{user.email}</Text>
  75. </View>
  76. )}
  77. <View className="flex justify-between items-center py-3">
  78. <Text className="text-gray-600">注册时间</Text>
  79. <Text className="text-gray-900">{new Date(user.createdAt).toLocaleDateString()}</Text>
  80. </View>
  81. </View>
  82. <View className="flex space-x-3">
  83. <Button className="flex-1" type="primary" onClick={handleProfile}>
  84. 查看资料
  85. </Button>
  86. <Button
  87. className="flex-1"
  88. loading={logoutLoading}
  89. onClick={handleLogout}
  90. >
  91. 退出登录
  92. </Button>
  93. </View>
  94. </View>
  95. <View className="grid grid-cols-3 gap-4">
  96. <View className="bg-white rounded-xl shadow-lg p-4 text-center">
  97. <Text className="text-3xl font-bold text-blue-600 mb-1">1</Text>
  98. <Text className="text-gray-600 text-sm">今日访问</Text>
  99. </View>
  100. <View className="bg-white rounded-xl shadow-lg p-4 text-center">
  101. <Text className="text-3xl font-bold text-green-600 mb-1">7</Text>
  102. <Text className="text-gray-600 text-sm">本周活跃</Text>
  103. </View>
  104. <View className="bg-white rounded-xl shadow-lg p-4 text-center">
  105. <Text className="text-3xl font-bold text-purple-600 mb-1">30</Text>
  106. <Text className="text-gray-600 text-sm">本月使用</Text>
  107. </View>
  108. </View>
  109. </View>
  110. ) : (
  111. <View className="p-6">
  112. <View className="flex flex-col items-center mb-8">
  113. <Image
  114. className="w-32 h-32 rounded-full mx-auto mb-4"
  115. src="https://images.unsplash.com/photo-1551650975-87deedd944c3?w=192&h=192&fit=crop"
  116. />
  117. <Text className="text-3xl font-bold text-gray-900 mb-2">欢迎使用小程序444</Text>
  118. <Text className="text-gray-600 text-lg">请先登录以使用完整功能</Text>
  119. </View>
  120. <View className="bg-white rounded-2xl shadow-lg p-6 mb-6">
  121. <View className="space-y-4">
  122. <Button
  123. className="w-full"
  124. type="primary"
  125. onClick={handleLogin}
  126. >
  127. 立即登录
  128. </Button>
  129. <Button
  130. className="w-full"
  131. plain
  132. onClick={goToRegister}
  133. >
  134. 注册新账号
  135. </Button>
  136. </View>
  137. </View>
  138. <View>
  139. <Text className="text-xl font-bold text-gray-900 mb-4 block">功能特色</Text>
  140. <View className="grid grid-cols-2 gap-4">
  141. <View className="bg-white rounded-xl shadow-lg p-4">
  142. <View className="text-2xl mb-2">
  143. <View className="i-heroicons-lock-closed-16-solid" />
  144. </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. }