更新睡眠报告时区显示问题。
This commit is contained in:
@@ -5,6 +5,7 @@ import 'package:vbvs_app/common/color/ServiceConstant.dart';
|
||||
import 'package:vbvs_app/common/util/MyUtils.dart';
|
||||
import 'package:vbvs_app/common/util/requestWithLog.dart';
|
||||
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
|
||||
import 'package:vbvs_app/controller/sleep/sleep_time_convert.dart';
|
||||
import 'package:vbvs_app/pages/main_bottom/component/main_page_b_bottom_change.dart';
|
||||
|
||||
part 'sleep_report_controller.g.dart'; // 由json_serializable自动生成的部分
|
||||
@@ -65,10 +66,23 @@ class SleepReportController extends GetControllerEx<SleepReportModel> {
|
||||
logTitle: "查询睡眠报告",
|
||||
method: MyHttpMethod.get,
|
||||
queryUrl: queryUrl,
|
||||
// onSuccess: (res) {
|
||||
// isLoading.value = false;
|
||||
// print(res);
|
||||
// final Map<String, dynamic> data = res.data;
|
||||
// sleepReport.value = res.data;
|
||||
// updateAll();
|
||||
// },
|
||||
onSuccess: (res) {
|
||||
isLoading.value = false;
|
||||
print(res);
|
||||
sleepReport.value = res.data;
|
||||
|
||||
// 获取原始数据
|
||||
final sleepData = Map<String, dynamic>.from(res.data);
|
||||
// 处理时区转换
|
||||
applyTimezoneConversion(sleepData);
|
||||
|
||||
sleepReport.value = sleepData;
|
||||
updateAll();
|
||||
},
|
||||
onFailure: (res) {
|
||||
|
||||
107
lib/controller/sleep/sleep_time_convert.dart
Normal file
107
lib/controller/sleep/sleep_time_convert.dart
Normal file
@@ -0,0 +1,107 @@
|
||||
// 时区转换函数
|
||||
void applyTimezoneConversion(Map<String, dynamic> data) {
|
||||
final dynamic timeZoneValue = data['utc'];
|
||||
if (timeZoneValue == null) return;
|
||||
|
||||
// 解析目标时区
|
||||
final int? targetOffset = _parseTimezone(timeZoneValue);
|
||||
if (targetOffset == null) return;
|
||||
|
||||
// 原始数据是UTC+8的时间戳
|
||||
const int sourceOffset = 8; // UTC+8
|
||||
final int diffHours = targetOffset - sourceOffset;
|
||||
final int diffMs = diffHours * 3600 * 1000;
|
||||
|
||||
// 转换startTime和endTime
|
||||
if (data['startTime'] != null && data['startTime'] is int) {
|
||||
data['startTime'] = data['startTime'] + diffMs;
|
||||
}
|
||||
if (data['endTime'] != null && data['endTime'] is int) {
|
||||
data['endTime'] = data['endTime'] + diffMs;
|
||||
}
|
||||
|
||||
// 递归转换所有时间戳,但要跳过hrsp数组
|
||||
_convertTimestampsRecursive(data, diffMs, skipField: 'hrsp');
|
||||
}
|
||||
|
||||
// 解析时区值 - 支持多种类型
|
||||
int? _parseTimezone(dynamic timeZoneValue) {
|
||||
if (timeZoneValue == null) return null;
|
||||
|
||||
// 如果是整数类型
|
||||
if (timeZoneValue is int) {
|
||||
return timeZoneValue;
|
||||
}
|
||||
|
||||
// 如果是字符串类型
|
||||
if (timeZoneValue is String) {
|
||||
// 移除空白字符
|
||||
final String trimmed = timeZoneValue.trim();
|
||||
|
||||
// 处理纯数字字符串,如 "9"、"-2"
|
||||
if (RegExp(r'^[+-]?\d+$').hasMatch(trimmed)) {
|
||||
return int.parse(trimmed);
|
||||
}
|
||||
|
||||
// 处理带UTC前缀的字符串,如 "utc+9"、"UTC-2"
|
||||
final match =
|
||||
RegExp(r'utc([+-]?\d+)', caseSensitive: false).firstMatch(trimmed);
|
||||
if (match != null) {
|
||||
return int.parse(match.group(1)!);
|
||||
}
|
||||
|
||||
// 处理其他格式,如 "+9"、"-2"
|
||||
if (trimmed.startsWith('+') || trimmed.startsWith('-')) {
|
||||
return int.parse(trimmed);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果是数字类型(double等)
|
||||
if (timeZoneValue is num) {
|
||||
return timeZoneValue.toInt();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// 递归转换所有时间戳,可指定跳过的字段
|
||||
void _convertTimestampsRecursive(dynamic data, int diffMs, {String? skipField}) {
|
||||
if (data is Map) {
|
||||
// 如果不是跳过的字段类型,转换时间戳
|
||||
if (!(skipField != null && data.containsKey(skipField))) {
|
||||
_convertMapTimestamps(data, diffMs);
|
||||
}
|
||||
|
||||
// 递归处理所有子元素
|
||||
data.forEach((key, value) {
|
||||
if (value is Map || value is List) {
|
||||
// 如果当前key是要跳过的字段,则不深入处理其内容
|
||||
if (skipField != null && key == skipField) {
|
||||
return; // 跳过hrsp数组的处理
|
||||
}
|
||||
_convertTimestampsRecursive(value, diffMs, skipField: skipField);
|
||||
}
|
||||
});
|
||||
} else if (data is List) {
|
||||
for (final item in data) {
|
||||
if (item is Map || item is List) {
|
||||
_convertTimestampsRecursive(item, diffMs, skipField: skipField);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 转换map中的时间戳字段
|
||||
void _convertMapTimestamps(Map map, int diffMs) {
|
||||
// 要转换的字段名
|
||||
const timestampFields = ['startTime', 'endTime', 'st', 'et'];
|
||||
|
||||
for (final field in timestampFields) {
|
||||
if (map.containsKey(field) && map[field] != null) {
|
||||
final value = map[field];
|
||||
if (value is int) {
|
||||
map[field] = value + diffMs;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user