初始化
This commit is contained in:
66
bin/model/ApiResponse.dart
Normal file
66
bin/model/ApiResponse.dart
Normal file
@@ -0,0 +1,66 @@
|
||||
import 'dart:convert';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:fixnum/fixnum.dart'; // 用于处理 Int64 类型
|
||||
|
||||
part 'ApiResponse.g.dart';
|
||||
|
||||
@JsonSerializable(genericArgumentFactories: true)
|
||||
class ApiResponse<T> {
|
||||
int? code;
|
||||
int? total;
|
||||
T? data;
|
||||
String? message;
|
||||
|
||||
ApiResponse();
|
||||
|
||||
// JSON 反序列化
|
||||
factory ApiResponse.fromJson(
|
||||
Map<String, dynamic> json,
|
||||
T Function(Object? json) fromJsonT,
|
||||
) =>
|
||||
_$ApiResponseFromJson(json, fromJsonT);
|
||||
|
||||
// JSON 序列化
|
||||
Map<String, dynamic> toJson(Object? Function(T value) toJsonT) =>
|
||||
_$ApiResponseToJson(this, toJsonT);
|
||||
|
||||
// 添加自定义的 toJson 序列化方法,确保能正确序列化泛型数据
|
||||
String serialize() {
|
||||
// 手动处理 data 中的 Int64 转换为 int
|
||||
var serializedData = _serializeData(data);
|
||||
return jsonEncode({
|
||||
'code': code,
|
||||
'total': total,
|
||||
'data': serializedData,
|
||||
'message': message,
|
||||
});
|
||||
}
|
||||
|
||||
// 递归处理 data 中的每个项,转换 Int64 为 int
|
||||
dynamic _serializeData(dynamic data) {
|
||||
if (data is List) {
|
||||
// 处理 List 中的 Int64 类型
|
||||
return data.map((e) => _serializeData(e)).toList();
|
||||
} else if (data is Map) {
|
||||
// 处理 Map 中的 Int64 类型
|
||||
var map = Map<String, dynamic>.from(data);
|
||||
map.forEach((key, value) {
|
||||
if (value is Int64) {
|
||||
map[key] = value.toInt();
|
||||
} else {
|
||||
map[key] = _serializeData(value);
|
||||
}
|
||||
});
|
||||
return map;
|
||||
} else if (data is Int64) {
|
||||
// 如果是 Int64 类型,转换为 int
|
||||
return data.toInt();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return serialize();
|
||||
}
|
||||
}
|
||||
40
bin/model/ApiResponse.g.dart
Normal file
40
bin/model/ApiResponse.g.dart
Normal file
@@ -0,0 +1,40 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'ApiResponse.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
ApiResponse<T> _$ApiResponseFromJson<T>(
|
||||
Map<String, dynamic> json,
|
||||
T Function(Object? json) fromJsonT,
|
||||
) =>
|
||||
ApiResponse<T>()
|
||||
..code = (json['code'] as num?)?.toInt()
|
||||
..total = (json['total'] as num?)?.toInt()
|
||||
..data = _$nullableGenericFromJson(json['data'], fromJsonT)
|
||||
..message = json['message'] as String?;
|
||||
|
||||
Map<String, dynamic> _$ApiResponseToJson<T>(
|
||||
ApiResponse<T> instance,
|
||||
Object? Function(T value) toJsonT,
|
||||
) =>
|
||||
<String, dynamic>{
|
||||
'code': instance.code,
|
||||
'total': instance.total,
|
||||
'data': _$nullableGenericToJson(instance.data, toJsonT),
|
||||
'message': instance.message,
|
||||
};
|
||||
|
||||
T? _$nullableGenericFromJson<T>(
|
||||
Object? input,
|
||||
T Function(Object? json) fromJson,
|
||||
) =>
|
||||
input == null ? null : fromJson(input);
|
||||
|
||||
Object? _$nullableGenericToJson<T>(
|
||||
T? input,
|
||||
Object? Function(T value) toJson,
|
||||
) =>
|
||||
input == null ? null : toJson(input);
|
||||
37
bin/model/Area.dart
Normal file
37
bin/model/Area.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'Area.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class Area {
|
||||
@JsonKey(name: '_id')
|
||||
String? id; // 区域id
|
||||
String? area_name; // 区域名称
|
||||
String? area_desc; // 区域描述
|
||||
int? created_at; // 创建时间(毫秒)
|
||||
int? updated_at; // 更新时间(毫秒)
|
||||
String? created_by_id; // 创建人ID
|
||||
String? created_by_name; // 创建人名称
|
||||
String? updated_by_id; // 更新人ID
|
||||
String? updated_by_name; // 更新人名称
|
||||
String? oid; // 机构ID
|
||||
int? deleted; // 是否删除
|
||||
String? points; // 坐标点集合
|
||||
String? tid; // 所属机构
|
||||
int? level; // 权限等级
|
||||
|
||||
Area();
|
||||
static Area fromJson(Map<String, dynamic> json) => _$AreaFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$AreaToJson(this);
|
||||
|
||||
// 使用查询参数填充 Area 对象
|
||||
static Area fromQueryParameters(Map<String, String> queryParameters, Map<String, dynamic> jwt) {
|
||||
var json = <String, dynamic>{};
|
||||
queryParameters.forEach((key, value) {
|
||||
json[key] = value;
|
||||
});
|
||||
json['tid'] = jwt['tid'];
|
||||
json['level'] = jwt['level'];
|
||||
return Area.fromJson(json);
|
||||
}
|
||||
}
|
||||
40
bin/model/Area.g.dart
Normal file
40
bin/model/Area.g.dart
Normal file
@@ -0,0 +1,40 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'Area.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
Area _$AreaFromJson(Map<String, dynamic> json) => Area()
|
||||
..id = json['_id'] as String?
|
||||
..area_name = json['area_name'] as String?
|
||||
..area_desc = json['area_desc'] as String?
|
||||
..created_at = (json['created_at'] as num?)?.toInt()
|
||||
..updated_at = (json['updated_at'] as num?)?.toInt()
|
||||
..created_by_id = json['created_by_id'] as String?
|
||||
..created_by_name = json['created_by_name'] as String?
|
||||
..updated_by_id = json['updated_by_id'] as String?
|
||||
..updated_by_name = json['updated_by_name'] as String?
|
||||
..oid = json['oid'] as String?
|
||||
..deleted = (json['deleted'] as num?)?.toInt()
|
||||
..points = json['points'] as String?
|
||||
..tid = json['tid'] as String?
|
||||
..level = (json['level'] as num?)?.toInt();
|
||||
|
||||
Map<String, dynamic> _$AreaToJson(Area instance) => <String, dynamic>{
|
||||
'_id': instance.id,
|
||||
'area_name': instance.area_name,
|
||||
'area_desc': instance.area_desc,
|
||||
'created_at': instance.created_at,
|
||||
'updated_at': instance.updated_at,
|
||||
'created_by_id': instance.created_by_id,
|
||||
'created_by_name': instance.created_by_name,
|
||||
'updated_by_id': instance.updated_by_id,
|
||||
'updated_by_name': instance.updated_by_name,
|
||||
'oid': instance.oid,
|
||||
'deleted': instance.deleted,
|
||||
'points': instance.points,
|
||||
'tid': instance.tid,
|
||||
'level': instance.level,
|
||||
};
|
||||
18
bin/model/WebSocketMessage.dart
Normal file
18
bin/model/WebSocketMessage.dart
Normal file
@@ -0,0 +1,18 @@
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'WebSocketMessage.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class WebSocketMessage {
|
||||
String path;
|
||||
int? type;
|
||||
dynamic data;
|
||||
String? to;
|
||||
String? mac;
|
||||
|
||||
WebSocketMessage({required this.path, this.type, this.data, this.to,this.mac});
|
||||
|
||||
static WebSocketMessage fromJson(Map<String, dynamic> json) =>
|
||||
_$WebSocketMessageFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$WebSocketMessageToJson(this);
|
||||
}
|
||||
25
bin/model/WebSocketMessage.g.dart
Normal file
25
bin/model/WebSocketMessage.g.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'WebSocketMessage.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
WebSocketMessage _$WebSocketMessageFromJson(Map<String, dynamic> json) =>
|
||||
WebSocketMessage(
|
||||
path: json['path'] as String,
|
||||
type: (json['type'] as num?)?.toInt(),
|
||||
data: json['data'],
|
||||
to: json['to'] as String?,
|
||||
mac: json['mac'] as String?,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$WebSocketMessageToJson(WebSocketMessage instance) =>
|
||||
<String, dynamic>{
|
||||
'path': instance.path,
|
||||
'type': instance.type,
|
||||
'data': instance.data,
|
||||
'to': instance.to,
|
||||
'mac': instance.mac,
|
||||
};
|
||||
29
bin/model/api_response.dart
Normal file
29
bin/model/api_response.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
class ApiResponse<T> {
|
||||
int? code;
|
||||
T? data;
|
||||
String? msg;
|
||||
int? total;
|
||||
dynamic rawResponse; // 原始 Dio 响应对象
|
||||
|
||||
ApiResponse({
|
||||
required this.code,
|
||||
this.data,
|
||||
this.msg,
|
||||
this.total,
|
||||
this.rawResponse,
|
||||
});
|
||||
|
||||
factory ApiResponse.fromJson(
|
||||
Map<String, dynamic> json,
|
||||
T Function(Object?) fromJsonT, {
|
||||
dynamic rawResponse,
|
||||
}) {
|
||||
return ApiResponse<T>(
|
||||
code: json['code'] as int?,
|
||||
data: json['data'] != null ? fromJsonT(json['data']) : null,
|
||||
msg: json['msg'] as String?,
|
||||
total: json['total'] as int?,
|
||||
rawResponse: rawResponse, // 保留 Dio 原始响应对象
|
||||
);
|
||||
}
|
||||
}
|
||||
19
bin/model/app_uri_status.dart
Normal file
19
bin/model/app_uri_status.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
// lib/http_status_codes.dart
|
||||
|
||||
class HttpStatusCodes {
|
||||
// Success codes
|
||||
static const int ok = 200;
|
||||
static const int created = 201;
|
||||
static const int accepted = 202;
|
||||
|
||||
// Client error codes
|
||||
static const int badRequest = 400;
|
||||
static const int unauthorized = 401;//401 参数错误
|
||||
static const int forbidden = 403;//403 禁止,多条数据,不允许修改
|
||||
static const int notFound = 404;//404 没有报告
|
||||
|
||||
// Server error codes
|
||||
static const int internalServerError = 500;//500 内部异常
|
||||
static const int notImplemented = 501;
|
||||
static const int serviceUnavailable = 503;
|
||||
}
|
||||
Reference in New Issue
Block a user