Files
tuiche/lib/pages/xiaoe/xiaoe_page.dart
2026-02-10 14:30:16 +08:00

416 lines
15 KiB
Dart

import 'dart:convert';
import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:vbvs_app/common/color/ServiceConstant.dart';
import 'package:vbvs_app/common/color/appConstants.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/common/util/requestWithLog.dart';
import 'package:vbvs_app/controller/device/blueteeth_bind_controller.dart';
import 'package:vbvs_app/controller/device/device_type_controller.dart';
import 'package:vbvs_app/controller/main_bottom/global_controller.dart';
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';
import 'package:vbvs_app/controller/user_info_controller.dart';
import 'package:vbvs_app/enum/APPPackageType.dart';
import 'package:EasyDartModule/EasyDartModule.dart' as edm;
// class XiaoEPage extends StatefulWidget {
// var sleepUri;
// XiaoEPage({super.key, required this.sleepUri});
// @override
// State<XiaoEPage> createState() => _XiaoEPageState();
// }
// class _XiaoEPageState extends State<XiaoEPage> {
// GlobalController globalController = Get.find();
// UserInfoController userInfoController = Get.find();
// BlueteethBindController blueteethBindController = Get.find();
// ThemeController themeController = Get.find();
// DeviceTypeController deviceTypeController = Get.find();
// ValueNotifier<bool> isPageLoading = ValueNotifier<bool>(true);
// RxList deviceList = [].obs;
// RxString finalUri = RxString('');
// @override
// void initState() {
// super.initState();
// getDeviceList();
// }
// @override
// void dispose() {
// // 清理 ValueNotifier
// isPageLoading.dispose();
// super.dispose();
// }
// @override
// Widget build(BuildContext context) {
// return LayoutBuilder(
// builder: (context, bodySize) => GestureDetector(
// // onTap: () => FocusScope.of(context).unfocus(),,
// child: Container(
// decoration: BoxDecoration(
// image: DecorationImage(
// // image: AssetImage(getBackgroundImageNoImage()), // 本地图片
// image: AssetImage('assets/images/new_background.png'), // 本地图片
// fit: BoxFit.fill, // 填满整个 Container
// ),
// ),
// child: Scaffold(
// backgroundColor: Colors.transparent, // 背景透明
// appBar: AppBar(
// // backgroundColor: themeController.currentColor.sc17,
// backgroundColor: Colors.transparent,
// automaticallyImplyLeading: false,
// iconTheme: IconThemeData(color: themeController.currentColor.sc3),
// titleSpacing: 0,
// title: Container(
// width: double.infinity,
// height: 180.rpx,
// child: Stack(
// alignment: Alignment.center,
// children: [
// /// 居中标题
// Text(
// '糖管家'.tr,
// style: TextStyle(
// fontFamily: 'Readex Pro',
// color: themeController.currentColor.sc3,
// letterSpacing: 0,
// fontSize: 30.rpx,
// ),
// ),
// /// 左边返回按钮
// Positioned(
// left: 0,
// child: returnIconButtomNew(),
// ),
// ],
// ),
// ),
// ),
// body: SafeArea(
// top: true,
// child: Obx(() {
// String sleepUri = finalUri.value;
// ef.log('finalUri: $sleepUri');
// return Stack(
// children: [
// InAppWebView(
// key: UniqueKey(),
// initialUrlRequest: URLRequest(url: WebUri(sleepUri)),
// onLoadStart: (controller, url) {
// // 页面开始加载时显示加载指示器
// isPageLoading.value = true;
// },
// onLoadStop: (controller, url) {
// // 页面加载完成后隐藏加载指示器
// isPageLoading.value = false;
// },
// initialOptions: InAppWebViewGroupOptions(
// crossPlatform: InAppWebViewOptions(
// transparentBackground: true, // ✅ 背景透明
// ),
// ),
// ),
// ValueListenableBuilder<bool>(
// valueListenable: isPageLoading,
// builder: (context, isLoading, child) {
// return isLoading
// ? Center(
// child: CircularProgressIndicator(
// strokeWidth: 2,
// valueColor: AlwaysStoppedAnimation<Color>(
// themeController.currentColor.sc1,
// ),
// ), // 加载指示器
// )
// : SizedBox.shrink();
// },
// ),
// ],
// );
// }),
// ),
// ),
// ),
// ),
// );
// }
// Future<void> getDeviceList() async {
// String serviceAddress = ServiceConstant.service_address;
// String serviceName = ServiceConstant.server_service;
// String serviceApi = ServiceConstant.personnel_info;
// // 初始URL
// String queryUrl = "$serviceAddress$serviceName$serviceApi";
// await requestWithLog(
// logTitle: "请求人员信息列表".tr,
// method: MyHttpMethod.get,
// queryUrl: queryUrl,
// onSuccess: (res) {
// List<dynamic> rawList = res.data ?? [];
// List<Map<String, dynamic>> newList = rawList.map((item) {
// String mac = item['mac'] ?? '';
// String name = (item['person'] != null &&
// item['person']['name'] != null &&
// item['person']['name'].toString().trim().isNotEmpty)
// ? item['person']['name'] + "_${mac}"
// : '未命名'.tr + "_${mac}";
// return {
// 'mac': mac,
// 'name': name,
// };
// }).toList();
// deviceList.value = newList;
// // 拼接参数 person
// if (deviceList.isNotEmpty) {
// // JSON 编码整个 deviceList 对象数组
// String personParam = Uri.encodeComponent(jsonEncode(deviceList));
// finalUri.value = "${widget.sleepUri}?person=$personParam";
// } else {
// finalUri.value = widget.sleepUri;
// }
// },
// onFailure: (res) {
// edm.EasyDartModule.logger.warning("请求人员信息列表失败: ${res.msg}");
// },
// );
// String? language = "";
// if (AppConstants().ent_type == APPPackageType.MHT.code) {
// if (mhLanguageController.selectLanguage != null) {
// language = mhLanguageController.selectLanguage.value!.language_code;
// }
// } else {
// if (languageController.selectLanguage != null) {
// language = languageController.selectLanguage.value!.language_code;
// }
// }
// if (language != null && language.isNotEmpty) {
// if (finalUri.value.contains("?")) {
// finalUri.value += "&lang=$language";
// } else {
// finalUri.value += "?lang=$language";
// }
// }
// if (finalUri.value.contains("?")) {
// finalUri.value += "&code=mht";
// } else {
// finalUri.value += "?code=mht";
// }
// ef.log("msg");
// edm.EasyDartModule.logger.info("finalUri: ${finalUri.value}");
// ef.log("finalUri: ${finalUri.value}");
// }
// }
class XiaoEPage extends StatefulWidget {
final String sleepUri;
const XiaoEPage({super.key, required this.sleepUri});
@override
State<XiaoEPage> createState() => _XiaoEPageState();
}
class _XiaoEPageState extends State<XiaoEPage>
with AutomaticKeepAliveClientMixin {
// ✅ 保持页面状态
GlobalController globalController = Get.find();
UserInfoController userInfoController = Get.find();
BlueteethBindController blueteethBindController = Get.find();
ThemeController themeController = Get.find();
DeviceTypeController deviceTypeController = Get.find();
ValueNotifier<bool> isPageLoading = ValueNotifier<bool>(true);
RxList deviceList = [].obs;
RxString finalUri = RxString('');
@override
bool get wantKeepAlive => true; // ✅ 开启页面缓存
@override
void initState() {
super.initState();
getDeviceList();
}
@override
void dispose() {
isPageLoading.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
super.build(context); // ✅ 必须调用,保证 keepAlive 生效
return LayoutBuilder(
builder: (context, bodySize) => GestureDetector(
child: Container(
decoration: BoxDecoration(
image: const DecorationImage(
image: AssetImage('assets/images/new_background.png'),
fit: BoxFit.fill,
),
),
child: Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
backgroundColor: Colors.transparent,
automaticallyImplyLeading: false,
iconTheme: IconThemeData(color: themeController.currentColor.sc3),
titleSpacing: 0,
title: SizedBox(
width: double.infinity,
height: 180.rpx,
child: Stack(
alignment: Alignment.center,
children: [
Text(
'糖管家'.tr,
style: TextStyle(
fontFamily: 'Readex Pro',
color: themeController.currentColor.sc3,
letterSpacing: 0,
fontSize: 30.rpx,
),
),
Positioned(
left: 0,
child: returnIconButtomNew(),
),
],
),
),
),
body: SafeArea(
top: true,
child: Obx(() {
String sleepUri = finalUri.value;
ef.log('finalUri: $sleepUri');
if (sleepUri.isEmpty) {
return Center(
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(
themeController.currentColor.sc1,
),
),
);
}
return Stack(
children: [
InAppWebView(
key: UniqueKey(),
initialUrlRequest: URLRequest(url: WebUri(sleepUri)),
onLoadStart: (controller, url) {
isPageLoading.value = true;
},
onLoadStop: (controller, url) {
isPageLoading.value = false;
},
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
transparentBackground: true,
),
),
),
ValueListenableBuilder<bool>(
valueListenable: isPageLoading,
builder: (context, isLoading, child) {
return isLoading
? Center(
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(
themeController.currentColor.sc1,
),
),
)
: const SizedBox.shrink();
},
),
],
);
}),
),
),
),
),
);
}
Future<void> getDeviceList() async {
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.personnel_info;
String queryUrl = "$serviceAddress$serviceName$serviceApi";
await requestWithLog(
logTitle: "请求人员信息列表".tr,
method: MyHttpMethod.get,
queryUrl: queryUrl,
onSuccess: (res) {
List<dynamic> rawList = res.data ?? [];
List<Map<String, dynamic>> newList = rawList.map((item) {
String mac = item['mac'] ?? '';
String name = (item['name'] != null &&
item['name'].toString().trim().isNotEmpty)
? item['name'] + "_$mac"
: '未命名'.tr + "_$mac";
return {'mac': mac, 'name': name};
}).toList();
deviceList.value = newList;
if (deviceList.isNotEmpty) {
String personParam = Uri.encodeComponent(jsonEncode(deviceList));
finalUri.value = "${widget.sleepUri}?person=$personParam";
} else {
finalUri.value = widget.sleepUri;
}
},
onFailure: (res) {
edm.EasyDartModule.logger.warning("请求人员信息列表失败: ${res.msg}");
},
);
String? language = "";
if (AppConstants().ent_type == APPPackageType.MHT.code) {
if (mhLanguageController.selectLanguage != null) {
language = mhLanguageController.selectLanguage.value!.language_code;
}
} else {
if (languageController.selectLanguage != null) {
language = languageController.selectLanguage.value!.language_code;
}
}
if (language != null && language.isNotEmpty) {
if (finalUri.value.contains("?")) {
finalUri.value += "&lang=$language";
} else {
finalUri.value += "?lang=$language";
}
}
if (finalUri.value.contains("?")) {
finalUri.value += "&code=mht";
} else {
finalUri.value += "?code=mht";
}
edm.EasyDartModule.logger.info("finalUri: ${finalUri.value}");
ef.log("finalUri: ${finalUri.value}");
}
}