hi-client/lib/app/network/base_response.dart
2025-10-13 18:08:02 +08:00

81 lines
2.2 KiB
Dart
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:convert';
import 'package:get/get.dart';
import 'package:kaer_with_panels/app/common/app_run_data.dart';
import 'package:kaer_with_panels/app/mixins/kr_app_bar_opacity_mixin.dart';
import 'package:kaer_with_panels/app/model/entity_from_json_util.dart';
import '../utils/kr_aes_util.dart';
/// 接口返回基础类
class BaseResponse<T> {
late int retCode; //状态码
late String retMsg; //返回的信息
late Map<String, dynamic> body; // 返回的数据
late T model;
List<T> list = []; // 初始化为空列表
bool isSuccess = true; // 是否返回正确数据
BaseResponse.fromJson(Map<String, dynamic> json) {
retCode = json['code'];
final aes = AESUtils();
final dataMap = json['data'] ?? Map<String, dynamic>();
final cipherText = dataMap['data'] ?? "";
final nonce = dataMap['time'] ?? "";
if (cipherText.isNotEmpty && nonce.isNotEmpty) {
final encrypted = aes.decryptData(cipherText, "ne6t2qcz-szoa-rw78-egqz-lrsxxbl0dke3", nonce);
body = jsonDecode(encrypted);
}
else
{
body = dataMap;
}
if (retCode == 40004 || retCode == 40005 || retCode == 40002 || retCode == 40003) {
KRAppRunData().kr_loginOut();
}
if (retCode != 200) {
isSuccess = false;
}
retMsg = json['msg'];
// 获取错误信息
final msg = "error.${retCode.toString()}".tr;
if (msg.isNotEmpty && msg != "error.${retCode.toString()}") {
retMsg = msg;
}
if (body.isNotEmpty) {
if (body is List) {
list = (json['data'] as List<dynamic>)
.map((e) => EntityFromJsonUtil.parseJsonToEntity<T>(e))
.toList();
} else {
if (T == dynamic) {
model = body as T;
} else {
model = EntityFromJsonUtil.parseJsonToEntity<T>(body);
}
}
} else {
// 当body为空时设置默认model值
}
}
// 获取泛型T的默认值
T _getDefaultValue<T>() {
if (T == String) return '' as T;
if (T == int) return 0 as T;
if (T == double) return 0.0 as T;
if (T == bool) return false as T;
if (T == Map) return {} as T;
if (T == List) return [] as T;
return null as T;
}
}