Files
slogan/app/lib/features/profile/photo_upload_provider.dart
T
admin b53c57e02d Add 'app/' from commit 'a11a66886941bba128d89c1124115e1b1c87a128'
git-subtree-dir: app
git-subtree-mainline: 6ebd902c6b
git-subtree-split: a11a668869
2026-08-04 14:59:55 +08:00

80 lines
2.2 KiB
Dart

import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/auth/auth_provider.dart';
/// 照片类型(与后端 consts.PhotoType 对应)
class PhotoType {
static const int headshot = 1;
static const int fullFront = 2;
static const int fullSide = 3;
static const int fullBack = 4;
static const Map<int, String> labels = {
headshot: '大头照',
fullFront: '全身正面',
fullSide: '全身侧面',
fullBack: '全身背面',
};
static const Map<int, String> descs = {
headshot: '清晰正脸、光线充足',
fullFront: '站立正面全身,拍全脚底',
fullSide: '站立侧面全身,自然放松',
fullBack: '站立背面全身,露出轮廓',
};
/// 构建 3D 化身所需视角(Tripo 多视角转 3D)
static const List<int> all = [fullFront, fullSide, fullBack];
}
class UserPhotoInfo {
final int id;
final int type;
final String url;
const UserPhotoInfo({
required this.id,
required this.type,
required this.url,
});
}
class PhotoNotifier extends AsyncNotifier<List<UserPhotoInfo>> {
@override
Future<List<UserPhotoInfo>> build() async {
final api = ref.read(apiClientProvider);
final data = await api.get<Map<String, dynamic>>('/user-photo/list');
final list = data['list'] as List<dynamic>? ?? [];
return list
.map((e) => UserPhotoInfo(
id: (e['id'] as num).toInt(),
type: (e['type'] as num).toInt(),
url: e['url'] as String? ?? '',
))
.toList();
}
Future<void> upload(int type, String filePath) async {
final api = ref.read(apiClientProvider);
await api.upload('/user-photo/upload', {'type': type}, 'file', filePath);
await refresh();
}
Future<void> delete(int id) async {
final api = ref.read(apiClientProvider);
await api.post('/user-photo/delete', {'id': id});
await refresh();
}
Future<void> refresh() async {
state = const AsyncLoading();
state = await AsyncValue.guard(build);
}
/// 某类型是否已上传
bool hasType(int type) => state.value?.any((p) => p.type == type) ?? false;
}
final photoProvider =
AsyncNotifierProvider<PhotoNotifier, List<UserPhotoInfo>>(PhotoNotifier.new);