index.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { View, Input, Button, Text, Image } from '@tarojs/components'
  2. import { useState } from 'react'
  3. import Taro from '@tarojs/taro'
  4. import { authManager } from '../../utils/auth'
  5. import './index.css'
  6. export default function Login() {
  7. const [username, setUsername] = useState('')
  8. const [password, setPassword] = useState('')
  9. const [loading, setLoading] = useState(false)
  10. const handleLogin = async () => {
  11. if (!username || !password) {
  12. Taro.showToast({
  13. title: '请输入用户名和密码',
  14. icon: 'none'
  15. })
  16. return
  17. }
  18. setLoading(true)
  19. try {
  20. const user = await authManager.login(username, password)
  21. Taro.showToast({
  22. title: '登录成功',
  23. icon: 'success'
  24. })
  25. // 登录成功后跳转到首页
  26. setTimeout(() => {
  27. Taro.switchTab({ url: '/pages/index/index' })
  28. }, 1500)
  29. } catch (error: any) {
  30. Taro.showToast({
  31. title: error.message || '登录失败',
  32. icon: 'none'
  33. })
  34. } finally {
  35. setLoading(false)
  36. }
  37. }
  38. const goToRegister = () => {
  39. Taro.navigateTo({ url: '/pages/register/index' })
  40. }
  41. return (
  42. <View className="login-container">
  43. <View className="login-header">
  44. <Image
  45. className="login-logo"
  46. src="https://source.unsplash.com/200x200/?logo"
  47. mode="aspectFit"
  48. />
  49. <View className="login-title-container">
  50. <Text className="login-title">欢迎回来</Text>
  51. <Text className="login-subtitle">请使用您的账号登录</Text>
  52. </View>
  53. </View>
  54. <View className="login-form">
  55. <View className="form-item">
  56. <Text className="form-label">用户名</Text>
  57. <View className="input-wrapper">
  58. <Input
  59. className="form-input"
  60. placeholder="请输入用户名"
  61. value={username}
  62. onInput={(e) => setUsername(e.detail.value)}
  63. maxlength={20}
  64. />
  65. </View>
  66. </View>
  67. <View className="form-item">
  68. <Text className="form-label">密码</Text>
  69. <View className="input-wrapper">
  70. <Input
  71. className="form-input"
  72. placeholder="请输入密码"
  73. password
  74. value={password}
  75. onInput={(e) => setPassword(e.detail.value)}
  76. maxlength={20}
  77. />
  78. </View>
  79. </View>
  80. <Button
  81. className={`login-button ${loading ? 'loading' : ''}`}
  82. loading={loading}
  83. onClick={handleLogin}
  84. disabled={loading}
  85. >
  86. {loading ? '登录中...' : '登录'}
  87. </Button>
  88. <View className="login-footer">
  89. <Text className="footer-text">
  90. 还没有账号?
  91. <Text className="link-text" onClick={goToRegister}>
  92. 立即注册
  93. </Text>
  94. </Text>
  95. </View>
  96. </View>
  97. <View className="login-footer-info">
  98. <Text className="footer-tip">登录即表示您同意我们的</Text>
  99. <View className="footer-links">
  100. <Text className="link-text">用户协议</Text>
  101. <Text className="link-separator">和</Text>
  102. <Text className="link-text">隐私政策</Text>
  103. </View>
  104. </View>
  105. </View>
  106. )
  107. }