179 lines
3.7 KiB
Dart
179 lines
3.7 KiB
Dart
// 网络请求相关
|
||
import 'package:dio/dio.dart';
|
||
import 'package:json_annotation/json_annotation.dart';
|
||
|
||
// 文件处理
|
||
import 'package:path/path.dart' as path;
|
||
|
||
// 生成的代码
|
||
part 'advanced_api_service_api.g.dart';
|
||
|
||
/// 基础响应结果
|
||
@JsonSerializable(genericArgumentFactories: true)
|
||
class ApiResult<T> {
|
||
/// 响应码
|
||
final int code;
|
||
|
||
/// 响应消息
|
||
final String message;
|
||
|
||
/// 响应数据
|
||
final T? data;
|
||
|
||
/// 是否成功
|
||
bool get isSuccess => code == 200;
|
||
|
||
const ApiResult({
|
||
required this.code,
|
||
required this.message,
|
||
this.data,
|
||
});
|
||
|
||
factory ApiResult.fromJson(
|
||
Map<String, dynamic> json,
|
||
T Function(Object? json) fromJsonT,
|
||
) =>
|
||
_$ApiResultFromJson(json, fromJsonT);
|
||
|
||
Map<String, dynamic> toJson(Object Function(T value) toJsonT) =>
|
||
_$ApiResultToJson(this, toJsonT);
|
||
}
|
||
|
||
/// 分页参数
|
||
@JsonSerializable()
|
||
class BasePageParameter {
|
||
/// 页码(从1开始)
|
||
final int page;
|
||
|
||
/// 每页大小
|
||
final int size;
|
||
|
||
const BasePageParameter({
|
||
this.page = 1,
|
||
this.size = 20,
|
||
});
|
||
|
||
factory BasePageParameter.fromJson(Map<String, dynamic> json) =>
|
||
_$BasePageParameterFromJson(json);
|
||
|
||
Map<String, dynamic> toJson() => _$BasePageParameterToJson(this);
|
||
}
|
||
|
||
/// 分页响应结果
|
||
@JsonSerializable(genericArgumentFactories: true)
|
||
class PagedResult<T> {
|
||
/// 数据列表
|
||
final List<T> list;
|
||
|
||
/// 总数量
|
||
final int total;
|
||
|
||
/// 当前页码
|
||
final int page;
|
||
|
||
/// 每页大小
|
||
final int size;
|
||
|
||
/// 总页数
|
||
int get totalPages => (total / size).ceil();
|
||
|
||
/// 是否有下一页
|
||
bool get hasNext => page < totalPages;
|
||
|
||
/// 是否有上一页
|
||
bool get hasPrevious => page > 1;
|
||
|
||
const PagedResult({
|
||
required this.list,
|
||
required this.total,
|
||
required this.page,
|
||
required this.size,
|
||
});
|
||
|
||
factory PagedResult.fromJson(
|
||
Map<String, dynamic> json,
|
||
T Function(Object? json) fromJsonT,
|
||
) =>
|
||
_$PagedResultFromJson(json, fromJsonT);
|
||
|
||
Map<String, dynamic> toJson(Object Function(T value) toJsonT) =>
|
||
_$PagedResultToJson(this, toJsonT);
|
||
}
|
||
|
||
/// 文件上传请求
|
||
@JsonSerializable()
|
||
class FileUploadRequest {
|
||
/// 文件
|
||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||
final MultipartFile file;
|
||
|
||
/// 文件名
|
||
final String? filename;
|
||
|
||
/// 文件类型
|
||
final String? contentType;
|
||
|
||
const FileUploadRequest({
|
||
required this.file,
|
||
this.filename,
|
||
this.contentType,
|
||
});
|
||
|
||
factory FileUploadRequest.fromJson(Map<String, dynamic> json) =>
|
||
_$FileUploadRequestFromJson(json);
|
||
|
||
Map<String, dynamic> toJson() => _$FileUploadRequestToJson(this);
|
||
}
|
||
|
||
/// 文件上传响应
|
||
@JsonSerializable()
|
||
class FileUploadResult {
|
||
/// 文件 URL
|
||
final String url;
|
||
|
||
/// 文件名
|
||
final String filename;
|
||
|
||
/// 文件大小
|
||
final int size;
|
||
|
||
/// 文件类型
|
||
final String? contentType;
|
||
|
||
const FileUploadResult({
|
||
required this.url,
|
||
required this.filename,
|
||
required this.size,
|
||
this.contentType,
|
||
});
|
||
|
||
factory FileUploadResult.fromJson(Map<String, dynamic> json) =>
|
||
_$FileUploadResultFromJson(json);
|
||
|
||
Map<String, dynamic> toJson() => _$FileUploadResultToJson(this);
|
||
}
|
||
|
||
/// 主 API 服务类
|
||
/// 包含所有模块的 API 接口
|
||
class AdvancedApiService {
|
||
final Dio _dio;
|
||
|
||
AdvancedApiService(this._dio, {String? baseUrl});
|
||
}
|
||
|
||
/// API 工具类
|
||
class ApiUtils {
|
||
/// 创建文件上传对象
|
||
static Future<MultipartFile> createFileUpload(String filePath) async {
|
||
return MultipartFile.fromFile(
|
||
filePath,
|
||
filename: path.basename(filePath),
|
||
);
|
||
}
|
||
|
||
/// 创建分页参数
|
||
static BasePageParameter createPageParam({int page = 1, int size = 20}) {
|
||
return BasePageParameter(page: page, size: size);
|
||
}
|
||
}
|