更新沃棣背景适配
This commit is contained in:
@@ -59,7 +59,7 @@
|
||||
|
||||
// final backgroundImage = showNoLoginView
|
||||
// ? 'assets/img/xiaoe.png' // 无设备或无登录时的背景
|
||||
// : 'assets/img/bgNoImg.png';
|
||||
// : getBackgroundImageNoImage();
|
||||
|
||||
// return Stack(
|
||||
// children: [
|
||||
@@ -257,7 +257,7 @@ class _EPageState extends State<EPage> with AutomaticKeepAliveClientMixin {
|
||||
ValueNotifier<String> serverError = ValueNotifier<String>('');
|
||||
RxList deviceList = [].obs;
|
||||
RxString finalUri = RxString('');
|
||||
|
||||
|
||||
// 本地服务器相关
|
||||
HttpServer? _localServer;
|
||||
int _serverPort = 0;
|
||||
@@ -294,7 +294,7 @@ class _EPageState extends State<EPage> with AutomaticKeepAliveClientMixin {
|
||||
try {
|
||||
isServerStarting.value = true;
|
||||
serverError.value = '';
|
||||
|
||||
|
||||
// 1. 从 assets 加载 ZIP 文件
|
||||
final ByteData zipData;
|
||||
try {
|
||||
@@ -303,48 +303,47 @@ class _EPageState extends State<EPage> with AutomaticKeepAliveClientMixin {
|
||||
serverError.value = '找不到小e资源文件: $e';
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
final Uint8List zipBytes = zipData.buffer.asUint8List();
|
||||
|
||||
|
||||
// 2. 创建临时目录并解压 ZIP
|
||||
_tempExtractDir = await Directory.systemTemp.createTemp('xiaoe_web_');
|
||||
final bool extractSuccess = await _extractZipToDirectory(zipBytes, _tempExtractDir!);
|
||||
|
||||
final bool extractSuccess =
|
||||
await _extractZipToDirectory(zipBytes, _tempExtractDir!);
|
||||
|
||||
if (!extractSuccess) {
|
||||
serverError.value = '解压小e资源文件失败';
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// 3. 查找入口文件 (index.html)
|
||||
final File? entryFile = await _findEntryFile(_tempExtractDir!);
|
||||
if (entryFile == null) {
|
||||
serverError.value = '找不到入口文件 (index.html)';
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// 4. 创建静态文件处理器
|
||||
final staticHandler = createStaticHandler(
|
||||
_tempExtractDir!.path,
|
||||
defaultDocument: 'index.html',
|
||||
serveFilesOutsidePath: false,
|
||||
);
|
||||
|
||||
|
||||
// 5. 添加日志中间件(可选)
|
||||
final handler = Pipeline()
|
||||
.addMiddleware(logRequests())
|
||||
.addHandler(staticHandler);
|
||||
|
||||
final handler =
|
||||
Pipeline().addMiddleware(logRequests()).addHandler(staticHandler);
|
||||
|
||||
// 6. 启动服务器(端口 0 表示自动分配)
|
||||
_localServer = await io.serve(handler, InternetAddress.loopbackIPv4, 0);
|
||||
_serverPort = _localServer!.port;
|
||||
_localServerUrl = 'http://127.0.0.1:$_serverPort';
|
||||
|
||||
|
||||
print('小e本地服务启动成功: $_localServerUrl');
|
||||
print('资源目录: ${_tempExtractDir!.path}');
|
||||
|
||||
|
||||
isServerStarting.value = false;
|
||||
return true;
|
||||
|
||||
} catch (e, stackTrace) {
|
||||
serverError.value = '启动本地服务器失败: $e';
|
||||
print('服务器启动错误: $e\n$stackTrace');
|
||||
@@ -353,27 +352,28 @@ class _EPageState extends State<EPage> with AutomaticKeepAliveClientMixin {
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _extractZipToDirectory(Uint8List zipBytes, Directory targetDir) async {
|
||||
Future<bool> _extractZipToDirectory(
|
||||
Uint8List zipBytes, Directory targetDir) async {
|
||||
try {
|
||||
// 解码 ZIP 文件
|
||||
final Archive archive = ZipDecoder().decodeBytes(zipBytes);
|
||||
|
||||
|
||||
for (final ArchiveFile file in archive) {
|
||||
final String filename = file.name;
|
||||
|
||||
|
||||
// 跳过目录条目(它们会通过文件创建自动生成)
|
||||
if (file.isFile) {
|
||||
// 确保目录存在
|
||||
final String filePath = p.join(targetDir.path, filename);
|
||||
final File outputFile = File(filePath);
|
||||
await outputFile.parent.create(recursive: true);
|
||||
|
||||
|
||||
// 写入文件内容
|
||||
final List<int> data = file.content as List<int>;
|
||||
await outputFile.writeAsBytes(data, flush: true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
print('解压完成,文件数量: ${archive.files.length}');
|
||||
return true;
|
||||
} catch (e, stackTrace) {
|
||||
@@ -391,17 +391,18 @@ class _EPageState extends State<EPage> with AutomaticKeepAliveClientMixin {
|
||||
'main.html',
|
||||
'default.html',
|
||||
];
|
||||
|
||||
|
||||
for (final entry in possibleEntries) {
|
||||
final File file = File(p.join(dir.path, entry));
|
||||
if (await file.exists()) {
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 如果没有找到标准入口,查找任何 .html 文件
|
||||
try {
|
||||
final List<FileSystemEntity> entities = await dir.list(recursive: false).toList();
|
||||
final List<FileSystemEntity> entities =
|
||||
await dir.list(recursive: false).toList();
|
||||
for (final entity in entities) {
|
||||
if (entity is File && entity.path.toLowerCase().endsWith('.html')) {
|
||||
return entity;
|
||||
@@ -410,7 +411,7 @@ class _EPageState extends State<EPage> with AutomaticKeepAliveClientMixin {
|
||||
} catch (e) {
|
||||
print('查找入口文件失败: $e');
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -421,7 +422,7 @@ class _EPageState extends State<EPage> with AutomaticKeepAliveClientMixin {
|
||||
_localServer = null;
|
||||
print('本地服务器已关闭');
|
||||
}
|
||||
|
||||
|
||||
// 清理临时目录
|
||||
if (_tempExtractDir != null && _tempExtractDir!.existsSync()) {
|
||||
try {
|
||||
@@ -442,9 +443,9 @@ class _EPageState extends State<EPage> with AutomaticKeepAliveClientMixin {
|
||||
return LayoutBuilder(
|
||||
builder: (context, bodySize) => GestureDetector(
|
||||
child: Container(
|
||||
decoration: const BoxDecoration(
|
||||
decoration: BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage('assets/img/bgNoImg.png'),
|
||||
image: AssetImage(getBackgroundImageNoImage()),
|
||||
fit: BoxFit.fill,
|
||||
),
|
||||
),
|
||||
@@ -512,7 +513,7 @@ class _EPageState extends State<EPage> with AutomaticKeepAliveClientMixin {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return ValueListenableBuilder<String>(
|
||||
valueListenable: serverError,
|
||||
builder: (context, error, child) {
|
||||
@@ -571,7 +572,7 @@ class _EPageState extends State<EPage> with AutomaticKeepAliveClientMixin {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return Obx(() {
|
||||
if (finalUri.isEmpty) {
|
||||
return Center(
|
||||
@@ -696,12 +697,12 @@ class _EPageState extends State<EPage> with AutomaticKeepAliveClientMixin {
|
||||
// 构建最终 URL
|
||||
String baseUrl = _localServerUrl;
|
||||
String queryParams = '?t=${DateTime.now().millisecondsSinceEpoch}';
|
||||
|
||||
|
||||
if (deviceList.isNotEmpty) {
|
||||
String personParam = Uri.encodeComponent(jsonEncode(deviceList));
|
||||
queryParams += '&person=$personParam';
|
||||
}
|
||||
|
||||
|
||||
// 添加语言参数
|
||||
String? language = "";
|
||||
// 这里保持你原来的语言逻辑
|
||||
@@ -720,19 +721,19 @@ class _EPageState extends State<EPage> with AutomaticKeepAliveClientMixin {
|
||||
if (language != null && language.isNotEmpty) {
|
||||
queryParams += '&lang=$language';
|
||||
}
|
||||
|
||||
|
||||
finalUri.value = baseUrl + queryParams;
|
||||
print('最终加载 URL: ${finalUri.value}');
|
||||
|
||||
} else {
|
||||
// API 调用失败,使用基础 URL
|
||||
finalUri.value = '$_localServerUrl?t=${DateTime.now().millisecondsSinceEpoch}';
|
||||
finalUri.value =
|
||||
'$_localServerUrl?t=${DateTime.now().millisecondsSinceEpoch}';
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
print('获取设备列表失败: $e');
|
||||
// 出错时仍然加载基础页面
|
||||
finalUri.value = '$_localServerUrl?t=${DateTime.now().millisecondsSinceEpoch}';
|
||||
finalUri.value =
|
||||
'$_localServerUrl?t=${DateTime.now().millisecondsSinceEpoch}';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user