feat(login): 二级密码登录支持(STATUS_SECONDARY_PASSCODE_ERROR #1)
Some checks failed
CI / Lint (push) Has been cancelled

## 问题
旧版 Flutter 项目在 /vcode/check 返回 30164 时展示二级密码输入界面;
新版完全缺失此路径,有二级密码的账号无法登录。

## 改动

### networks_sdk
- `networks_sdk_method_channel_datasource.dart`:executeRequest 的
  generic catch 改为 rethrow,允许 decodeResponse override 抛出
  自定义业务异常(原为 ApiError.unknown 包裹导致数据丢失)

### 数据层
- `errors.dart`:新增 `secondaryPasscodeRequired = 30164`
- `exceptions.dart`(新增):`SecondaryPasscodeRequiredException`
  携带 vcodeToken / recoveryEmail / hint / resetStatus
- `verify_otp_request.dart`:override decodeResponse,拦截 30164,
  从响应 data 提取字段,throw SecondaryPasscodeRequiredException
- `login_request.dart`:新增可选 password 字段 + toJson override
  (条件序列化,null 时不带 password 字段)
- `auth_repository.dart`:新增 loginWithPasscode() 接口
- `auth_repository_impl.dart`:实现 loginWithPasscode()

### 业务层
- `login_usecase.dart`:新增 loginWithSecondaryPasscode()
  (MD5 哈希 passcode → 调 AuthRepository.loginWithPasscode)
- `pubspec.yaml`:新增 crypto: ^3.0.6(用于 MD5)

### UI 层
- `login_state.dart`:新增 LoginStep.secondaryPasscode
  + vcodeToken / passcodeHint / recoveryEmail 字段
- `login_view_model.dart`:verifyAndLogin 捕获 SecondaryPasscodeRequiredException
  跳转步骤 3;新增 verifyPasscode()
- `login_secondary_passcode_step.dart`(新增):密码输入 UI(hint 显示、
  obscured 输入框、错误提示、忘记密码占位)
- `login_page.dart`:switch 路由接入 LoginStep.secondaryPasscode
This commit is contained in:
pp-bot
2026-03-31 15:36:54 +09:00
parent 0995a4bf79
commit b8f1f82ee5
13 changed files with 438 additions and 15 deletions

View File

@@ -5,6 +5,7 @@ import 'package:im_app/features/login/presentation/login_state.dart';
import 'package:im_app/features/login/presentation/login_view_model.dart';
import 'package:im_app/features/login/view/widgets/login_otp_step.dart';
import 'package:im_app/features/login/view/widgets/login_phone_step.dart';
import 'package:im_app/features/login/view/widgets/login_secondary_passcode_step.dart';
/// 登录页 — 两步流程:手机号 → 验证码
///
@@ -24,11 +25,13 @@ class _LoginPageState extends ConsumerState<LoginPage> {
// demo 预填,上线前去掉
final _phoneCtrl = TextEditingController(text: '83465308');
final _otpCtrl = TextEditingController(text: '0000');
final _passcodeCtrl = TextEditingController();
@override
void dispose() {
_phoneCtrl.dispose();
_otpCtrl.dispose();
_passcodeCtrl.dispose();
super.dispose();
}
@@ -44,8 +47,15 @@ class _LoginPageState extends ConsumerState<LoginPage> {
.verifyAndLogin(_otpCtrl.text.trim());
}
void _verifyPasscode() {
ref
.read(loginViewModelProvider.notifier)
.verifyPasscode(_passcodeCtrl.text);
}
void _backToPhone() {
_otpCtrl.clear();
_passcodeCtrl.clear();
ref.read(loginViewModelProvider.notifier).backToPhone();
}
@@ -53,22 +63,35 @@ class _LoginPageState extends ConsumerState<LoginPage> {
Widget build(BuildContext context) {
final state = ref.watch(loginViewModelProvider);
Widget body;
switch (state.step) {
case LoginStep.phone:
body = LoginPhoneStep(
phoneCtrl: _phoneCtrl,
state: state,
onSendOtp: () => _sendOtp(state),
);
case LoginStep.otp:
body = LoginOtpStep(
otpCtrl: _otpCtrl,
state: state,
onVerifyAndLogin: _verifyAndLogin,
onBackToPhone: _backToPhone,
);
case LoginStep.secondaryPasscode:
body = LoginSecondaryPasscodeStep(
passcodeCtrl: _passcodeCtrl,
state: state,
onVerifyPasscode: _verifyPasscode,
onBackToPhone: _backToPhone,
);
}
return Scaffold(
appBar: AppBar(automaticallyImplyLeading: false, title: const Text('登录')),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 32),
child: state.step == LoginStep.phone
? LoginPhoneStep(
phoneCtrl: _phoneCtrl,
state: state,
onSendOtp: () => _sendOtp(state),
)
: LoginOtpStep(
otpCtrl: _otpCtrl,
state: state,
onVerifyAndLogin: _verifyAndLogin,
onBackToPhone: _backToPhone,
),
child: SingleChildScrollView(child: body),
),
);
}

View File

@@ -0,0 +1,158 @@
import 'package:flutter/material.dart';
import 'package:im_app/features/login/presentation/login_state.dart';
/// 步骤 3二级密码输入界面
///
/// 当 OTP 验证后服务端返回 30164 时触发,显示:
/// - 脱敏手机号(参考 otp 步骤)
/// - 密码提示语(用户设置的 hint可为空
/// - 密码输入框obscured
/// - 确认按钮
/// - "忘记密码" 占位TODO: 接入找回流程)
///
/// 对齐旧版 Flutter `secondaryPasscodeLoginVerifyView`
class LoginSecondaryPasscodeStep extends StatelessWidget {
const LoginSecondaryPasscodeStep({
super.key,
required this.passcodeCtrl,
required this.state,
required this.onVerifyPasscode,
required this.onBackToPhone,
});
final TextEditingController passcodeCtrl;
final LoginState state;
final VoidCallback onVerifyPasscode;
final VoidCallback onBackToPhone;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(height: 48),
// 标题
Text(
'请输入二级密码',
style: theme.textTheme.headlineSmall?.copyWith(
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
// 脱敏手机号
Text(
state.maskedContact,
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.onSurface.withAlpha(153),
),
textAlign: TextAlign.center,
),
const SizedBox(height: 32),
// 密码提示语hint 非空时显示)
if (state.passcodeHint.isNotEmpty) ...[
Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
decoration: BoxDecoration(
color: theme.colorScheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [
Icon(
Icons.info_outline,
size: 16,
color: theme.colorScheme.onSurface.withAlpha(153),
),
const SizedBox(width: 8),
Expanded(
child: Text(
'提示:${state.passcodeHint}',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurface.withAlpha(153),
),
),
),
],
),
),
const SizedBox(height: 16),
],
// 密码输入框
TextField(
controller: passcodeCtrl,
obscureText: true,
keyboardType: TextInputType.visiblePassword,
textInputAction: TextInputAction.done,
onSubmitted: (_) => onVerifyPasscode(),
decoration: const InputDecoration(
labelText: '二级密码',
hintText: '请输入您的二级密码',
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.lock_outline),
),
),
const SizedBox(height: 8),
// 错误提示
if (state.error != null)
Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Text(
state.error!,
style: TextStyle(
color: theme.colorScheme.error,
fontSize: 13,
),
textAlign: TextAlign.center,
),
),
const SizedBox(height: 16),
// 确认按钮
FilledButton(
onPressed: state.isLoading ? null : onVerifyPasscode,
child: state.isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('确认'),
),
const SizedBox(height: 12),
// 忘记密码TODO: 接入找回流程)
TextButton(
onPressed: null, // TODO: 跳转找回密码页面
child: Text(
'忘记密码?',
style: TextStyle(
color: theme.colorScheme.primary.withAlpha(128),
),
),
),
const SizedBox(height: 8),
// 返回
TextButton(
onPressed: onBackToPhone,
child: const Text('返回手机号'),
),
],
);
}
}