feat: add Flutter mobile clients and staff delivery API

This commit is contained in:
david
2026-07-30 21:47:41 +08:00
parent 550efb3812
commit 36a5ced1c0
228 changed files with 17159 additions and 22 deletions

View File

@@ -0,0 +1,26 @@
import 'package:flutter/material.dart';
import '../ui/core/app_theme.dart';
import 'dependencies.dart';
import 'router.dart';
class UserClientApp extends StatefulWidget {
const UserClientApp({required this.dependencies, super.key});
final AppDependencies dependencies;
@override
State<UserClientApp> createState() => _UserClientAppState();
}
class _UserClientAppState extends State<UserClientApp> {
late final _router = createRouter(widget.dependencies);
@override
Widget build(BuildContext context) => MaterialApp.router(
title: '瓶安芯',
debugShowCheckedModeBanner: false,
theme: AppTheme.light(),
routerConfig: _router,
);
}

View File

@@ -0,0 +1,83 @@
import 'package:flutter/foundation.dart';
import '../data/repositories/client_repository.dart';
import '../data/services/api_client.dart';
import '../data/services/secure_session_store.dart';
class AppDependencies {
AppDependencies._({
required this.session,
required this.repository,
});
final UserSession session;
final ClientRepository repository;
static Future<AppDependencies> create() async {
final store = SecureSessionStore();
final session = UserSession(store);
await session.restore();
final api = ApiClient(() => session.token);
return AppDependencies._(session: session, repository: ClientRepository(api));
}
}
class UserSession extends ChangeNotifier {
UserSession(this._store);
static const _root = '/heqi/client/v1/user';
final SecureSessionStore _store;
String _token = '';
String get token => _token;
bool get isAuthenticated => _token.isNotEmpty;
Future<void> restore() async {
_token = await _store.readToken() ?? '';
}
Future<void> login({
required String phone,
required String password,
String? verificationCode,
String? requestIdentity,
}) async {
final api = ApiClient(() => '');
final verification = verificationCode != null && verificationCode.isNotEmpty;
final details = jsonMap(
await api.post(
'$_root/auth/login',
authenticated: false,
body: {
'phone': phone,
'mode': verification ? 'verification_code' : 'password',
'password': verification ? '' : password,
'code': verificationCode ?? '',
'request_identity': requestIdentity ?? '',
},
),
);
_token = details['access_token'] as String? ?? '';
if (_token.isEmpty) throw const ApiException(500, '登录令牌缺失');
await _store.writeToken(_token);
notifyListeners();
}
Future<String> sendCode(String phone, String purpose) async {
final api = ApiClient(() => '');
final details = jsonMap(
await api.post(
'$_root/auth/verification-code',
authenticated: false,
body: {'phone': phone, 'purpose': purpose},
),
);
return details['request_identity'] as String? ?? '';
}
Future<void> logout() async {
_token = '';
await _store.clear();
notifyListeners();
}
}

View File

@@ -0,0 +1,125 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../ui/features/auth/login_page.dart';
import '../ui/features/auth/register_page.dart';
import '../ui/features/home/home_page.dart';
import '../ui/features/orders/orders_page.dart';
import '../ui/features/profile/profile_page.dart';
import '../ui/features/shared/record_list_page.dart';
import '../ui/features/shared/record_list_view_model.dart';
import '../ui/features/shop/shop_page.dart';
import 'dependencies.dart';
GoRouter createRouter(AppDependencies dependencies) => GoRouter(
initialLocation: '/home',
refreshListenable: dependencies.session,
redirect: (context, state) {
final authRoute = state.matchedLocation == '/login' || state.matchedLocation == '/register';
if (!dependencies.session.isAuthenticated && !authRoute) return '/login';
if (dependencies.session.isAuthenticated && state.matchedLocation == '/login') return '/home';
return null;
},
routes: [
GoRoute(
path: '/login',
builder: (context, state) => LoginPage(session: dependencies.session),
),
GoRoute(
path: '/register',
builder: (context, state) => RegisterPage(session: dependencies.session),
),
GoRoute(
path: '/records/contracts',
builder: (context, state) => RecordListPage(
title: '供气合同',
eyebrow: '可信履约',
viewModel: RecordListViewModel(dependencies.repository.contracts),
),
),
GoRoute(
path: '/records/wallet',
builder: (context, state) => RecordListPage(
title: '钱包流水',
eyebrow: '资金记录',
viewModel: RecordListViewModel(dependencies.repository.walletRecords),
),
),
StatefulShellRoute.indexedStack(
builder: (context, state, navigationShell) => _UserShell(navigationShell: navigationShell),
branches: [
StatefulShellBranch(
routes: [
GoRoute(
path: '/home',
builder: (context, state) => HomePage(repository: dependencies.repository),
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/shop',
builder: (context, state) => ShopPage(repository: dependencies.repository),
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/orders',
builder: (context, state) => OrdersPage(repository: dependencies.repository),
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/me',
builder: (context, state) =>
ProfilePage(session: dependencies.session, repository: dependencies.repository),
),
],
),
],
),
],
);
class _UserShell extends StatelessWidget {
const _UserShell({required this.navigationShell});
final StatefulNavigationShell navigationShell;
@override
Widget build(BuildContext context) => Scaffold(
body: navigationShell,
bottomNavigationBar: NavigationBar(
selectedIndex: navigationShell.currentIndex,
onDestinationSelected: (index) =>
navigationShell.goBranch(index, initialLocation: index == navigationShell.currentIndex),
destinations: const [
NavigationDestination(
icon: Icon(Icons.home_outlined),
selectedIcon: Icon(Icons.home),
label: '首页',
),
NavigationDestination(
icon: Icon(Icons.shopping_bag_outlined),
selectedIcon: Icon(Icons.shopping_bag),
label: '商城',
),
NavigationDestination(
icon: Icon(Icons.receipt_long_outlined),
selectedIcon: Icon(Icons.receipt_long),
label: '订单',
),
NavigationDestination(
icon: Icon(Icons.person_outline),
selectedIcon: Icon(Icons.person),
label: '我的',
),
],
),
);
}