65 lines
1.7 KiB
Dart
65 lines
1.7 KiB
Dart
import 'dart:typed_data';
|
|
import 'package:dio/dio.dart';
|
|
import '../../core/constants/api_endpoints.dart';
|
|
import '../../core/network/api_response.dart';
|
|
import '../../core/network/dio_client.dart';
|
|
import '../models/user.dart';
|
|
|
|
/// 用户服务
|
|
class UserService {
|
|
final DioClient _client;
|
|
|
|
UserService(this._client);
|
|
|
|
/// 用户登录
|
|
Future<ApiResponse<Map<String, dynamic>>> login(
|
|
String username,
|
|
String password,
|
|
) async {
|
|
return _client.post<Map<String, dynamic>>(
|
|
ApiEndpoints.login,
|
|
data: {'username': username, 'password': password},
|
|
);
|
|
}
|
|
|
|
/// 用户注册
|
|
Future<ApiResponse<Map<String, dynamic>>> register(
|
|
String username,
|
|
String password,
|
|
) async {
|
|
return _client.post<Map<String, dynamic>>(
|
|
ApiEndpoints.register,
|
|
data: {'username': username, 'password': password},
|
|
);
|
|
}
|
|
|
|
/// 获取用户信息
|
|
Future<ApiResponse<User>> getUserInfo() async {
|
|
return _client.get<User>(
|
|
ApiEndpoints.userInfo,
|
|
fromJson: (data) => User.fromJson(data as Map<String, dynamic>),
|
|
);
|
|
}
|
|
|
|
/// 上传 KYC 资料(身份证正反面图片字节)
|
|
/// 使用 fromBytes 以兼容 Web 和移动端
|
|
Future<ApiResponse<void>> uploadKyc(
|
|
Uint8List frontBytes,
|
|
Uint8List backBytes,
|
|
) async {
|
|
final formData = FormData.fromMap({
|
|
'front': MultipartFile.fromBytes(frontBytes, filename: 'front.jpg'),
|
|
'back': MultipartFile.fromBytes(backBytes, filename: 'back.jpg'),
|
|
});
|
|
return _client.upload<void>(
|
|
ApiEndpoints.kyc,
|
|
formData: formData,
|
|
);
|
|
}
|
|
|
|
/// 退出登录
|
|
Future<ApiResponse<void>> logout() async {
|
|
return _client.post<void>(ApiEndpoints.logout);
|
|
}
|
|
}
|