79 lines
2.7 KiB
Dart
79 lines
2.7 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:EasyDartModule/EasyDartModule.dart';
|
|
import 'package:ef/ef.dart';
|
|
import 'package:flutterflow_ui/flutterflow_ui.dart';
|
|
import 'package:json_annotation/json_annotation.dart';
|
|
import 'package:vbvs_app/common/color/ServiceConstant.dart';
|
|
import 'package:vbvs_app/common/color/app_uri_status.dart';
|
|
import 'package:vbvs_app/common/util/DailyLogUtils.dart';
|
|
import 'package:vbvs_app/common/util/MyUtils.dart';
|
|
import 'package:vbvs_app/model/api_response.dart';
|
|
|
|
part 'message_controller.g.dart'; // 由json_serializable自动生成的部分
|
|
|
|
@JsonSerializable()
|
|
class MessageModel {
|
|
int? type = 1; //设备类型 1:体征消息 2.系统消息
|
|
|
|
MessageModel();
|
|
|
|
// 从JSON反序列化时的异常处理
|
|
|
|
factory MessageModel.fromJson(Map<String, dynamic> json) {
|
|
try {
|
|
return _$MessageModelFromJson(json);
|
|
} catch (e) {
|
|
// 在实际应用中,应该有更细致的异常处理策略和错误日志
|
|
return MessageModel(); // 或者返回一个带有错误信息的特定DeviceInfoModel实例
|
|
}
|
|
}
|
|
|
|
// 序列化为JSON时的异常处理
|
|
Map<String, dynamic> toJson() => _$MessageModelToJson(this);
|
|
}
|
|
|
|
class MessageController extends GetControllerEx<MessageModel> {
|
|
MessageController() {
|
|
attr = GetModel(MessageModel()).obs;
|
|
}
|
|
|
|
RxList msssageList = [].obs;
|
|
|
|
Future<ApiResponse> getMessageList({String? key}) async {
|
|
try {
|
|
ApiResponse apiResponse = ApiResponse(code: -1, msg: "请求失败".tr);
|
|
String serviceAddress = ServiceConstant.service_address;
|
|
String serviceName = ServiceConstant.server_service;
|
|
String serviceApi = ServiceConstant.message_list;
|
|
String messageType = "app_system";
|
|
if (model.type == 1) {
|
|
messageType = "app_body";
|
|
} else {
|
|
messageType = "app_system";
|
|
}
|
|
String queryUrl =
|
|
"${serviceAddress}${serviceName}${serviceApi}?type=${messageType}'}";
|
|
var response = await EasyDartModule.dio.get(queryUrl);
|
|
if (response != null) {
|
|
var responseData =
|
|
response.data is String ? jsonDecode(response.data) : response.data;
|
|
ApiResponse res =
|
|
ApiResponse.fromJson(responseData, (object) => object);
|
|
MyUtils.formatResponse(res, "请求成功".tr, "请求失败".tr);
|
|
if (res.code == HttpStatusCodes.ok) {
|
|
updateAll();
|
|
return res;
|
|
}
|
|
} else {
|
|
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
|
}
|
|
return apiResponse;
|
|
} catch (e) {
|
|
EasyDartModule.logger.info("设备请求列表: $e");
|
|
DailyLogUtils.writeLog("设备请求列表: $e");
|
|
}
|
|
return ApiResponse(code: -1, msg: "未知错误".tr); // Default return statement
|
|
}
|
|
}
|