global.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import 'package:fis_common/index.dart';
  2. import 'package:fis_common/web/web_asset_bundle.dart';
  3. import 'package:fis_i18n/i18n.dart';
  4. import 'package:fis_ui/utils/sizer/sizer.dart';
  5. import 'package:flutter/foundation.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter/services.dart';
  8. import 'package:get/get.dart';
  9. import 'package:vitalapp/consts/devices.dart';
  10. import 'package:vitalapp/database/db.dart';
  11. import 'package:vitalapp/js_platform/listener.dart';
  12. import 'package:vitalapp/managers/interfaces/excelData.dart';
  13. import 'package:vitalapp/pages/admin/usb_state.dart';
  14. import 'package:vnote_device_plugin/events/global_event_handler.dart';
  15. import 'package:vnote_device_plugin/vnote_device_plugin_platform_interface.dart';
  16. import 'package:vitalapp/architecture/network_connectivity.dart';
  17. import 'package:vitalapp/architecture/utils/prompt_box.dart';
  18. import 'package:vitalapp/managers/index.dart';
  19. import 'package:vitalapp/managers/interfaces/permission.dart';
  20. import 'package:vitalapp/rpc.dart';
  21. import 'package:vitalapp/store/store.dart';
  22. import 'package:fis_common/logger/logger.dart';
  23. import 'package:fis_measure/process/workspace/rpc_bridge.dart';
  24. import 'package:fis_common/env/env.dart';
  25. import 'package:universal_html/js.dart' as js;
  26. import 'architecture/app_parameters.dart';
  27. import 'architecture/services/index.dart';
  28. import 'architecture/storage/shared_storage.dart';
  29. import 'architecture/utils/advance_debounce.dart';
  30. import 'architecture/utils/web_parameter_helpler.dart';
  31. import 'managers/interfaces/account.dart';
  32. import 'managers/interfaces/template.dart';
  33. /// 【全局】 是否在线
  34. bool get kIsOnline => netChecker.isOnline;
  35. FPlatformEnum get platform => FPlatform.current;
  36. abstract class Global {
  37. static AssetBundle? _assetBundle;
  38. /// 资源包
  39. static AssetBundle get assetBundle => _assetBundle!;
  40. static Future<void> init() async {
  41. // 日志
  42. if (kReleaseMode) {
  43. // 生产环境
  44. Get.isLogEnable = false;
  45. } else {
  46. // debug/profile 环境
  47. logger.setMinimumLevel(LogLevel.Degbug);
  48. }
  49. await logger.init();
  50. logger.i("Global init start...");
  51. // 资源包
  52. _initAssetBundle();
  53. // 锁定横屏
  54. await SystemChrome.setPreferredOrientations([
  55. DeviceOrientation.landscapeLeft,
  56. // DeviceOrientation.landscapeRight,
  57. ]);
  58. try {
  59. UsbState usbState = UsbState();
  60. usbState.startListening();
  61. } catch (e) {
  62. logger.e("Global init UsbState error.", e);
  63. }
  64. // SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
  65. SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
  66. SystemChrome.setSystemUIChangeCallback((systemOverlaysAreVisible) async {
  67. advanceDebounce(() {
  68. Future.delayed(const Duration(seconds: 2), () {
  69. SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
  70. });
  71. }, "setSystemUIChangeCallback");
  72. });
  73. SystemChrome.setSystemUIOverlayStyle(
  74. const SystemUiOverlayStyle(
  75. statusBarColor: Colors.transparent,
  76. statusBarIconBrightness: Brightness.light,
  77. ),
  78. );
  79. // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
  80. // Loading工具
  81. PromptBox.init();
  82. /// 共享存储
  83. await Get.putAsync<SharedStorageService>(() => SharedStorageService.init());
  84. /// 预设配置
  85. await AppParameters.load();
  86. // 状态
  87. await Store.init();
  88. // 开始检测网络状态
  89. netChecker.run();
  90. if (platform == FPlatformEnum.android) {
  91. await db.init();
  92. /// 设备测量插件初始化
  93. await _initDevices();
  94. }
  95. ///获取启动参数
  96. if (ParameterHelper.parasmeters.isEmpty)
  97. ParameterHelper.getAppStartParameters();
  98. if (kDebugMode && Store.user.token.isNullOrEmpty) {
  99. Store.user.setToken("94642923B8D240FF81F7744D3B8EDEBF");
  100. }
  101. _initRpc();
  102. /// 全局管理器中心
  103. ManagerCenter.load();
  104. //_checkIsLogin();
  105. /// 初始化动态权限
  106. _initPermission();
  107. var bookOptions = [
  108. FTrKeybookOption.ChineseOption,
  109. ];
  110. final curLocale = CHINESE_LOCALE;
  111. await FI18n.init((locale) => Get.updateLocale(locale), bookOptions);
  112. await FI18n.load(curLocale);
  113. Get.put(FI18n.ins);
  114. Sizer.init();
  115. // 依赖注入
  116. await initServices();
  117. // // 主题
  118. // final theme = AppTheme(themeMode: ThemeMode.system);
  119. // await FTheme.init(theme);
  120. // Get.put(theme);
  121. await _loadDefaultFont();
  122. final templateController = Get.find<ITemplateManager>();
  123. await templateController.saveTemplate();
  124. //Get.put(RPCBridge(rpc, Store.user.token ?? ''));
  125. //JsPlatformListener().run();
  126. logger.i("Global init finish...");
  127. }
  128. /// 初始化资源包
  129. static void _initAssetBundle() {
  130. if (kIsWeb) {
  131. final jsVersion = js.context["serviceWorkerVersion"];
  132. String version = jsVersion?.toString() ??
  133. "debug_${DateTime.now().millisecondsSinceEpoch}";
  134. _assetBundle = WebPlatformAssetBundle(
  135. rootPath: js.context["STATIC_ROOT"]!.toString(),
  136. version: version,
  137. );
  138. } else {
  139. _assetBundle = rootBundle;
  140. }
  141. }
  142. static Future<void> _loadDefaultFont() async {
  143. try {
  144. final loader = FontLoader("NotoSansSC");
  145. loader.addFont(rootBundle.load("assets/fonts/NotoSansSC-R-fixed.otf"));
  146. await loader.load();
  147. } catch (e) {
  148. logger.e("Global load default font error.", e);
  149. }
  150. }
  151. static void _initRpc() {
  152. RpcSettingCache.setRpcHostPath(Store.app.serverUrl);
  153. }
  154. static Future<void> _initPermission() async {
  155. IPermissionManager permissionManager = Get.find<IPermissionManager>();
  156. await permissionManager.requestPermissions([
  157. 'Location',
  158. 'BluetoothConnect',
  159. 'BluetoothScan',
  160. 'BluetoothAdvertise',
  161. 'Camera',
  162. 'InstallPackages',
  163. ]);
  164. }
  165. static Future<void> _checkIsLogin() async {
  166. if (kIsOnline) {
  167. final accountController = Get.find<IAccountManager>();
  168. await accountController.getOperationPermission();
  169. }
  170. }
  171. static Future<void> _initDevices() async {
  172. // TODO: 后续改成按后门配置开启
  173. await VnoteDevicePluginPlatform.instance.init(false);
  174. final List<_DeviceStatusTipsHandler> statusHandlers = [];
  175. for (int status in DeviceConsts.STATUS_DESC_MAP.keys) {
  176. final message = DeviceConsts.STATUS_DESC_MAP[status]!;
  177. statusHandlers.add(_DeviceStatusTipsHandler(status, message));
  178. }
  179. GlobalDeviceEventHandler.setup(statusHandlers: statusHandlers);
  180. }
  181. }
  182. class _DeviceStatusTipsHandler extends DeviceEventStatusHandler {
  183. late final String _msg;
  184. _DeviceStatusTipsHandler(int status, String msg) : super(status) {
  185. _msg = msg;
  186. }
  187. String get message => _msg;
  188. @override
  189. void handle(Map<String, dynamic>? data) {
  190. // PromptBox.toast(message);
  191. Get.snackbar("提示", message);
  192. }
  193. }