2026-03-27 19:07:39 +08:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
|
2026-03-29 20:26:30 +08:00
|
|
|
import '../../../../data/network/i_api_client.dart';
|
|
|
|
|
import '../../../contacts/data/models/user_profile.dart';
|
2026-03-27 19:07:39 +08:00
|
|
|
|
|
|
|
|
class UserProfileService {
|
|
|
|
|
static const _prefix = '/api/v1/users';
|
|
|
|
|
|
|
|
|
|
final IApiClient _client;
|
|
|
|
|
|
|
|
|
|
UserProfileService(this._client);
|
|
|
|
|
|
|
|
|
|
Future<UserProfile> getMe() async {
|
|
|
|
|
final response = await _client.get<Map<String, dynamic>>('$_prefix/me');
|
|
|
|
|
final data = response.data;
|
|
|
|
|
if (data == null) {
|
|
|
|
|
throw StateError('Invalid getMe response: empty payload');
|
|
|
|
|
}
|
|
|
|
|
return UserProfile.fromJson(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<UserProfile> updateMe(UserUpdateRequest request) async {
|
|
|
|
|
final response = await _client.patch<Map<String, dynamic>>(
|
|
|
|
|
'$_prefix/me',
|
|
|
|
|
data: request.toJson(),
|
|
|
|
|
);
|
|
|
|
|
final data = response.data;
|
|
|
|
|
if (data == null) {
|
|
|
|
|
throw StateError('Invalid updateMe response: empty payload');
|
|
|
|
|
}
|
|
|
|
|
return UserProfile.fromJson(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<String> uploadAvatar(File file) async {
|
|
|
|
|
final formData = FormData.fromMap({
|
|
|
|
|
'file': await MultipartFile.fromFile(
|
|
|
|
|
file.path,
|
|
|
|
|
filename: file.path.split('/').last,
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
final response = await _client.post<Map<String, dynamic>>(
|
|
|
|
|
'$_prefix/me/avatar',
|
|
|
|
|
data: formData,
|
|
|
|
|
);
|
|
|
|
|
final data = response.data;
|
|
|
|
|
if (data == null || data['url'] is! String) {
|
|
|
|
|
throw StateError('Invalid uploadAvatar response: missing url');
|
|
|
|
|
}
|
|
|
|
|
return data['url'] as String;
|
|
|
|
|
}
|
|
|
|
|
}
|