up
This commit is contained in:
@@ -2,11 +2,12 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../../core/network/api_exception.dart';
|
||||
import '../../core/theme/app_theme.dart';
|
||||
import '../../providers/auth_provider.dart';
|
||||
import '../../services/auth_service.dart';
|
||||
|
||||
/// 登录页:骨架期提供手机号+密码登录,验证码/邮箱登录随迭代补充
|
||||
/// 登录页:账号(手机号/邮箱,含@自动识别)+ 密码登录
|
||||
class LoginScreen extends ConsumerStatefulWidget {
|
||||
const LoginScreen({super.key});
|
||||
|
||||
@@ -21,10 +22,10 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
String? _error;
|
||||
|
||||
Future<void> _submit() async {
|
||||
final phone = _phone.text.trim();
|
||||
final account = _phone.text.trim();
|
||||
final password = _password.text;
|
||||
if (phone.isEmpty || password.isEmpty) {
|
||||
setState(() => _error = '请输入手机号和密码');
|
||||
if (account.isEmpty || password.isEmpty) {
|
||||
setState(() => _error = '请输入账号和密码');
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
@@ -32,15 +33,20 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
_error = null;
|
||||
});
|
||||
try {
|
||||
// 含 @ 视为邮箱登录,否则手机号登录(服务端两种都支持密码登录)
|
||||
final isEmail = account.contains('@');
|
||||
final result = await AuthService.login(
|
||||
type: 'phone_password',
|
||||
phone: phone,
|
||||
type: isEmail ? 'email_password' : 'phone_password',
|
||||
phone: isEmail ? null : account,
|
||||
email: isEmail ? account : null,
|
||||
password: password,
|
||||
);
|
||||
await ref.read(authProvider.notifier).onLoginSuccess(result.user);
|
||||
if (mounted) context.go('/home');
|
||||
} catch (e) {
|
||||
setState(() => _error = e.toString().replaceFirst('ApiException(', '').split(', ').last);
|
||||
} on ApiException catch (e) {
|
||||
setState(() => _error = e.message);
|
||||
} catch (_) {
|
||||
setState(() => _error = '网络异常,请稍后重试');
|
||||
} finally {
|
||||
if (mounted) setState(() => _loading = false);
|
||||
}
|
||||
@@ -62,8 +68,8 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
const SizedBox(height: 32),
|
||||
TextField(
|
||||
controller: _phone,
|
||||
keyboardType: TextInputType.phone,
|
||||
decoration: const InputDecoration(hintText: '手机号'),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: const InputDecoration(hintText: '手机号 / 邮箱'),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
|
||||
Reference in New Issue
Block a user