完善移动端错误提示中文化
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
// 功能描述:提供工作人员端手机号密码登录界面及本地输入校验。
|
||||
// 版本:1.1.0
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../app/dependencies.dart';
|
||||
import '../../../data/services/api_client.dart';
|
||||
|
||||
/// 工作人员端手机号密码登录页面。
|
||||
class LoginPage extends StatefulWidget {
|
||||
const LoginPage({required this.session, super.key});
|
||||
|
||||
@@ -11,22 +15,41 @@ class LoginPage extends StatefulWidget {
|
||||
State<LoginPage> createState() => _LoginPageState();
|
||||
}
|
||||
|
||||
/// 管理登录输入、提交状态和错误提示。
|
||||
class _LoginPageState extends State<LoginPage> {
|
||||
static final RegExp _phonePattern = RegExp(r'^1[3-9]\d{9}$');
|
||||
|
||||
final _phone = TextEditingController();
|
||||
final _password = TextEditingController();
|
||||
bool _busy = false;
|
||||
String? _error;
|
||||
String? _phoneError;
|
||||
bool _obscurePassword = true;
|
||||
|
||||
/// 校验手机号并提交登录请求。
|
||||
Future<void> _login() async {
|
||||
final phone = _phone.text.trim();
|
||||
if (!_phonePattern.hasMatch(phone)) {
|
||||
setState(() {
|
||||
_phoneError = '请输入正确的11位手机号';
|
||||
_error = null;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_busy = true;
|
||||
_phoneError = null;
|
||||
_error = null;
|
||||
});
|
||||
try {
|
||||
await widget.session.login(_phone.text.trim(), _password.text);
|
||||
await widget.session.login(phone, _password.text);
|
||||
} catch (error) {
|
||||
if (mounted) setState(() => _error = error.toString());
|
||||
// 登录接口使用统一密码错误码,页面不区分账号是否存在,避免泄露账号状态。
|
||||
final message = error is ApiException
|
||||
? (error.code == 1108 ? '手机号或密码错误' : error.toString())
|
||||
: '登录失败,请稍后重试';
|
||||
if (mounted) setState(() => _error = message);
|
||||
} finally {
|
||||
if (mounted) setState(() => _busy = false);
|
||||
}
|
||||
@@ -84,9 +107,13 @@ class _LoginPageState extends State<LoginPage> {
|
||||
TextField(
|
||||
controller: _phone,
|
||||
keyboardType: TextInputType.phone,
|
||||
decoration: const InputDecoration(
|
||||
onChanged: (_) {
|
||||
if (_phoneError != null) setState(() => _phoneError = null);
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText: '工作人员手机号',
|
||||
prefixIcon: Icon(Icons.phone_outlined),
|
||||
prefixIcon: const Icon(Icons.phone_outlined),
|
||||
errorText: _phoneError,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
|
||||
Reference in New Issue
Block a user