|
|
@@ -1,16 +1,16 @@
|
|
|
import { View, Input, Button, Text, Image } from '@tarojs/components'
|
|
|
import { useState, useEffect } from 'react'
|
|
|
import Taro from '@tarojs/taro'
|
|
|
-import { authManager } from '../../utils/auth'
|
|
|
import './index.css'
|
|
|
+import { useAuth } from '@/utils/auth'
|
|
|
|
|
|
export default function Login() {
|
|
|
const [username, setUsername] = useState('')
|
|
|
const [password, setPassword] = useState('')
|
|
|
- const [loading, setLoading] = useState(false)
|
|
|
const [showPassword, setShowPassword] = useState(false)
|
|
|
+ const { login, isLoading } = useAuth() // 使用 useAuth 获取登录方法和加载状态
|
|
|
|
|
|
- // 页面加载动画效果
|
|
|
+ // 设置导航栏标题
|
|
|
useEffect(() => {
|
|
|
Taro.setNavigationBarTitle({
|
|
|
title: '用户登录'
|
|
|
@@ -54,17 +54,19 @@ export default function Login() {
|
|
|
})
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
- setLoading(true)
|
|
|
|
|
|
try {
|
|
|
- // 添加加载动画
|
|
|
+ // 显示加载动画
|
|
|
Taro.showLoading({
|
|
|
title: '登录中...',
|
|
|
mask: true
|
|
|
})
|
|
|
|
|
|
- const user = await authManager.login(username.trim(), password.trim())
|
|
|
+ // 使用 useAuth 提供的 login 方法,传递符合 LoginRequest 类型的参数
|
|
|
+ await login({
|
|
|
+ username: username.trim(),
|
|
|
+ password: password.trim()
|
|
|
+ })
|
|
|
|
|
|
Taro.hideLoading()
|
|
|
|
|
|
@@ -108,8 +110,6 @@ export default function Login() {
|
|
|
duration: 3000
|
|
|
})
|
|
|
}
|
|
|
- } finally {
|
|
|
- setLoading(false)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -117,7 +117,6 @@ export default function Login() {
|
|
|
Taro.navigateTo({
|
|
|
url: '/pages/register/index',
|
|
|
success: () => {
|
|
|
- // 页面跳转成功后的回调
|
|
|
console.log('跳转到注册页面')
|
|
|
},
|
|
|
fail: (error) => {
|
|
|
@@ -147,7 +146,7 @@ export default function Login() {
|
|
|
|
|
|
// 处理键盘完成事件
|
|
|
const handleInputConfirm = () => {
|
|
|
- if (username && password && !loading) {
|
|
|
+ if (username && password && !isLoading) {
|
|
|
handleLogin()
|
|
|
}
|
|
|
}
|
|
|
@@ -204,12 +203,12 @@ export default function Login() {
|
|
|
</View>
|
|
|
|
|
|
<Button
|
|
|
- className={`login-button ${loading ? 'loading' : ''}`}
|
|
|
- loading={loading}
|
|
|
+ className={`login-button ${isLoading ? 'loading' : ''}`}
|
|
|
+ loading={isLoading}
|
|
|
onClick={handleLogin}
|
|
|
- disabled={loading || !username || !password}
|
|
|
+ disabled={isLoading || !username || !password}
|
|
|
>
|
|
|
- {loading ? '登录中...' : '安全登录'}
|
|
|
+ {isLoading ? '登录中...' : '安全登录'}
|
|
|
</Button>
|
|
|
|
|
|
<View className="login-footer">
|
|
|
@@ -232,4 +231,5 @@ export default function Login() {
|
|
|
</View>
|
|
|
</View>
|
|
|
)
|
|
|
-}
|
|
|
+}
|
|
|
+
|