feat: 用户管理对接真实 API
后端: 新增 /api/v1/users CRUD + 重置密码接口
前端: user model/repository/provider + 设置页用户 tab 改为真实数据,
支持新增、编辑、启停、重置密码
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../core/api/api_client.dart';
|
||||
import '../core/auth/auth_state.dart';
|
||||
import '../models/user.dart';
|
||||
import '../repositories/user_repository.dart';
|
||||
|
||||
final userRepositoryProvider = Provider<UserRepository>((ref) {
|
||||
return UserRepository(ref.watch(apiClientProvider));
|
||||
});
|
||||
|
||||
final userListProvider =
|
||||
AsyncNotifierProvider<UserListNotifier, List<AppUser>>(
|
||||
UserListNotifier.new,
|
||||
);
|
||||
|
||||
class UserListNotifier extends AsyncNotifier<List<AppUser>> {
|
||||
@override
|
||||
Future<List<AppUser>> build() {
|
||||
ref.watch(authStateProvider.select((s) => s.user?.shopId));
|
||||
return ref.read(userRepositoryProvider).list();
|
||||
}
|
||||
|
||||
Future<void> reload() async {
|
||||
state = const AsyncValue.loading();
|
||||
state = await AsyncValue.guard(
|
||||
() => ref.read(userRepositoryProvider).list());
|
||||
}
|
||||
|
||||
Future<void> createUser(Map<String, dynamic> data) async {
|
||||
await ref.read(userRepositoryProvider).create(data);
|
||||
await reload();
|
||||
}
|
||||
|
||||
Future<void> updateUser(int id, Map<String, dynamic> data) async {
|
||||
await ref.read(userRepositoryProvider).update(id, data);
|
||||
await reload();
|
||||
}
|
||||
|
||||
Future<void> resetPassword(int id, String newPassword) async {
|
||||
await ref.read(userRepositoryProvider).resetPassword(id, newPassword);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user