迁移 Flutter 端与训练脚本,模型/训练产物移出 git(遵循纯代码约定)
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'auth_view_model.dart';
|
||||
|
||||
/// 登录/注册页:手机号 + 密码;注册成功后自动登录。
|
||||
class AuthScreen extends StatefulWidget {
|
||||
const AuthScreen({super.key});
|
||||
|
||||
@override
|
||||
State<AuthScreen> createState() => _AuthScreenState();
|
||||
}
|
||||
|
||||
class _AuthScreenState extends State<AuthScreen> {
|
||||
final _phoneCtrl = TextEditingController();
|
||||
final _passwordCtrl = TextEditingController();
|
||||
bool _obscure = true;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_phoneCtrl.dispose();
|
||||
_passwordCtrl.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
static final _phoneRe = RegExp(r'^1[3-9]\d{9}$');
|
||||
|
||||
Future<void> _submit() async {
|
||||
final vm = context.read<AuthViewModel>();
|
||||
final phone = _phoneCtrl.text.trim();
|
||||
final password = _passwordCtrl.text;
|
||||
if (!_phoneRe.hasMatch(phone)) {
|
||||
vm.showError('请输入正确的 11 位手机号');
|
||||
return;
|
||||
}
|
||||
if (password.length < 6) {
|
||||
vm.showError('密码至少 6 位');
|
||||
return;
|
||||
}
|
||||
final ok = await vm.submit(phone: phone, password: password);
|
||||
if (ok && mounted) {
|
||||
Navigator.of(context).pushReplacementNamed('/home');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final vm = context.watch<AuthViewModel>();
|
||||
final isLogin = vm.mode == AuthMode.login;
|
||||
|
||||
return Scaffold(
|
||||
body: SafeArea(
|
||||
child: Center(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 32),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Icon(Icons.pets, size: 64, color: Colors.green),
|
||||
const SizedBox(height: 12),
|
||||
const Text(
|
||||
'视野',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 28, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'野生动物实时识别',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(color: Colors.grey.shade600),
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
TextField(
|
||||
controller: _phoneCtrl,
|
||||
keyboardType: TextInputType.phone,
|
||||
maxLength: 11,
|
||||
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
|
||||
decoration: const InputDecoration(
|
||||
labelText: '手机号',
|
||||
prefixIcon: Icon(Icons.phone_android),
|
||||
border: OutlineInputBorder(),
|
||||
counterText: '',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: _passwordCtrl,
|
||||
obscureText: _obscure,
|
||||
maxLength: 64,
|
||||
decoration: InputDecoration(
|
||||
labelText: '密码',
|
||||
prefixIcon: const Icon(Icons.lock_outline),
|
||||
border: const OutlineInputBorder(),
|
||||
counterText: '',
|
||||
suffixIcon: IconButton(
|
||||
icon: Icon(_obscure ? Icons.visibility_off : Icons.visibility),
|
||||
onPressed: () => setState(() => _obscure = !_obscure),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
FilledButton(
|
||||
onPressed: vm.loading ? null : _submit,
|
||||
style: FilledButton.styleFrom(
|
||||
minimumSize: const Size.fromHeight(48),
|
||||
),
|
||||
child: vm.loading
|
||||
? const SizedBox(
|
||||
width: 22,
|
||||
height: 22,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: Text(isLogin ? '登录' : '注册并登录'),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextButton(
|
||||
onPressed: vm.loading ? null : vm.switchMode,
|
||||
child: Text(isLogin ? '没有账号?去注册' : '已有账号?去登录'),
|
||||
),
|
||||
if (vm.error != null) ...[
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
vm.error!,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(color: Colors.red),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../payment/order_api.dart';
|
||||
import 'session_store.dart';
|
||||
|
||||
enum AuthMode { login, register }
|
||||
|
||||
class AuthViewModel extends ChangeNotifier {
|
||||
final OrderApi orderApi;
|
||||
final SessionStore sessionStore;
|
||||
|
||||
AuthMode _mode = AuthMode.login;
|
||||
bool _loading = false;
|
||||
String? _error;
|
||||
|
||||
AuthMode get mode => _mode;
|
||||
bool get loading => _loading;
|
||||
String? get error => _error;
|
||||
|
||||
AuthViewModel({required this.orderApi, required this.sessionStore});
|
||||
|
||||
void switchMode() {
|
||||
_mode = _mode == AuthMode.login ? AuthMode.register : AuthMode.login;
|
||||
_error = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 本地输入校验失败提示(不进网络请求)
|
||||
void showError(String message) {
|
||||
_error = message;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 登录或注册;成功返回 true(调用方负责跳转主界面)
|
||||
Future<bool> submit({required String phone, required String password}) async {
|
||||
_loading = true;
|
||||
_error = null;
|
||||
notifyListeners();
|
||||
try {
|
||||
if (_mode == AuthMode.login) {
|
||||
final token = await orderApi.login(phone: phone, password: password);
|
||||
await sessionStore.save(phone, token);
|
||||
} else {
|
||||
await orderApi.register(phone: phone, password: password);
|
||||
final token = await orderApi.login(phone: phone, password: password);
|
||||
await sessionStore.save(phone, token);
|
||||
}
|
||||
return true;
|
||||
} on OrderApiException catch (e) {
|
||||
_error = e.message;
|
||||
return false;
|
||||
} finally {
|
||||
_loading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
/// 登录会话持久化:token + 手机号存 secure storage。
|
||||
/// 启动时读取判断是否已登录;登出/401 时清除回登录页。
|
||||
class SessionStore {
|
||||
static const _storage = FlutterSecureStorage();
|
||||
static const _tokenKey = 'auth_token';
|
||||
static const _phoneKey = 'auth_phone';
|
||||
|
||||
static String? _cachedToken;
|
||||
static String? _cachedPhone;
|
||||
|
||||
Future<String?> readToken() async {
|
||||
if (_cachedToken != null) return _cachedToken;
|
||||
return _cachedToken = await _storage.read(key: _tokenKey);
|
||||
}
|
||||
|
||||
Future<String?> readPhone() async {
|
||||
if (_cachedPhone != null) return _cachedPhone;
|
||||
return _cachedPhone = await _storage.read(key: _phoneKey);
|
||||
}
|
||||
|
||||
Future<void> save(String phone, String token) async {
|
||||
_cachedPhone = phone;
|
||||
_cachedToken = token;
|
||||
await _storage.write(key: _phoneKey, value: phone);
|
||||
await _storage.write(key: _tokenKey, value: token);
|
||||
}
|
||||
|
||||
Future<void> clear() async {
|
||||
_cachedPhone = null;
|
||||
_cachedToken = null;
|
||||
await _storage.delete(key: _phoneKey);
|
||||
await _storage.delete(key: _tokenKey);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user