create project
This commit is contained in:
113
frontend/lib/providers/auth_provider.dart
Normal file
113
frontend/lib/providers/auth_provider.dart
Normal file
@@ -0,0 +1,113 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../api/api_client.dart';
|
||||
import '../api/auth_api.dart';
|
||||
import '../models/user.dart';
|
||||
|
||||
final apiClientProvider = Provider<ApiClient>((ref) => ApiClient());
|
||||
|
||||
final authApiProvider = Provider<AuthApi>((ref) {
|
||||
return AuthApi(ref.read(apiClientProvider));
|
||||
});
|
||||
|
||||
class AuthState {
|
||||
final bool isLoggedIn;
|
||||
final User? user;
|
||||
final String? token;
|
||||
final bool isLoading;
|
||||
final String? error;
|
||||
|
||||
const AuthState({
|
||||
this.isLoggedIn = false,
|
||||
this.user,
|
||||
this.token,
|
||||
this.isLoading = false,
|
||||
this.error,
|
||||
});
|
||||
|
||||
AuthState copyWith({
|
||||
bool? isLoggedIn,
|
||||
User? user,
|
||||
String? token,
|
||||
bool? isLoading,
|
||||
String? error,
|
||||
}) {
|
||||
return AuthState(
|
||||
isLoggedIn: isLoggedIn ?? this.isLoggedIn,
|
||||
user: user ?? this.user,
|
||||
token: token ?? this.token,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AuthNotifier extends StateNotifier<AuthState> {
|
||||
final AuthApi _api;
|
||||
final ApiClient _client;
|
||||
|
||||
AuthNotifier(this._api, this._client) : super(const AuthState());
|
||||
|
||||
Future<void> login(String phone, String password) async {
|
||||
state = state.copyWith(isLoading: true, error: null);
|
||||
try {
|
||||
final res = await _api.login(phone, password);
|
||||
if (res['success'] == true) {
|
||||
final token = res['data']['token'] as String;
|
||||
final user = User.fromJson(res['data']['user']);
|
||||
await _client.saveToken(token);
|
||||
state = AuthState(isLoggedIn: true, user: user, token: token);
|
||||
} else {
|
||||
state = state.copyWith(isLoading: false, error: res['error'] as String?);
|
||||
}
|
||||
} catch (e) {
|
||||
state = state.copyWith(isLoading: false, error: '网络错误: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> register(String phone, String password, String nickname) async {
|
||||
state = state.copyWith(isLoading: true, error: null);
|
||||
try {
|
||||
final res = await _api.register(phone, password, nickname);
|
||||
if (res['success'] == true) {
|
||||
final token = res['data']['token'] as String;
|
||||
final user = User.fromJson(res['data']['user']);
|
||||
await _client.saveToken(token);
|
||||
state = AuthState(isLoggedIn: true, user: user, token: token);
|
||||
} else {
|
||||
state = state.copyWith(isLoading: false, error: res['error'] as String?);
|
||||
}
|
||||
} catch (e) {
|
||||
state = state.copyWith(isLoading: false, error: '网络错误: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
await _client.clearToken();
|
||||
state = const AuthState();
|
||||
}
|
||||
|
||||
Future<void> restoreSession() async {
|
||||
final token = await _client.getToken();
|
||||
if (token == null) return;
|
||||
try {
|
||||
final res = await _api.getProfile();
|
||||
if (res['success'] == true) {
|
||||
state = AuthState(isLoggedIn: true, user: User.fromJson(res['data']), token: token);
|
||||
} else {
|
||||
await _client.clearToken();
|
||||
}
|
||||
} catch (_) {
|
||||
// offline - keep token for retry
|
||||
}
|
||||
}
|
||||
|
||||
void clearError() {
|
||||
state = state.copyWith(error: null);
|
||||
}
|
||||
}
|
||||
|
||||
final authProvider = StateNotifierProvider<AuthNotifier, AuthState>((ref) {
|
||||
final api = ref.read(authApiProvider);
|
||||
final client = ref.read(apiClientProvider);
|
||||
return AuthNotifier(api, client);
|
||||
});
|
||||
45
frontend/lib/providers/bill_provider.dart
Normal file
45
frontend/lib/providers/bill_provider.dart
Normal file
@@ -0,0 +1,45 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../api/bill_api.dart';
|
||||
import '../api/api_client.dart';
|
||||
import '../models/bill.dart';
|
||||
|
||||
final billApiProvider = Provider<BillApi>((ref) {
|
||||
return BillApi(ref.read(apiClientProvider));
|
||||
});
|
||||
|
||||
class BillNotifier extends StateNotifier<AsyncValue<List<Bill>>> {
|
||||
final BillApi _api;
|
||||
String? _currentFamilyId;
|
||||
|
||||
BillNotifier(this._api) : super(const AsyncValue.data([]));
|
||||
|
||||
Future<void> loadBills({String? familyId}) async {
|
||||
_currentFamilyId = familyId;
|
||||
state = const AsyncValue.loading();
|
||||
try {
|
||||
final res = await _api.getBills(familyId: familyId);
|
||||
if (res['success'] == true) {
|
||||
state = AsyncValue.data((res['data'] as List).map((e) => Bill.fromJson(e)).toList());
|
||||
} else {
|
||||
state = AsyncValue.error(res['error'] ?? '加载失败', StackTrace.current);
|
||||
}
|
||||
} catch (e, st) {
|
||||
state = AsyncValue.error(e.toString(), st);
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> addBill(Map<String, dynamic> data) async {
|
||||
try {
|
||||
final res = await _api.createBill(data);
|
||||
if (res['success'] == true) {
|
||||
await loadBills(familyId: _currentFamilyId);
|
||||
return true;
|
||||
}
|
||||
} catch (_) {}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
final billProvider = StateNotifierProvider<BillNotifier, AsyncValue<List<Bill>>>((ref) {
|
||||
return BillNotifier(ref.read(billApiProvider));
|
||||
});
|
||||
52
frontend/lib/providers/family_provider.dart
Normal file
52
frontend/lib/providers/family_provider.dart
Normal file
@@ -0,0 +1,52 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../api/family_api.dart';
|
||||
import '../api/api_client.dart';
|
||||
import '../models/family.dart';
|
||||
|
||||
final familyApiProvider = Provider<FamilyApi>((ref) {
|
||||
return FamilyApi(ref.read(apiClientProvider));
|
||||
});
|
||||
|
||||
class FamilyNotifier extends StateNotifier<AsyncValue<List<Family>>> {
|
||||
final FamilyApi _api;
|
||||
|
||||
FamilyNotifier(this._api) : super(const AsyncValue.loading());
|
||||
|
||||
Future<void> loadFamilies() async {
|
||||
state = const AsyncValue.loading();
|
||||
try {
|
||||
final res = await _api.getFamilies();
|
||||
if (res['success'] == true) {
|
||||
state = AsyncValue.data((res['data'] as List).map((e) => Family.fromJson(e)).toList());
|
||||
} else {
|
||||
state = AsyncValue.error(res['error'] ?? '加载失败', StackTrace.current);
|
||||
}
|
||||
} catch (e, st) {
|
||||
state = AsyncValue.error(e.toString(), st);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Family?> createFamily(String name) async {
|
||||
try {
|
||||
final res = await _api.createFamily(name);
|
||||
if (res['success'] == true) {
|
||||
return Family.fromJson(res['data']);
|
||||
}
|
||||
} catch (_) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<Family?> joinFamily(String inviteCode) async {
|
||||
try {
|
||||
final res = await _api.joinFamily(inviteCode);
|
||||
if (res['success'] == true) {
|
||||
return Family.fromJson(res['data']);
|
||||
}
|
||||
} catch (_) {}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
final familyProvider = StateNotifierProvider<FamilyNotifier, AsyncValue<List<Family>>>((ref) {
|
||||
return FamilyNotifier(ref.read(familyApiProvider));
|
||||
});
|
||||
Reference in New Issue
Block a user