import 'dart:async'; import 'dart:io'; import '../../../data/models/appconfig.dart'; import '../../../data/models/appconfig_model.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:get_storage/get_storage.dart'; import '../../../data/models/user_Info.dart'; import '../../../routes/app_routes.dart'; import '../../../core/themes/theme_service.dart'; import '../../../data/services/network/api.dart'; import '../../../data/services/version_update_service.dart'; import '../../../modules/common/widgets/version_update_dialog.dart'; import '../../common/webview_page.dart'; class SplashController extends GetxController { final GetStorage _storage = GetStorage(); // 进度条动画控制 final _loadingProgress = 0.0.obs; double get loadingProgress => _loadingProgress.value; // 资源加载状态 final _isResourceLoaded = false.obs; bool get isResourceLoaded => _isResourceLoaded.value; final _hasAcceptedPrivacy = false.obs; //是否同意隐私政策 // 版本号 //final String version = 'V1.0.0'; // 主题服务 final ThemeService _themeService = ThemeService(); @override void onInit() { super.onInit(); // 首先检查隐私政策状态 final hasAcceptedPrivacy = _storage.read('hasAcceptedPrivacy') ?? false; WidgetsBinding.instance.addPostFrameCallback((_) { if (!hasAcceptedPrivacy) { // 优先显示隐私政策弹窗,暂停其他初始化 _showPrivacyDialog(); } else { // 已同意隐私政策,正常启动流程 _startNormalFlow(); } }); } void _startNormalFlow() { _startLoadingAnimation(); _loadAppResources(); } void _onPrivacyAccepted() { _storage.write('hasAcceptedPrivacy', true); Get.back(); // 关闭弹窗 _startNormalFlow(); // 开始正常流程 } void _onPrivacyRejected() { exit(0); // 退出应用 } // 显示隐私政策弹窗 void _showPrivacyDialog() { // 获取当前主题模式 final bool isDarkMode = Get.theme.brightness == Brightness.dark; Get.dialog( WillPopScope( onWillPop: () async => false, // 禁止通过返回键关闭弹窗 child: Dialog( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(16), ), backgroundColor: isDarkMode ? Colors.grey[800] : Colors.white, // 暗黑模式使用深灰色背景 child: Container( padding: const EdgeInsets.all(20), child: Column( mainAxisSize: MainAxisSize.min, children: [ Text( 'loginDeclaration'.tr, //APP 声明 style: TextStyle( fontSize: 18, fontWeight: FontWeight.bold, color: isDarkMode ? Colors.white : Colors.black, // 暗黑模式使用白色文字 ), ), const SizedBox(height: 16), Padding( padding: const EdgeInsets.symmetric(horizontal: 20), child: Text.rich( TextSpan( children: [ TextSpan( text: 'welcomeToThisApp'.tr, //欢迎使用本APP,请充分阅读 style: TextStyle( fontSize: 14, color: isDarkMode ? Colors.white70 : Colors.black87, // 暗黑模式使用浅白色 ), ), WidgetSpan( alignment: PlaceholderAlignment.middle, child: GestureDetector( onTap: () { String url = 'https://www.voitrans.net/doc/ysxy.html'; // 隐私政策链接 if (Get.locale?.languageCode != 'zh' || Get.locale?.countryCode != 'CN') { url = 'https://www.voitrans.net/doc/ysxy_en.html'; // 隐私政策英文链接 } Get.to(() => WebViewPage( url: url, title: 'privacyPolicy'.tr, )); }, child: Text( 'privacyPolicy'.tr, //隐私政策 style: const TextStyle( fontSize: 14, color: Color(0xFF3B82F6), ), ), ), ), TextSpan( text: 'and'.tr, //和 style: TextStyle( fontSize: 14, color: isDarkMode ? Colors.white70 : Colors.black87, ), ), WidgetSpan( alignment: PlaceholderAlignment.middle, child: GestureDetector( onTap: () { String url = 'https://www.voitrans.net/doc/yhxy.html'; // 用户协议链接 if (Get.locale?.languageCode != 'zh' || Get.locale?.countryCode != 'CN') { url = 'https://www.voitrans.net/doc/yhxy_en.html'; // 用户协议英文链接 } Get.to(() => WebViewPage( url: url, title: 'userAgreement'.tr, //用户协议 )); }, child: Text( 'userAgreement'.tr, //用户协议 style: const TextStyle( fontSize: 14, color: Color(0xFF3B82F6), ), ), ), ), TextSpan( text: 'privacyPolicyText' .tr, //',点击“同意并进入”表示您已充分阅读并同意上述协议。点击“不同意”将退出应用。', style: TextStyle( fontSize: 14, color: isDarkMode ? Colors.white70 : Colors.black87, ), ), ], ), textAlign: TextAlign.center, ), ), const SizedBox(height: 20), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ TextButton( onPressed: () { // 用户不同意,退出应用 _onPrivacyRejected(); }, style: TextButton.styleFrom( foregroundColor: isDarkMode ? Colors.grey[400] : Colors.black54, // 暗黑模式使用浅灰色 ), child: Text( 'disagree'.tr, //不同意 style: TextStyle( color: isDarkMode ? Colors.grey[400] : Colors.black54, ), ), //child: const Text('不同意'), ), ElevatedButton( onPressed: () { _onPrivacyAccepted(); // 用户同意,关闭弹窗并继续 _hasAcceptedPrivacy.value = true; //Get.back(); }, style: ElevatedButton.styleFrom( backgroundColor: const Color(0xFF0066CC), foregroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), ), elevation: isDarkMode ? 4 : 2, // 暗黑模式下增加阴影效果提高辨识度 padding: const EdgeInsets.symmetric( horizontal: 16, vertical: 10), // 统一的内边距 ), child: Text('agreeAndEnter'.tr), ), ], ), ], ), ), ), ), barrierDismissible: false, // 禁止点击外部关闭弹窗 ); } // 初始化地图SDK // void _initMapSDK() { // try { // AmapFlutter().initSdk( // iosKey: "a4a1394fe817c2f86a424b897b4a9af4", // androidKey: "78e7946dd602778df82c6ae143a88184", // ); // } catch (e) { // print('地图SDK初始化失败: $e'); // } // } // 进度条动画(持续2秒) void _startLoadingAnimation() { const totalDuration = 1500; // 动画总时长增加到1.5秒 const steps = totalDuration ~/ 30; // 30的间隔,更平滑的动画 const stepValue = 1.0 / steps; Timer.periodic(const Duration(milliseconds: 30), (timer) { if (_loadingProgress.value < 1.0) { _loadingProgress.value += stepValue; } else { timer.cancel(); _checkNavigateCondition(); } }); } // 异步加载应用资源 Future _loadAppResources() async { try { // 模拟资源加载(如API配置、本地缓存等) await Future.delayed(const Duration(milliseconds: 1500)); // 实际资源加载代码 // await _loadImages(); // await _initDatabase(); // 检查版本更新(在后台进行,不阻塞启动流程) //_checkVersionUpdate(); _isResourceLoaded.value = true; _checkNavigateCondition(); } catch (e) { Get.snackbar('错误', '资源加载失败,请检查网络连接'); _isResourceLoaded.value = true; // 允许用户继续使用 print('资源加载错误: $e'); } } // 检查跳转条件 void _checkNavigateCondition() { if (_loadingProgress.value >= 1.0 && _isResourceLoaded.value) { _navigateToLogin(); } } // 获取登录Token并跳转 Future _navigateToLogin() async { bool isLoggedIn = false; final token = _storage.read('logintoken'); print('getLoginToken:进来了${token}'); if (token != null && token.isNotEmpty) { isLoggedIn = await getLoginToken(); } if (isLoggedIn) { await getAppConfig(); Get.offAllNamed(Routes.pairing); } else { Get.offAllNamed(Routes.login); // 跳转到登录页 } } Future getLoginToken() async { try { print('getLoginToken:进来了'); //if (User.getname() != "") { final response = await Api.tokenlogin(); print('getLoginToken: $response'); if (response == null) return false; if (response['user'] != '') { print('getLoginToken:进来了'); // 根据API设计调整 final user = User.fromJson(response); // 存储用户信息(示例) await _storage.write('user_info', user.toJson()); return true; } //return true; //} return false; } catch (e) { print('Token获取失败: $e'); return false; } } Future getAppConfig() async { try { print('getAppConfig:进来了'); //if (User.getname() != "") { final response = await Api.getappconfig(); print('getAppConfig: $response'); if (response == null) return false; // 根据API设计调整 final resp = UserGetAppConfigResp.fromJson(response); print('getAppConfig:进来了'); // 存储用户信息(示例) // _storage.write('ENV', resp.env); // _storage.write('AGENTS', resp.agents); // _storage.write('MCPS', resp.mcps); AppConfig.initialize(resp); return true; } catch (e) { print('getAppConfig 取失败: $e'); return false; } } /// 检查版本更新(后台执行,不阻塞启动流程) // void _checkVersionUpdate() { // // 延迟执行版本检查,避免影响启动速度 // Timer(const Duration(seconds: 1), () async { // try { // final versionService = VersionUpdateService.to; // final updateInfo = await versionService.checkForUpdate(); // if (updateInfo.hasUpdate) { // // 显示更新对话框 // await VersionUpdateDialog.show(updateInfo); // } // } catch (e) { // print('版本检查失败: $e'); // // 版本检查失败不影响用户使用,静默处理 // } // }); // } }