更新分享
This commit is contained in:
@@ -2,54 +2,54 @@ import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:EasyDartModule/EasyDartModule.dart';
|
||||
import 'package:easydevice/src/ble_device.dart';
|
||||
import 'package:easydevice/easydevice.dart';
|
||||
import 'package:ef/ef.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/component/tool/TopSlideNotification.dart';
|
||||
import 'package:vbvs_app/controller/person/person_controller.dart';
|
||||
import 'package:vbvs_app/model/BleDeviceData.dart';
|
||||
import 'package:vbvs_app/model/api_response.dart';
|
||||
|
||||
part 'blueteeth_bind_controller.g.dart'; // 由json_serializable自动生成的部分
|
||||
part 'blueteeth_bind_controller.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class BlueteethBindModel {
|
||||
int? read = 1; //是否不再提示教程 0 不再提示 1 需要提示
|
||||
double? singal = -70; //扫描信号强度
|
||||
int? read = 1;
|
||||
double? singal = -70;
|
||||
|
||||
List<BleDeviceData>? devicelist = []; //蓝牙扫描到的设备数据列表
|
||||
List<BleDeviceData>? betDevicelist = []; //请求的
|
||||
List? blelist = []; //蓝牙扫描到的设备数据列表
|
||||
List? wifiList = [];
|
||||
@JsonKey(ignore: true)
|
||||
List<BleDeviceData>? devicelist = [];//蓝牙扫描
|
||||
@JsonKey(ignore: true)
|
||||
List<BleDeviceData>? betDevicelist = [];//网络状态
|
||||
@JsonKey(ignore: true)
|
||||
List? blelist = [];
|
||||
|
||||
List bindArr = ["", "", ""];
|
||||
|
||||
String connectedWifiName = "";
|
||||
|
||||
int connectedRssi = 0;
|
||||
|
||||
String deviceName = "";
|
||||
|
||||
bool? deviceIndex0 = true;
|
||||
bool? deviceIndex1 = false;
|
||||
bool? deviceIndex2 = false;
|
||||
|
||||
BlueteethBindModel();
|
||||
bool wifiPassShow = false;
|
||||
String? wifiPass;
|
||||
|
||||
// 从JSON反序列化时的异常处理
|
||||
BlueteethBindModel();
|
||||
|
||||
factory BlueteethBindModel.fromJson(Map<String, dynamic> json) {
|
||||
try {
|
||||
return _$BlueteethBindModelFromJson(json);
|
||||
} catch (e) {
|
||||
// 在实际应用中,应该有更细致的异常处理策略和错误日志
|
||||
return BlueteethBindModel(); // 或者返回一个带有错误信息的特定DeviceInfoModel实例
|
||||
return BlueteethBindModel();
|
||||
}
|
||||
}
|
||||
|
||||
// 序列化为JSON时的异常处理
|
||||
Map<String, dynamic> toJson() => _$BlueteethBindModelToJson(this);
|
||||
}
|
||||
|
||||
@@ -60,30 +60,73 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
||||
|
||||
Timer? _statusTimer;
|
||||
|
||||
BLEDevice? currentDevice;
|
||||
THapp? currentDevice;
|
||||
RxInt wifiStatus = 0.obs;
|
||||
RxList wifiList = [].obs;
|
||||
RxMap connect_wifi = {}.obs;
|
||||
RxString scanMac = "".obs;
|
||||
|
||||
// 安全展示 TopSlideNotification
|
||||
void safeShowNotification(String msg) {
|
||||
try {
|
||||
final ctx = Get.context;
|
||||
if (ctx != null && ctx.mounted) {
|
||||
TopSlideNotification.show(
|
||||
ctx,
|
||||
text: msg,
|
||||
textColor: themeController.currentColor.sc9,
|
||||
);
|
||||
} else {
|
||||
print("TopSlideNotification 未显示:context 不可用或未挂载");
|
||||
}
|
||||
} catch (e, stack) {
|
||||
// print("TopSlideNotification 显示异常: $e\n$stack");
|
||||
}
|
||||
}
|
||||
|
||||
// 启动每10秒获取设备状态
|
||||
void startStatusPolling() {
|
||||
updateDeviceStatus().then((res) {
|
||||
if (res.code == HttpStatusCodes.ok) {
|
||||
updateAll();
|
||||
} else {
|
||||
safeShowNotification(res.msg ?? "获取设备状态异常");
|
||||
EasyDartModule.logger.info("获取设备状态异常: $res");
|
||||
DailyLogUtils.writeLog("获取设备状态异常: $res");
|
||||
}
|
||||
});
|
||||
|
||||
if (_statusTimer == null) {
|
||||
_statusTimer = Timer.periodic(Duration(seconds: 10), (timer) {
|
||||
updateDeviceStatus();
|
||||
updateDeviceStatus().then((res) {
|
||||
if (res.code == HttpStatusCodes.ok) {
|
||||
updateAll();
|
||||
} else {
|
||||
safeShowNotification(res.msg ?? "获取设备状态异常");
|
||||
EasyDartModule.logger.info("获取设备状态异常: $res");
|
||||
DailyLogUtils.writeLog("获取设备状态异常: $res");
|
||||
}
|
||||
}).catchError((e, stack) {
|
||||
print("updateDeviceStatus 执行异常: $e\n$stack");
|
||||
safeShowNotification("设备状态请求失败");
|
||||
EasyDartModule.logger.info("设备状态异常: $e");
|
||||
DailyLogUtils.writeLog("设备状态异常: $e");
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 停止轮询
|
||||
void stopStatusPolling() {
|
||||
_statusTimer?.cancel();
|
||||
_statusTimer = null;
|
||||
}
|
||||
|
||||
// 你的已有方法
|
||||
Future<void> updateDeviceStatus() async {
|
||||
Future<ApiResponse> updateDeviceStatus() async {
|
||||
try {
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.get_bluetooth_device_status;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
String queryUrl = "$serviceAddress$serviceName$serviceApi";
|
||||
|
||||
if (model.devicelist != null && model.devicelist!.isNotEmpty) {
|
||||
final macParams = model.devicelist!
|
||||
@@ -97,13 +140,16 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
||||
}
|
||||
|
||||
var response = await EasyDartModule.dio.get(queryUrl);
|
||||
var responseData =
|
||||
response.data is String ? jsonDecode(response.data) : response.data;
|
||||
ApiResponse res =
|
||||
ApiResponse.fromJson(responseData, (object) => object);
|
||||
if (res.code != HttpStatusCodes.ok) return res;
|
||||
|
||||
if (response.data['data'] != null && response.data['data'] is List) {
|
||||
List<dynamic> responseList = response.data['data'];
|
||||
|
||||
Map<String, BleDeviceData> deviceMap = {
|
||||
for (var d in model.devicelist!)
|
||||
if (d.mac != null) d.mac!: d
|
||||
for (var d in model.devicelist!) if (d.mac != null) d.mac!: d
|
||||
};
|
||||
|
||||
List<String> slaveMacsToRemove = [];
|
||||
@@ -123,17 +169,26 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
model.devicelist!
|
||||
.removeWhere((device) => slaveMacsToRemove.contains(device.mac));
|
||||
model.betDevicelist = model.devicelist!;
|
||||
} else {
|
||||
model.betDevicelist = [];
|
||||
}
|
||||
|
||||
print("获取设备状态成功");
|
||||
updateAll();
|
||||
return res;
|
||||
}
|
||||
} catch (e) {
|
||||
print("获取设备状态异常: $e");
|
||||
EasyDartModule.logger.info("获取设备状态异常: $e");
|
||||
DailyLogUtils.writeLog("获取设备状态异常: $e");
|
||||
return ApiResponse(code: -1, msg: "请求失败".tr);
|
||||
}
|
||||
|
||||
return ApiResponse(code: -1, msg: "未知错误".tr);
|
||||
}
|
||||
|
||||
Future<ApiResponse> bindDeviceAndMAC(BleDeviceData d) async {
|
||||
@@ -142,7 +197,7 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.device_bind;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
String queryUrl = "$serviceAddress$serviceName$serviceApi";
|
||||
var data = {
|
||||
"deviceType": 1,
|
||||
"mac": d.mac,
|
||||
@@ -154,10 +209,55 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
||||
response.data is String ? jsonDecode(response.data) : response.data;
|
||||
ApiResponse res =
|
||||
ApiResponse.fromJson(responseData, (object) => object);
|
||||
MyUtils.formatResponse(apiResponse, "蓝牙绑定.绑定成功".tr, "蓝牙绑定.绑定成功".tr);
|
||||
MyUtils.formatResponse(res, "蓝牙绑定.绑定成功".tr, "蓝牙绑定.绑定成功".tr);
|
||||
if (res.code == HttpStatusCodes.ok) {
|
||||
PersonController personController = Get.find();
|
||||
personController.currentPersonId.value = res.data['id'];
|
||||
return res;
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
} else {
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
} catch (e) {
|
||||
EasyDartModule.logger.info("蓝牙绑定.绑定异常: $e");
|
||||
DailyLogUtils.writeLog("蓝牙绑定.绑定异常: $e");
|
||||
}
|
||||
return ApiResponse(code: -1, msg: "未知错误".tr);
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
stopStatusPolling();
|
||||
super.onClose();
|
||||
}
|
||||
|
||||
bindDevice(Map<String, dynamic> map) {}
|
||||
|
||||
Future<ApiResponse> bindDeviceByScan(String mac) async {
|
||||
try {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "蓝牙绑定.绑定失败".tr);
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.device_bind;
|
||||
String queryUrl = "$serviceAddress$serviceName$serviceApi";
|
||||
var data = {
|
||||
"deviceType": 1,
|
||||
"mac": mac,
|
||||
};
|
||||
var response =
|
||||
await EasyDartModule.dio.post(queryUrl, data: jsonEncode(data));
|
||||
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) {
|
||||
return res;
|
||||
}
|
||||
apiResponse.msg = res.msg ?? apiResponse.msg;
|
||||
} else {
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
@@ -166,14 +266,6 @@ class BlueteethBindController extends GetControllerEx<BlueteethBindModel> {
|
||||
EasyDartModule.logger.info("蓝牙绑定.绑定异常: $e");
|
||||
DailyLogUtils.writeLog("蓝牙绑定.绑定异常: $e");
|
||||
}
|
||||
return ApiResponse(code: -1, msg: "未知错误".tr); // Default return statement
|
||||
return ApiResponse(code: -1, msg: "未知错误".tr);
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
stopStatusPolling(); // 控制器销毁时停止轮询
|
||||
super.onClose();
|
||||
}
|
||||
|
||||
bindDevice(Map<String, dynamic> map) {}
|
||||
}
|
||||
|
||||
@@ -7,9 +7,30 @@ part of 'blueteeth_bind_controller.dart';
|
||||
// **************************************************************************
|
||||
|
||||
BlueteethBindModel _$BlueteethBindModelFromJson(Map<String, dynamic> json) =>
|
||||
BlueteethBindModel()..read = (json['read'] as num).toInt();
|
||||
BlueteethBindModel()
|
||||
..read = (json['read'] as num?)?.toInt()
|
||||
..singal = (json['singal'] as num?)?.toDouble()
|
||||
..bindArr = json['bindArr'] as List<dynamic>
|
||||
..connectedWifiName = json['connectedWifiName'] as String
|
||||
..connectedRssi = (json['connectedRssi'] as num).toInt()
|
||||
..deviceName = json['deviceName'] as String
|
||||
..deviceIndex0 = json['deviceIndex0'] as bool?
|
||||
..deviceIndex1 = json['deviceIndex1'] as bool?
|
||||
..deviceIndex2 = json['deviceIndex2'] as bool?
|
||||
..wifiPassShow = json['wifiPassShow'] as bool
|
||||
..wifiPass = json['wifiPass'] as String?;
|
||||
|
||||
Map<String, dynamic> _$BlueteethBindModelToJson(BlueteethBindModel instance) =>
|
||||
<String, dynamic>{
|
||||
'read': instance.read,
|
||||
'singal': instance.singal,
|
||||
'bindArr': instance.bindArr,
|
||||
'connectedWifiName': instance.connectedWifiName,
|
||||
'connectedRssi': instance.connectedRssi,
|
||||
'deviceName': instance.deviceName,
|
||||
'deviceIndex0': instance.deviceIndex0,
|
||||
'deviceIndex1': instance.deviceIndex1,
|
||||
'deviceIndex2': instance.deviceIndex2,
|
||||
'wifiPassShow': instance.wifiPassShow,
|
||||
'wifiPass': instance.wifiPass,
|
||||
};
|
||||
|
||||
256
lib/controller/device/body_device_controller.dart
Normal file
256
lib/controller/device/body_device_controller.dart
Normal file
@@ -0,0 +1,256 @@
|
||||
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 'body_device_controller.g.dart'; // 由json_serializable自动生成的部分
|
||||
|
||||
@JsonSerializable()
|
||||
class BodyDeviceModel {
|
||||
int? type = 1; //设备类型 1:我的e护 2.云关爱
|
||||
|
||||
BodyDeviceModel();
|
||||
|
||||
// 从JSON反序列化时的异常处理
|
||||
|
||||
factory BodyDeviceModel.fromJson(Map<String, dynamic> json) {
|
||||
try {
|
||||
return _$BodyDeviceModelFromJson(json);
|
||||
} catch (e) {
|
||||
// 在实际应用中,应该有更细致的异常处理策略和错误日志
|
||||
return BodyDeviceModel(); // 或者返回一个带有错误信息的特定DeviceInfoModel实例
|
||||
}
|
||||
}
|
||||
|
||||
// 序列化为JSON时的异常处理
|
||||
Map<String, dynamic> toJson() => _$BodyDeviceModelToJson(this);
|
||||
}
|
||||
|
||||
class BodyDeviceController extends GetControllerEx<BodyDeviceModel> {
|
||||
BodyDeviceController() {
|
||||
attr = GetModel(BodyDeviceModel()).obs;
|
||||
}
|
||||
|
||||
RxInt bindDeviceNum = 0.obs;
|
||||
RxList deviceList = [].obs;
|
||||
RxMap<String, List<dynamic>> sleepReportData = <String, List<dynamic>>{}.obs;
|
||||
|
||||
RxString keyWord = "".obs;
|
||||
|
||||
Future<ApiResponse> getDeviceNum() async {
|
||||
try {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "设备.设备列表请求失败".tr);
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.device_list;
|
||||
String queryUrl =
|
||||
"${serviceAddress}${serviceName}${serviceApi}?bindNum=1";
|
||||
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) {
|
||||
bindDeviceNum.value = res.total!;
|
||||
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
|
||||
}
|
||||
|
||||
Future<ApiResponse> getDeviceList({String? key}) async {
|
||||
try {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "设备.设备列表请求失败".tr);
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.device_list;
|
||||
String queryUrl =
|
||||
"${serviceAddress}${serviceName}${serviceApi}?bindType=${model.type}${key != null ? '&key=$key' : ''}";
|
||||
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) {
|
||||
// bindDeviceNum.value = res.total!;
|
||||
deviceList.value = res.data!;
|
||||
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
|
||||
}
|
||||
|
||||
Future<ApiResponse> deleteDevice(Map<String, dynamic> device) async {
|
||||
try {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "请求失败".tr);
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.device_bind;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
final data = {
|
||||
"mac": device['mac'],
|
||||
};
|
||||
var response =
|
||||
await EasyDartModule.dio.delete(queryUrl, data: jsonEncode(data));
|
||||
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) {
|
||||
return res;
|
||||
}
|
||||
} else {
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
return apiResponse;
|
||||
} catch (e) {
|
||||
EasyDartModule.logger.info("解绑设备: $e");
|
||||
DailyLogUtils.writeLog("解绑设备: $e");
|
||||
} finally {
|
||||
EasyDartModule.logger.info("用户操作:解绑设备");
|
||||
DailyLogUtils.writeLog("用户操作:解绑设备");
|
||||
}
|
||||
return ApiResponse(code: -1, msg: "未知错误".tr); // Default return statement
|
||||
}
|
||||
|
||||
Future<ApiResponse> getSleepReport() async {
|
||||
try {
|
||||
sleepReportData.value = {};
|
||||
ApiResponse<Map<String, List<dynamic>>> apiResponse = ApiResponse(
|
||||
code: -1,
|
||||
msg: "请求失败".tr,
|
||||
);
|
||||
|
||||
if (deviceList.value.isEmpty) {
|
||||
return ApiResponse(
|
||||
code: HttpStatusCodes.ok,
|
||||
msg: "请求成功".tr,
|
||||
);
|
||||
}
|
||||
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.sleep_report;
|
||||
|
||||
for (var device in deviceList.value) {
|
||||
String mac = device['mac'] ?? "";
|
||||
if (mac.isEmpty) continue;
|
||||
|
||||
sleepReportData[mac] = []; // 初始化当前设备的数据列表
|
||||
|
||||
String queryUrl =
|
||||
"$serviceAddress$serviceName$serviceApi?mac=$mac&time=${DateTime.now().millisecondsSinceEpoch}";
|
||||
|
||||
try {
|
||||
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);
|
||||
|
||||
if (res.code == HttpStatusCodes.ok && res.data != null) {
|
||||
// 确保数据是一个列表
|
||||
if (res.data is List) {
|
||||
sleepReportData[mac] = List.from(res.data);
|
||||
} else {
|
||||
sleepReportData[mac] = [res.data];
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
EasyDartModule.logger.warning("请求设备 $mac 的睡眠数据失败: $e");
|
||||
}
|
||||
}
|
||||
if (sleepReportData.value.isNotEmpty) {
|
||||
// 遍历 Map 中的每一个键值对
|
||||
sleepReportData.value.forEach((key, report) {
|
||||
// 确保 report 列表不为空
|
||||
if (report.isNotEmpty) {
|
||||
// 获取该列表的最后一个元素
|
||||
var lastElement = report.last;
|
||||
|
||||
// 给最后一个元素添加 selected 属性
|
||||
lastElement['selected'] = true; // 假设每个元素是一个 Map 类型
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
updateAll();
|
||||
|
||||
return ApiResponse(
|
||||
code: HttpStatusCodes.ok,
|
||||
msg: "请求成功".tr,
|
||||
data: sleepReportData,
|
||||
);
|
||||
} catch (e) {
|
||||
EasyDartModule.logger.info("设备请求列表异常: $e");
|
||||
DailyLogUtils.writeLog("设备请求列表异常: $e");
|
||||
return ApiResponse(code: -1, msg: "未知错误".tr, data: {});
|
||||
}
|
||||
}
|
||||
|
||||
updateDeviceShow(device) async {
|
||||
try {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "操作失败".tr);
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.device_show;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
var data = {
|
||||
"id": device['_id'],
|
||||
"show": !device['show'],
|
||||
};
|
||||
var response =
|
||||
await EasyDartModule.dio.put(queryUrl, data: jsonEncode(data));
|
||||
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) {
|
||||
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
|
||||
}
|
||||
}
|
||||
15
lib/controller/device/body_device_controller.g.dart
Normal file
15
lib/controller/device/body_device_controller.g.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'body_device_controller.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
BodyDeviceModel _$BodyDeviceModelFromJson(Map<String, dynamic> json) =>
|
||||
BodyDeviceModel()..type = (json['type'] as num?)?.toInt();
|
||||
|
||||
Map<String, dynamic> _$BodyDeviceModelToJson(BodyDeviceModel instance) =>
|
||||
<String, dynamic>{
|
||||
'type': instance.type,
|
||||
};
|
||||
120
lib/controller/device/device_share_controller.dart
Normal file
120
lib/controller/device/device_share_controller.dart
Normal file
@@ -0,0 +1,120 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:EasyDartModule/EasyDartModule.dart';
|
||||
import 'package:ef/ef.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 'device_share_controller.g.dart'; // 由json_serializable自动生成的部分
|
||||
|
||||
@JsonSerializable()
|
||||
class DeviceShareModel {
|
||||
String? _id; // 设备类型
|
||||
String? name; // 设备类型
|
||||
int? type; // 设备类型
|
||||
String? image; // 设备类型
|
||||
DeviceShareModel();
|
||||
|
||||
// 从JSON反序列化时的异常处理
|
||||
|
||||
factory DeviceShareModel.fromJson(Map<String, dynamic> json) {
|
||||
try {
|
||||
return _$DeviceShareModelFromJson(json);
|
||||
} catch (e) {
|
||||
// 在实际应用中,应该有更细致的异常处理策略和错误日志
|
||||
return DeviceShareModel(); // 或者返回一个带有错误信息的特定DeviceInfoModel实例
|
||||
}
|
||||
}
|
||||
|
||||
// 序列化为JSON时的异常处理
|
||||
Map<String, dynamic> toJson() => _$DeviceShareModelToJson(this);
|
||||
}
|
||||
|
||||
class DeviceShareController extends GetControllerEx<DeviceShareModel> {
|
||||
DeviceShareController() {
|
||||
attr = GetModel(DeviceShareModel()).obs;
|
||||
}
|
||||
RxList deviceTypeList = [].obs;
|
||||
|
||||
RxString account = "".obs;
|
||||
RxString msg = "".obs;
|
||||
RxInt code = 0.obs;
|
||||
|
||||
Future<ApiResponse> getDeviceType() async {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "设备类型.请求失败".tr);
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.device_type;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
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);
|
||||
if (res.code != HttpStatusCodes.ok) {
|
||||
if (res.msg == null || res.msg!.isEmpty) {
|
||||
res.msg = apiResponse.msg;
|
||||
}
|
||||
} else {
|
||||
deviceTypeList.value = res.data;
|
||||
}
|
||||
return res;
|
||||
} else {
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
}
|
||||
|
||||
Future<ApiResponse> shareDevice(String mac) async {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "请求失败".tr);
|
||||
EasyDartModule.logger.info("分享设备");
|
||||
DailyLogUtils.writeLog("分享设备");
|
||||
try {
|
||||
if (account.value == null || account.value.isEmpty) {
|
||||
apiResponse.msg = "请输入手机号或者邮箱".tr;
|
||||
return apiResponse;
|
||||
}
|
||||
if (!MyUtils.isValidPhoneNumber(account.value) &&
|
||||
!MyUtils.isValidEmail(account.value)) {
|
||||
apiResponse.msg = '请输入正确的手机号或者邮箱'.tr;
|
||||
return apiResponse;
|
||||
}
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.device_share;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
var data = {"type": 1, "userName": account.value, "mac": mac};
|
||||
var response =
|
||||
await EasyDartModule.dio.post(queryUrl, data: jsonEncode(data));
|
||||
if (response != null) {
|
||||
var responseData =
|
||||
response.data is String ? jsonDecode(response.data) : response.data;
|
||||
ApiResponse res =
|
||||
ApiResponse.fromJson(responseData, (object) => object);
|
||||
if (res.code != HttpStatusCodes.ok) {
|
||||
if (res.msg == null || res.msg!.isEmpty) {
|
||||
res.msg = apiResponse.msg;
|
||||
}
|
||||
} else {
|
||||
if (res.msg == null || res.msg!.isEmpty) {
|
||||
res.msg = "操作成功".tr;
|
||||
}
|
||||
}
|
||||
msg.value = res.msg!;
|
||||
code.value = res.code!;
|
||||
updateAll();
|
||||
return res;
|
||||
} else {
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
} catch (e) {
|
||||
EasyDartModule.logger.info("分享设备失败:${e.toString()}");
|
||||
DailyLogUtils.writeLog("分享设备失败:${e.toString()}");
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
}
|
||||
}
|
||||
20
lib/controller/device/device_share_controller.g.dart
Normal file
20
lib/controller/device/device_share_controller.g.dart
Normal file
@@ -0,0 +1,20 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'device_share_controller.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
DeviceShareModel _$DeviceShareModelFromJson(Map<String, dynamic> json) =>
|
||||
DeviceShareModel()
|
||||
..name = json['name'] as String?
|
||||
..type = (json['type'] as num?)?.toInt()
|
||||
..image = json['image'] as String?;
|
||||
|
||||
Map<String, dynamic> _$DeviceShareModelToJson(DeviceShareModel instance) =>
|
||||
<String, dynamic>{
|
||||
'name': instance.name,
|
||||
'type': instance.type,
|
||||
'image': instance.image,
|
||||
};
|
||||
65
lib/controller/device/device_type_controller.dart
Normal file
65
lib/controller/device/device_type_controller.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:EasyDartModule/EasyDartModule.dart';
|
||||
import 'package:ef/ef.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/model/api_response.dart';
|
||||
|
||||
part 'device_type_controller.g.dart'; // 由json_serializable自动生成的部分
|
||||
|
||||
@JsonSerializable()
|
||||
class DeviceTypeModel {
|
||||
String? _id; // 设备类型
|
||||
String? name; // 设备类型
|
||||
int? type; // 设备类型
|
||||
String? image; // 设备类型
|
||||
DeviceTypeModel();
|
||||
|
||||
// 从JSON反序列化时的异常处理
|
||||
|
||||
factory DeviceTypeModel.fromJson(Map<String, dynamic> json) {
|
||||
try {
|
||||
return _$DeviceTypeModelFromJson(json);
|
||||
} catch (e) {
|
||||
// 在实际应用中,应该有更细致的异常处理策略和错误日志
|
||||
return DeviceTypeModel(); // 或者返回一个带有错误信息的特定DeviceInfoModel实例
|
||||
}
|
||||
}
|
||||
|
||||
// 序列化为JSON时的异常处理
|
||||
Map<String, dynamic> toJson() => _$DeviceTypeModelToJson(this);
|
||||
}
|
||||
|
||||
class DeviceTypeController extends GetControllerEx<DeviceTypeModel> {
|
||||
DeviceTypeController() {
|
||||
attr = GetModel(DeviceTypeModel()).obs;
|
||||
}
|
||||
RxList deviceTypeList = [].obs;
|
||||
|
||||
Future<ApiResponse> getDeviceType() async {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "设备类型.请求失败".tr);
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.device_type;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
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);
|
||||
if (res.code != HttpStatusCodes.ok) {
|
||||
if (res.msg == null || res.msg!.isEmpty) {
|
||||
res.msg = apiResponse.msg;
|
||||
}
|
||||
} else {
|
||||
deviceTypeList.value = res.data;
|
||||
}
|
||||
return res;
|
||||
} else {
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
}
|
||||
}
|
||||
20
lib/controller/device/device_type_controller.g.dart
Normal file
20
lib/controller/device/device_type_controller.g.dart
Normal file
@@ -0,0 +1,20 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'device_type_controller.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
DeviceTypeModel _$DeviceTypeModelFromJson(Map<String, dynamic> json) =>
|
||||
DeviceTypeModel()
|
||||
..name = json['name'] as String?
|
||||
..type = (json['type'] as num?)?.toInt()
|
||||
..image = json['image'] as String?;
|
||||
|
||||
Map<String, dynamic> _$DeviceTypeModelToJson(DeviceTypeModel instance) =>
|
||||
<String, dynamic>{
|
||||
'name': instance.name,
|
||||
'type': instance.type,
|
||||
'image': instance.image,
|
||||
};
|
||||
50
lib/controller/home/home_controller.dart
Normal file
50
lib/controller/home/home_controller.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'package:ef/ef.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:vbvs_app/controller/device/body_device_controller.dart';
|
||||
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';
|
||||
import 'package:vbvs_app/controller/user_info_controller.dart';
|
||||
|
||||
part 'home_controller.g.dart'; // 由json_serializable自动生成的部分
|
||||
|
||||
@JsonSerializable()
|
||||
class HomeModel {
|
||||
int? type = 1; //我的e护 2.云关爱
|
||||
|
||||
HomeModel();
|
||||
|
||||
// 从JSON反序列化时的异常处理
|
||||
|
||||
factory HomeModel.fromJson(Map<String, dynamic> json) {
|
||||
try {
|
||||
return _$HomeModelFromJson(json);
|
||||
} catch (e) {
|
||||
// 在实际应用中,应该有更细致的异常处理策略和错误日志
|
||||
return HomeModel(); // 或者返回一个带有错误信息的特定DeviceInfoModel实例
|
||||
}
|
||||
}
|
||||
|
||||
// 序列化为JSON时的异常处理
|
||||
Map<String, dynamic> toJson() => _$HomeModelToJson(this);
|
||||
}
|
||||
|
||||
class HomeController extends GetControllerEx<HomeModel> {
|
||||
|
||||
UserInfoController userInfoController = Get.find();
|
||||
ThemeController themeController = Get.find();
|
||||
BodyDeviceController deviceController = Get.find();
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
HomeController() {
|
||||
attr = GetModel(HomeModel()).obs;
|
||||
}
|
||||
|
||||
RxInt sleepNum = 0.obs;
|
||||
|
||||
getSleepReport() {
|
||||
sleepNum.value = 5;
|
||||
updateAll();
|
||||
}
|
||||
}
|
||||
14
lib/controller/home/home_controller.g.dart
Normal file
14
lib/controller/home/home_controller.g.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'home_controller.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
HomeModel _$HomeModelFromJson(Map<String, dynamic> json) =>
|
||||
HomeModel()..type = (json['type'] as num?)?.toInt();
|
||||
|
||||
Map<String, dynamic> _$HomeModelToJson(HomeModel instance) => <String, dynamic>{
|
||||
'type': instance.type,
|
||||
};
|
||||
@@ -12,6 +12,7 @@ import 'package:vbvs_app/common/color/app_uri_status.dart';
|
||||
import 'package:vbvs_app/common/util/MyUtils.dart';
|
||||
import 'package:vbvs_app/controller/user_info_controller.dart';
|
||||
import 'package:vbvs_app/model/api_response.dart';
|
||||
import 'package:vbvs_app/model/user_data.dart';
|
||||
part 'login_controller.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
@@ -84,10 +85,12 @@ class LoginController extends GetControllerEx<LoginModel> {
|
||||
if (res.code == HttpStatusCodes.ok) {
|
||||
UserInfoController userInfoController = Get.find();
|
||||
userInfoController.model.login = 1;
|
||||
userInfoController.model.user = UserModel.fromJson(res.data);
|
||||
String token = response.headers['token']!.first;
|
||||
EasyDartModule.dio.token = token;
|
||||
final box = GetStorage();
|
||||
box.write('token', userInfoController.model.token); // 存储 token
|
||||
box.write('token', token); // 存储 token
|
||||
box.write('user', userInfoController.model.user!.toJson()); // 存储用户信息
|
||||
}
|
||||
return res;
|
||||
} else {
|
||||
@@ -97,43 +100,48 @@ class LoginController extends GetControllerEx<LoginModel> {
|
||||
|
||||
Future<ApiResponse> getCode(BuildContext context) async {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "其他手机登录页.发送失败".tr);
|
||||
if (model.register_agree == null || model.register_agree != true) {
|
||||
apiResponse.msg = "登录页.未同意协议".tr;
|
||||
return apiResponse;
|
||||
}
|
||||
if (model.phone == null || model.phone!.isEmpty) {
|
||||
apiResponse.msg = "其他手机登录页.请输入手机号".tr;
|
||||
return apiResponse;
|
||||
}
|
||||
if (!MyUtils.isValidPhoneNumber(model.phone!) &&
|
||||
!MyUtils.isValidEmail(model.phone!)) {
|
||||
apiResponse.msg = '其他手机登录页.不正确手机号'.tr;
|
||||
return apiResponse;
|
||||
}
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.send_code;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
var data = {
|
||||
"userName": model.phone,
|
||||
};
|
||||
var response =
|
||||
await EasyDartModule.dio.post(queryUrl, data: jsonEncode(data));
|
||||
if (response != null) {
|
||||
var responseData =
|
||||
response.data is String ? jsonDecode(response.data) : response.data;
|
||||
ApiResponse res = ApiResponse.fromJson(responseData, (object) => object);
|
||||
if (res.code != HttpStatusCodes.ok) {
|
||||
if (res.msg == null || res.msg!.isEmpty) {
|
||||
res.msg = apiResponse.msg;
|
||||
}
|
||||
} else {
|
||||
if (res.msg == null || res.msg!.isEmpty) {
|
||||
res.msg = "其他手机登录页.发送成功".tr;
|
||||
}
|
||||
try {
|
||||
if (model.register_agree == null || model.register_agree != true) {
|
||||
apiResponse.msg = "登录页.未同意协议".tr;
|
||||
return apiResponse;
|
||||
}
|
||||
return res;
|
||||
} else {
|
||||
if (model.phone == null || model.phone!.isEmpty) {
|
||||
apiResponse.msg = "其他手机登录页.请输入手机号".tr;
|
||||
return apiResponse;
|
||||
}
|
||||
if (!MyUtils.isValidPhoneNumber(model.phone!) &&
|
||||
!MyUtils.isValidEmail(model.phone!)) {
|
||||
apiResponse.msg = '其他手机登录页.不正确手机号'.tr;
|
||||
return apiResponse;
|
||||
}
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.send_code;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
var data = {
|
||||
"userName": model.phone,
|
||||
};
|
||||
var response =
|
||||
await EasyDartModule.dio.post(queryUrl, data: jsonEncode(data));
|
||||
if (response != null) {
|
||||
var responseData =
|
||||
response.data is String ? jsonDecode(response.data) : response.data;
|
||||
ApiResponse res =
|
||||
ApiResponse.fromJson(responseData, (object) => object);
|
||||
if (res.code != HttpStatusCodes.ok) {
|
||||
if (res.msg == null || res.msg!.isEmpty) {
|
||||
res.msg = apiResponse.msg;
|
||||
}
|
||||
} else {
|
||||
if (res.msg == null || res.msg!.isEmpty) {
|
||||
res.msg = "其他手机登录页.发送成功".tr;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
} else {
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
} catch (e) {
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,8 @@ LoginModel _$LoginModelFromJson(Map<String, dynamic> json) => LoginModel()
|
||||
..showPd = json['showPd'] as bool?
|
||||
..forceLogin = (json['forceLogin'] as num?)?.toInt()
|
||||
..isIos = json['isIos'] as bool?
|
||||
..isWeChatNotInstalled = json['isWeChatNotInstalled'] as bool?;
|
||||
..isWeChatNotInstalled = json['isWeChatNotInstalled'] as bool?
|
||||
..register_agree = json['register_agree'] as bool?;
|
||||
|
||||
Map<String, dynamic> _$LoginModelToJson(LoginModel instance) =>
|
||||
<String, dynamic>{
|
||||
@@ -30,4 +31,5 @@ Map<String, dynamic> _$LoginModelToJson(LoginModel instance) =>
|
||||
'forceLogin': instance.forceLogin,
|
||||
'isIos': instance.isIos,
|
||||
'isWeChatNotInstalled': instance.isWeChatNotInstalled,
|
||||
'register_agree': instance.register_agree,
|
||||
};
|
||||
|
||||
78
lib/controller/message/message_controller.dart
Normal file
78
lib/controller/message/message_controller.dart
Normal file
@@ -0,0 +1,78 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
15
lib/controller/message/message_controller.g.dart
Normal file
15
lib/controller/message/message_controller.g.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'message_controller.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
MessageModel _$MessageModelFromJson(Map<String, dynamic> json) =>
|
||||
MessageModel()..type = (json['type'] as num?)?.toInt();
|
||||
|
||||
Map<String, dynamic> _$MessageModelToJson(MessageModel instance) =>
|
||||
<String, dynamic>{
|
||||
'type': instance.type,
|
||||
};
|
||||
@@ -1,14 +1,20 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:EasyDartModule/EasyDartModule.dart';
|
||||
import 'package:ef/ef.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 'person_controller.g.dart'; // 由json_serializable自动生成的部分
|
||||
|
||||
@JsonSerializable()
|
||||
class PersonModel {
|
||||
int read = 1;
|
||||
|
||||
DateTime? birthday;
|
||||
double? weight;
|
||||
int? read = 1;
|
||||
// String? name;
|
||||
|
||||
PersonModel();
|
||||
|
||||
@@ -30,5 +36,152 @@ class PersonModel {
|
||||
class PersonController extends GetControllerEx<PersonModel> {
|
||||
PersonController() {
|
||||
attr = GetModel(PersonModel()).obs;
|
||||
}
|
||||
}
|
||||
}
|
||||
RxList selectedDiseaseIds = [].obs;
|
||||
// RxList diseaseList = [
|
||||
// {'id': 1, 'name': '高血压'},
|
||||
// {'id': 2, 'name': '糖尿病'},
|
||||
// {'id': 3, 'name': '冠心病'},
|
||||
// {'id': 4, 'name': '哮喘'},
|
||||
// {'id': 5, 'name': '脑卒中'},
|
||||
// {'id': 6, 'name': '慢性肾病'},
|
||||
// {'id': 7, 'name': '慢阻肺'},
|
||||
// {'id': 8, 'name': '类风湿关节炎'},
|
||||
// {'id': 9, 'name': '类风湿关节炎类风湿关节炎'},
|
||||
// {'id': 10, 'name': '类风湿关节炎类风湿关节炎类风湿关节炎'},
|
||||
// ].obs;
|
||||
|
||||
RxString currentPersonId = "".obs;
|
||||
RxString name = "".obs;
|
||||
RxInt gender = 1.obs;
|
||||
RxString birthday = "".obs;
|
||||
RxInt weight = 65.obs;
|
||||
DateTime? dateTime; //选择时间
|
||||
RxList diseaseList = [].obs;
|
||||
|
||||
//保存人员资料
|
||||
// void savePersonData() {
|
||||
// print("id->" + currentPersonId.value);
|
||||
// print("name->" + name.value);
|
||||
// print("gender->${gender.value}");
|
||||
// print("生日->${birthday.value}");
|
||||
// print("体重->${weight.value}");
|
||||
// print("慢病->${selectedDiseaseIds.value}");
|
||||
// }
|
||||
|
||||
Future<ApiResponse> savePersonData() async {
|
||||
try {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "蓝牙绑定.绑定失败".tr);
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.person_info;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
if (name.value.isEmpty) {
|
||||
apiResponse.msg = "请输入姓名".tr;
|
||||
return apiResponse;
|
||||
}
|
||||
if (birthday.value.isEmpty) {
|
||||
apiResponse.msg = "请选择生日".tr;
|
||||
return apiResponse;
|
||||
}
|
||||
if (weight.value == 0) {
|
||||
apiResponse.msg = "请输入体重".tr;
|
||||
return apiResponse;
|
||||
}
|
||||
|
||||
var data = {
|
||||
"id": currentPersonId.value,
|
||||
"name": name.value,
|
||||
"gender": gender.value,
|
||||
"birthday": birthday.value,
|
||||
"weight": weight.value,
|
||||
"disease": selectedDiseaseIds.value,
|
||||
};
|
||||
var response =
|
||||
await EasyDartModule.dio.put(queryUrl, data: jsonEncode(data));
|
||||
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) {
|
||||
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
|
||||
}
|
||||
|
||||
Future<ApiResponse> updatePersonName(person, deviceId) async {
|
||||
try {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "请求失败".tr);
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.person_info;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
if (name.value.isEmpty) {
|
||||
apiResponse.msg = "请输入姓名".tr;
|
||||
return apiResponse;
|
||||
}
|
||||
person['name'] = name.value;
|
||||
person['id'] = deviceId;
|
||||
var response =
|
||||
await EasyDartModule.dio.put(queryUrl, data: jsonEncode(person));
|
||||
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) {
|
||||
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
|
||||
}
|
||||
|
||||
Future<ApiResponse> getDiseaseData() async {
|
||||
try {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "请求失败".tr);
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.disease_list;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
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) {
|
||||
// bindDeviceNum.value = res.total!;
|
||||
diseaseList.value = res.data!;
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,14 +6,10 @@ part of 'person_controller.dart';
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
PersonModel _$PersonModelFromJson(Map<String, dynamic> json) => PersonModel()
|
||||
..read = (json['read'] as num).toInt()
|
||||
..birthday = json['birthday'] == null
|
||||
? null
|
||||
: DateTime.parse(json['birthday'] as String);
|
||||
PersonModel _$PersonModelFromJson(Map<String, dynamic> json) =>
|
||||
PersonModel()..read = (json['read'] as num?)?.toInt();
|
||||
|
||||
Map<String, dynamic> _$PersonModelToJson(PersonModel instance) =>
|
||||
<String, dynamic>{
|
||||
'read': instance.read,
|
||||
'birthday': instance.birthday?.toIso8601String(),
|
||||
};
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
import 'package:EasyDartModule/EasyDartModule.dart';
|
||||
import 'package:dio/dio.dart' as dio;
|
||||
import 'package:ef/ef.dart';
|
||||
import 'package:flutterflow_ui/flutterflow_ui.dart';
|
||||
import 'package:get_storage/get_storage.dart';
|
||||
import 'package:img_picker/img_picker.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/controller/device/body_device_controller.dart';
|
||||
import 'package:vbvs_app/controller/home/home_controller.dart';
|
||||
import 'package:vbvs_app/model/api_response.dart';
|
||||
import 'package:vbvs_app/model/user_data.dart';
|
||||
|
||||
part 'user_info_controller.g.dart';
|
||||
@@ -24,11 +36,12 @@ class UserInfoModel {
|
||||
String? img_bucket = 'user';
|
||||
int? login = 0; //0未登录 1 登录
|
||||
|
||||
|
||||
|
||||
int? deviceBindNum = 0; //绑定设备数量
|
||||
|
||||
int? loginPhone = 0;//0 本机号码 1其他手机号
|
||||
int? loginPhone = 0; //0 本机号码 1其他手机号
|
||||
|
||||
// String? nickname; //修改昵称
|
||||
// String? avatar; //修改头像地址
|
||||
|
||||
UserInfoModel();
|
||||
static UserInfoModel fromJson(Map<String, dynamic> json) =>
|
||||
@@ -41,4 +54,146 @@ class UserInfoController extends GetControllerEx<UserInfoModel> {
|
||||
UserInfoController() {
|
||||
attr = GetModel(UserInfoModel()).obs;
|
||||
}
|
||||
|
||||
Future<ApiResponse> uploadImg() async {
|
||||
EasyDartModule.logger.info("请求上传图片");
|
||||
DailyLogUtils.writeLog("请求上传图片");
|
||||
final ImagePicker picker = ImagePicker();
|
||||
final XFile? image = await picker.pickImage(source: ImageSource.gallery);
|
||||
try {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "我的.头像上传失败".tr);
|
||||
if (image != null) {
|
||||
int fileSize = await image.length(); // 获取图片大小,单位为字节
|
||||
if (fileSize > 1048576) {
|
||||
apiResponse.msg = "我的.头像限制".tr;
|
||||
return apiResponse;
|
||||
}
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.upload_file;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
var formData = dio.FormData.fromMap({
|
||||
"type": 1,
|
||||
"file": await dio.MultipartFile.fromFile(
|
||||
image.path, // 确保 image 是 File 类型
|
||||
filename: image.path.split('/').last,
|
||||
),
|
||||
});
|
||||
var response = await EasyDartModule.dio.post(queryUrl, data: formData);
|
||||
if (response != null) {
|
||||
var responseData = response.data is String
|
||||
? jsonDecode(response.data)
|
||||
: response.data;
|
||||
ApiResponse res =
|
||||
ApiResponse.fromJson(responseData, (object) => object);
|
||||
MyUtils.formatResponse(apiResponse, "我的.上传成功".tr, "我的.头像上传失败".tr);
|
||||
model.user!.tmpHead = res.data['path'];
|
||||
updateAll();
|
||||
return res;
|
||||
} else {
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
} else {
|
||||
apiResponse.msg = "我的.未选择图片".tr;
|
||||
return apiResponse;
|
||||
}
|
||||
} catch (e) {
|
||||
EasyDartModule.logger.error("上传图片失败->$e");
|
||||
DailyLogUtils.writeError("上传图片失败->$e");
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
}
|
||||
|
||||
//更新用户信息
|
||||
Future<ApiResponse> updateData() async {
|
||||
EasyDartModule.logger.info("更新用户资料");
|
||||
DailyLogUtils.writeLog("更新用户资料");
|
||||
try {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "我的.保存失败".tr);
|
||||
UserModel user = model.user!;
|
||||
if (user.tmpNickName == null || user.tmpNickName!.isEmpty) {
|
||||
apiResponse.msg = "我的.昵称为空".tr;
|
||||
return apiResponse;
|
||||
}
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.user_info;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
final data = {
|
||||
"nickName": user.tmpNickName,
|
||||
if (user.tmpHead != null && user.tmpHead!.isNotEmpty)
|
||||
"avatar": user.tmpHead,
|
||||
};
|
||||
|
||||
var response =
|
||||
await EasyDartModule.dio.put(queryUrl, data: jsonEncode(data));
|
||||
if (apiResponse != null) {
|
||||
var responseData =
|
||||
response.data is String ? jsonDecode(response.data) : response.data;
|
||||
ApiResponse res =
|
||||
ApiResponse.fromJson(responseData, (object) => object);
|
||||
MyUtils.formatResponse(res, "我的.保存成功".tr, "我的.保存失败".tr);
|
||||
return res;
|
||||
} else {
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
} catch (e) {
|
||||
EasyDartModule.logger.info("更新用户资料失败->$e");
|
||||
DailyLogUtils.writeLog("更新用户资料失败->$e");
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
}
|
||||
|
||||
//查询用户信息
|
||||
Future<ApiResponse> getUserInfo() async {
|
||||
EasyDartModule.logger.info("查询用户资料");
|
||||
DailyLogUtils.writeLog("查询用户资料");
|
||||
try {
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "我的.查询失败".tr);
|
||||
String serviceAddress = ServiceConstant.service_address;
|
||||
String serviceName = ServiceConstant.server_service;
|
||||
String serviceApi = ServiceConstant.user_info;
|
||||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||||
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(apiResponse, "我的.保存成功".tr, "我的.保存失败".tr);
|
||||
if (res.code == HttpStatusCodes.ok) {
|
||||
UserInfoController userInfoController = Get.find();
|
||||
userInfoController.model.user = UserModel.fromJson(res.data);
|
||||
final box = GetStorage();
|
||||
box.write('user', userInfoController.model.user!.toJson()); // 存储用户信息
|
||||
userInfoController.updateAll();
|
||||
}
|
||||
return apiResponse;
|
||||
} else {
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
} catch (e) {
|
||||
EasyDartModule.logger.info("更新用户资料失败->$e");
|
||||
DailyLogUtils.writeLog("更新用户资料失败->$e");
|
||||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||||
}
|
||||
}
|
||||
|
||||
ApiResponse logOut() {
|
||||
ApiResponse apiResponse = ApiResponse(code: 1, msg: "设置页.退出成功".tr);
|
||||
EasyDartModule.logger.info("退出登录");
|
||||
DailyLogUtils.writeLog("退出登录");
|
||||
model.login = 0;
|
||||
EasyDartModule.dio.token = null;
|
||||
final box = GetStorage();
|
||||
box.remove("user");
|
||||
box.remove("token");
|
||||
BodyDeviceController bodyDeviceController = Get.find();
|
||||
bodyDeviceController.bindDeviceNum.value = 0;
|
||||
HomeController homeController = Get.find();
|
||||
homeController.sleepNum.value = 0;
|
||||
|
||||
bodyDeviceController.sleepReportData.value = {};
|
||||
return apiResponse;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,9 @@ UserInfoModel _$UserInfoModelFromJson(Map<String, dynamic> json) =>
|
||||
..deviceModel = json['deviceModel'] as String?
|
||||
..appVersion = json['appVersion'] as String?
|
||||
..img_bucket = json['img_bucket'] as String?
|
||||
..login = (json['login'] as num?)?.toInt();
|
||||
..login = (json['login'] as num?)?.toInt()
|
||||
..deviceBindNum = (json['deviceBindNum'] as num?)?.toInt()
|
||||
..loginPhone = (json['loginPhone'] as num?)?.toInt();
|
||||
|
||||
Map<String, dynamic> _$UserInfoModelToJson(UserInfoModel instance) =>
|
||||
<String, dynamic>{
|
||||
@@ -33,4 +35,6 @@ Map<String, dynamic> _$UserInfoModelToJson(UserInfoModel instance) =>
|
||||
'appVersion': instance.appVersion,
|
||||
'img_bucket': instance.img_bucket,
|
||||
'login': instance.login,
|
||||
'deviceBindNum': instance.deviceBindNum,
|
||||
'loginPhone': instance.loginPhone,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user