This commit is contained in:
lz_db
2026-10-17 09:41:11 +08:00
parent c519bdae9f
commit 28b11b4365
7 changed files with 126 additions and 28 deletions
+13 -4
View File
@@ -1,11 +1,20 @@
import 'package:package_info_plus/package_info_plus.dart';
/// 全局配置常量(域名发现、公共请求头、版本号,对应《01-开发文档》5.5/5.6/5.7)
class AppConfig {
AppConfig._();
/// App 版本(对应 pubspec version 的 name 部分;version_code 对应 build 号)
/// ⚠️ 每次发版必须与 pubspec.yaml 的 version 同步递增,否则老用户会一直提示更新
static const String appVersion = '1.2.0';
static const int appVersionCode = 12;
/// App 版本(运行时从 PackageInfo 读取,始终与 APK 真实 versionName/versionCode 一致;
/// 不要改回硬编码——硬编码一旦与 pubspec 脱节,老用户会无限收到更新提示)
static late final String appVersion;
static late final int appVersionCode;
/// 启动时初始化版本信息(main() 中 runApp 前必须调用)
static Future<void> initVersion() async {
final info = await PackageInfo.fromPlatform();
appVersion = info.version;
appVersionCode = int.tryParse(info.buildNumber) ?? 0;
}
/// 平台标识(运行时按 iOS/Android 判定,见 platformName)
static const String platformNameIOS = 'ios';
+3
View File
@@ -6,11 +6,14 @@ import 'core/storage/local_store.dart';
import 'core/storage/search_history_store.dart';
import 'core/storage/watch_dedup_store.dart';
import 'core/theme/app_theme.dart';
import 'core/config/app_config.dart';
import 'router.dart';
/// App 入口:本地存储 + MediaKit + MaterialApp.router
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// 版本信息(版本检查/请求头 X-App-Version 依赖,必须先初始化)
await AppConfig.initVersion();
// MediaKit(libmpv 底层)全局初始化:每个进程必须且仅调一次
MediaKit.ensureInitialized();
// 本地存储初始化(设备指纹在 splash 阶段生成)
+101 -19
View File
@@ -10,7 +10,7 @@ import '../../core/theme/app_theme.dart';
import '../../providers/auth_provider.dart';
import '../../services/auth_service.dart';
/// 注册页:手机号 + 验证码 + 密码;邀请码选填(口令/模糊归因命中时自动预填)
/// 注册页:手机号/邮箱 + 验证码 + 密码;邀请码选填(口令/模糊归因命中时自动预填)
class RegisterScreen extends ConsumerStatefulWidget {
const RegisterScreen({super.key});
@@ -19,11 +19,16 @@ class RegisterScreen extends ConsumerStatefulWidget {
}
class _RegisterScreenState extends ConsumerState<RegisterScreen> {
final _phone = TextEditingController();
final _account = TextEditingController();
final _code = TextEditingController();
final _password = TextEditingController();
final _nickname = TextEditingController();
late final TextEditingController _inviteCode;
/// 注册方式:phone / email
String _mode = 'phone';
bool get _isEmail => _mode == 'email';
bool _loading = false;
String? _error;
@@ -31,6 +36,9 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
int _countdown = 0;
Timer? _timer;
/// 邮箱格式校验
static final _emailReg = RegExp(r'^[\w.+-]+@[\w-]+\.[\w.-]+$');
@override
void initState() {
super.initState();
@@ -41,7 +49,7 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
@override
void dispose() {
_timer?.cancel();
_phone.dispose();
_account.dispose();
_code.dispose();
_password.dispose();
_nickname.dispose();
@@ -49,20 +57,41 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
super.dispose();
}
void _switchMode(String mode) {
if (mode == _mode) return;
setState(() {
_mode = mode;
_error = null;
// 切换方式后账号和验证码需要重新填写
_account.clear();
_code.clear();
});
}
Future<void> _sendCode() async {
final phone = _phone.text.trim();
if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
setState(() => _error = '请输入正确的手机号');
return;
final account = _account.text.trim();
if (_isEmail) {
if (!_emailReg.hasMatch(account)) {
setState(() => _error = '请输入正确的邮箱地址');
return;
}
} else {
if (!RegExp(r'^1\d{10}$').hasMatch(account)) {
setState(() => _error = '请输入正确的手机号');
return;
}
}
try {
final devCode = await AuthService.sendSmsCode(phone, 'register');
// 未配置短信/邮件通道时服务端走开发模式,直接返回验证码
final devCode = _isEmail
? await AuthService.sendEmailCode(account, 'register')
: await AuthService.sendSmsCode(account, 'register');
setState(() {
_error = null;
_countdown = 60;
});
if (devCode != null && mounted) {
// 短信通道未配置时的开发模式:直接展示验证码(上线配置短信后服务端不再返回)
// 通道未配置时的开发模式:直接展示验证码(上线配置后服务端不再返回)
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('开发模式验证码:$devCode(5 分钟内有效)'), duration: const Duration(seconds: 6)),
);
@@ -84,11 +113,11 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
}
Future<void> _submit() async {
final phone = _phone.text.trim();
final account = _account.text.trim();
final code = _code.text.trim();
final password = _password.text;
if (phone.isEmpty || code.isEmpty || password.isEmpty) {
setState(() => _error = '请填写手机号、验证码和密码');
if (account.isEmpty || code.isEmpty || password.isEmpty) {
setState(() => _error = _isEmail ? '请填写邮箱、验证码和密码' : '请填写手机号、验证码和密码');
return;
}
setState(() {
@@ -97,8 +126,8 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
});
try {
final result = await AuthService.register(
type: 'phone',
account: phone,
type: _mode,
account: account,
code: code,
password: password,
nickname: _nickname.text.trim(),
@@ -132,11 +161,14 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
padding: const EdgeInsets.all(24),
children: [
const Text('创建账号', style: TextStyle(fontSize: 26, fontWeight: FontWeight.w700)),
const SizedBox(height: 32),
const SizedBox(height: 24),
// 手机号 / 邮箱 切换
_ModeSwitch(current: _mode, onChanged: _switchMode),
const SizedBox(height: 24),
TextField(
controller: _phone,
keyboardType: TextInputType.phone,
decoration: const InputDecoration(hintText: '手机号'),
controller: _account,
keyboardType: _isEmail ? TextInputType.emailAddress : TextInputType.phone,
decoration: InputDecoration(hintText: _isEmail ? '邮箱' : '手机号'),
),
const SizedBox(height: 16),
Row(
@@ -145,7 +177,7 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
child: TextField(
controller: _code,
keyboardType: TextInputType.number,
decoration: const InputDecoration(hintText: '短信验证码'),
decoration: InputDecoration(hintText: _isEmail ? '邮箱验证码' : '短信验证码'),
),
),
const SizedBox(width: 12),
@@ -201,3 +233,53 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
);
}
}
/// 注册方式切换条(手机号 / 邮箱)
class _ModeSwitch extends StatelessWidget {
const _ModeSwitch({required this.current, required this.onChanged});
final String current;
final ValueChanged<String> onChanged;
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: AppColors.surface,
borderRadius: BorderRadius.circular(10),
),
padding: const EdgeInsets.all(3),
child: Row(
children: [
_tab(context, '手机号', 'phone'),
_tab(context, '邮箱', 'email'),
],
),
);
}
Widget _tab(BuildContext context, String label, String value) {
final selected = current == value;
return Expanded(
child: GestureDetector(
onTap: () => onChanged(value),
child: Container(
padding: const EdgeInsets.symmetric(vertical: 9),
alignment: Alignment.center,
decoration: BoxDecoration(
color: selected ? Theme.of(context).scaffoldBackgroundColor : Colors.transparent,
borderRadius: BorderRadius.circular(8),
),
child: Text(
label,
style: TextStyle(
fontSize: 14,
fontWeight: selected ? FontWeight.w600 : FontWeight.w400,
color: selected ? AppColors.textPrimary : AppColors.textSecondary,
),
),
),
),
);
}
}
@@ -18,9 +18,9 @@ class SettingsScreen extends ConsumerWidget {
body: ListView(
children: [
// 版本信息
const ListTile(
leading: Icon(Icons.info_outline, color: AppColors.textSecondary),
title: Text('当前版本'),
ListTile(
leading: const Icon(Icons.info_outline, color: AppColors.textSecondary),
title: const Text('当前版本'),
trailing: Text(
'v${AppConfig.appVersion} (${AppConfig.appVersionCode})',
style: TextStyle(color: AppColors.textWeak, fontSize: 13),
+4 -1
View File
@@ -2,7 +2,7 @@ name: video_app
description: "短视频应用(Flutter,iOS/Android 双端)"
publish_to: 'none'
version: 1.2.0+12
version: 1.2.0+13
environment:
sdk: ^3.9.2
@@ -32,6 +32,9 @@ dependencies:
cupertino_icons: ^1.0.8
# 应用包信息(运行时读取真实 versionName/versionCode,修复更新弹窗误判)
package_info_plus: ^8.3.0
# 状态管理(02-技术文档 2.1)
flutter_riverpod: ^2.6.1
# 网络