index.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { View, Input, Button, Text } 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. <Text className="login-title">欢迎登录</Text>
  45. <Text className="login-subtitle">请使用您的账号登录</Text>
  46. </View>
  47. <View className="login-form">
  48. <View className="form-item">
  49. <Text className="form-label">用户名</Text>
  50. <Input
  51. className="form-input"
  52. placeholder="请输入用户名"
  53. value={username}
  54. onInput={(e) => setUsername(e.detail.value)}
  55. />
  56. </View>
  57. <View className="form-item">
  58. <Text className="form-label">密码</Text>
  59. <Input
  60. className="form-input"
  61. placeholder="请输入密码"
  62. password
  63. value={password}
  64. onInput={(e) => setPassword(e.detail.value)}
  65. />
  66. </View>
  67. <Button
  68. className="login-button"
  69. loading={loading}
  70. onClick={handleLogin}
  71. >
  72. 登录222
  73. </Button>
  74. <View className="login-footer">
  75. <Text className="register-link" onClick={goToRegister}>
  76. 还没有账号?立即注册
  77. </Text>
  78. </View>
  79. </View>
  80. </View>
  81. )
  82. }