feat(app): 完成 P0 钱包解锁、下载落盘与手机号登录

This commit is contained in:
Zhou Weidong committed 2026-10-19 23:46:47 +08:00
1 parent de941c841c
commit 136ee74618
47 files changed
+3264 -70

No files matched your search

+52
View File
@@ -165,4 +165,56 @@ void main() {
expect(auth.isSignedIn, isFalse);
expect(await AuthSessionStore().read(), isNull);
});
test('should_store_session_when_phone_code_login_succeeds', () async {
late AuthController auth;
final adapter = FakeHttpAdapter((options) async {
if (options.uri.path == '/app/auth/otp') {
final body = (options.data as Map).cast<String, dynamic>();
expect(body['targetType'], 'PHONE');
expect(body['target'], '08123456789');
expect(body['scene'], 'LOGIN');
expect(body['deviceId'], isNotEmpty);
return FakeHttpResponse(
jsonEncode({'code': 0, 'message': 'ok', 'data': null}),
);
}
expect(options.uri.path, '/app/auth/login');
final body = (options.data as Map).cast<String, dynamic>();
expect(body['targetType'], 'PHONE');
expect(body['code'], '123456');
return FakeHttpResponse(tokenBody(accessToken: 'p1', refreshToken: 'pr1'));
});
final store = AuthSessionStore();
auth = AuthController(
AuthApi(clientWith(() => auth, adapter: adapter)),
store,
);
await auth.sendOtp(target: '08123456789', scene: 'LOGIN');
await auth.signInWithPhone(target: '08123456789', code: '123456');
expect(auth.isSignedIn, isTrue);
expect(auth.accessToken, 'p1');
});
test('should_store_session_when_phone_registration_succeeds', () async {
late AuthController auth;
final adapter = FakeHttpAdapter((options) async {
expect(options.uri.path, '/app/auth/register');
final body = (options.data as Map).cast<String, dynamic>();
expect(body['targetType'], 'PHONE');
expect(body['code'], '654321');
return FakeHttpResponse(tokenBody(accessToken: 'p2', refreshToken: 'pr2'));
});
auth = AuthController(
AuthApi(clientWith(() => auth, adapter: adapter)),
AuthSessionStore(),
);
await auth.registerWithPhone(target: '08123456789', code: '654321');
expect(auth.isSignedIn, isTrue);
expect(auth.user?.uid, 'U1');
});
}
+7 -4
View File
@@ -18,7 +18,7 @@ import '../support/fake_haptics.dart';
import '../support/fake_http_adapter.dart';
void main() {
testWidgets('should_show_three_login_entries_and_open_email_form', (
testWidgets('should_show_real_login_entries_and_open_phone_sheet', (
tester,
) async {
SharedPreferencesAsyncPlatform.instance =
@@ -48,14 +48,17 @@ void main() {
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('auth-entry-google')), findsOneWidget);
expect(find.byKey(const Key('auth-entry-google')), findsNothing);
expect(find.byKey(const Key('auth-entry-phone')), findsOneWidget);
expect(find.byKey(const Key('auth-entry-email')), findsOneWidget);
expect(find.byKey(const Key('auth-legal')), findsOneWidget);
await tester.tap(find.byKey(const Key('auth-entry-google')));
await tester.tap(find.byKey(const Key('auth-entry-phone')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('phone-auth-sheet')), findsOneWidget);
await tester.tapAt(const Offset(8, 8));
await tester.pumpAndSettle();
expect(find.text('Coming soon'), findsOneWidget);
await tester.tap(find.byKey(const Key('auth-entry-email')));
await tester.pumpAndSettle();
+1 -1
View File
@@ -80,7 +80,7 @@ void main() {
tester.getSize(find.byKey(const Key('auth-brand-logo'))),
const Size(64, 64),
);
expect(find.byKey(const Key('auth-entry-google')), findsOneWidget);
expect(find.byKey(const Key('auth-entry-google')), findsNothing);
expect(find.byKey(const Key('auth-entry-phone')), findsOneWidget);
expect(find.byKey(const Key('auth-entry-email')), findsOneWidget);
expect(find.byKey(const Key('auth-legal')), findsOneWidget);
+127
View File
@@ -0,0 +1,127 @@
// Author: fruit.com.cn
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fruitshorts/core/data/remote/api_client.dart';
import 'package:fruitshorts/core/data/remote/auth_api.dart';
import 'package:fruitshorts/core/data/services/auth_controller.dart';
import 'package:fruitshorts/core/data/services/auth_session_store.dart';
import 'package:fruitshorts/core/platform/haptics.dart';
import 'package:fruitshorts/l10n/app_localizations.dart';
import 'package:fruitshorts/pages/auth/phone_auth_sheet.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
import '../support/fake_haptics.dart';
import '../support/fake_http_adapter.dart';
void main() {
setUp(() {
SharedPreferencesAsyncPlatform.instance =
InMemorySharedPreferencesAsync.empty();
});
testWidgets('should_send_otp_and_sign_in_when_phone_form_is_submitted', (
tester,
) async {
final adapter = FakeHttpAdapter((options) async {
if (options.uri.path == '/app/auth/otp') {
return FakeHttpResponse(
jsonEncode({'code': 0, 'message': 'ok', 'data': null}),
);
}
expect(options.uri.path, '/app/auth/login');
return FakeHttpResponse(
jsonEncode({
'code': 0,
'message': 'ok',
'data': {
'accessToken': 'phone-token',
'refreshToken': 'phone-refresh',
'expiresIn': 7200,
'user': {'uid': 'U9', 'nickname': 'Phone User'},
},
}),
);
});
final auth = AuthController(
AuthApi(ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter)),
AuthSessionStore(),
);
await _pumpHost(tester, auth);
await tester.tap(find.byKey(const Key('open-phone-sheet')));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('auth-phone-number')),
'08123456789',
);
await tester.tap(find.byKey(const Key('auth-send-code')));
await tester.pumpAndSettle();
expect(
adapter.requests.where((item) => item.uri.path == '/app/auth/otp'),
hasLength(1),
);
await tester.enterText(find.byKey(const Key('auth-phone-code')), '123456');
await tester.tap(find.byKey(const Key('auth-phone-submit')));
await tester.pumpAndSettle();
expect(auth.isSignedIn, isTrue);
expect(auth.accessToken, 'phone-token');
});
testWidgets('should_keep_sheet_open_when_phone_number_is_invalid', (
tester,
) async {
final adapter = FakeHttpAdapter(
(_) async => FakeHttpResponse(
jsonEncode({'code': 0, 'message': 'ok', 'data': null}),
),
);
final auth = AuthController(
AuthApi(ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter)),
AuthSessionStore(),
);
await _pumpHost(tester, auth);
await tester.tap(find.byKey(const Key('open-phone-sheet')));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(const Key('auth-phone-number')), '12');
await tester.tap(find.byKey(const Key('auth-send-code')));
await tester.pumpAndSettle();
expect(find.text('Enter a valid phone number'), findsOneWidget);
expect(adapter.requests, isEmpty);
});
}
Future<void> _pumpHost(WidgetTester tester, AuthController auth) {
return tester.pumpWidget(
MultiProvider(
providers: [
ChangeNotifierProvider<AuthController>.value(value: auth),
Provider<Haptics>.value(value: FakeHaptics()),
],
child: MaterialApp(
locale: const Locale('en'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Builder(
builder: (context) => Scaffold(
body: Center(
child: TextButton(
key: const Key('open-phone-sheet'),
onPressed: () => showPhoneAuthSheet(context),
child: const Text('Open'),
),
),
),
),
),
),
);
}