92 lines
2.3 KiB
Dart
92 lines
2.3 KiB
Dart
import '../../core/constants/api_endpoints.dart';
|
|
import '../../core/network/api_response.dart';
|
|
import '../../core/network/dio_client.dart';
|
|
import '../models/order_models.dart';
|
|
|
|
/// 交易服務
|
|
class TradeService {
|
|
final DioClient _client;
|
|
|
|
TradeService(this._client);
|
|
|
|
/// 買入
|
|
Future<ApiResponse<Map<String, dynamic>>> buy({
|
|
required String coinCode,
|
|
required String price,
|
|
required String quantity,
|
|
int orderType = 1, // 1=市价单, 2=限价单
|
|
}) async {
|
|
return _client.post<Map<String, dynamic>>(
|
|
ApiEndpoints.buy,
|
|
data: {
|
|
'coinCode': coinCode,
|
|
'price': price,
|
|
'quantity': quantity,
|
|
'orderType': orderType,
|
|
},
|
|
);
|
|
}
|
|
|
|
/// 賣出
|
|
Future<ApiResponse<Map<String, dynamic>>> sell({
|
|
required String coinCode,
|
|
required String price,
|
|
required String quantity,
|
|
int orderType = 1, // 1=市价单, 2=限价单
|
|
}) async {
|
|
return _client.post<Map<String, dynamic>>(
|
|
ApiEndpoints.sell,
|
|
data: {
|
|
'coinCode': coinCode,
|
|
'price': price,
|
|
'quantity': quantity,
|
|
'orderType': orderType,
|
|
},
|
|
);
|
|
}
|
|
|
|
/// 獲取交易記錄
|
|
Future<ApiResponse<Map<String, dynamic>>> getOrders({
|
|
String? coinCode,
|
|
int? direction,
|
|
int pageNum = 1,
|
|
int pageSize = 20,
|
|
}) async {
|
|
final params = <String, dynamic>{
|
|
'pageNum': pageNum,
|
|
'pageSize': pageSize,
|
|
};
|
|
if (coinCode != null) params['coinCode'] = coinCode;
|
|
if (direction != null) params['direction'] = direction;
|
|
|
|
return _client.get<Map<String, dynamic>>(
|
|
ApiEndpoints.tradeOrders,
|
|
queryParameters: params,
|
|
);
|
|
}
|
|
|
|
/// 獲取訂單詳情
|
|
Future<ApiResponse<OrderTrade>> getOrderDetail(String orderNo) async {
|
|
final response = await _client.get<Map<String, dynamic>>(
|
|
ApiEndpoints.tradeOrderDetail,
|
|
queryParameters: {'orderNo': orderNo},
|
|
);
|
|
|
|
if (response.success && response.data != null) {
|
|
return ApiResponse.success(
|
|
OrderTrade.fromJson(response.data!),
|
|
response.message,
|
|
);
|
|
}
|
|
return ApiResponse.fail(response.message ?? '獲取訂單詳情失敗');
|
|
}
|
|
|
|
/// 撤销限价委托
|
|
Future<ApiResponse<Map<String, dynamic>>> cancelOrder(String orderNo) async {
|
|
return _client.post<Map<String, dynamic>>(
|
|
ApiEndpoints.tradeCancel,
|
|
data: {'orderNo': orderNo},
|
|
);
|
|
}
|
|
}
|