system_setting.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import 'dart:convert';
  2. import 'package:fis_jsonrpc/encrpyt.dart';
  3. import 'package:fis_jsonrpc/rpc.dart';
  4. import 'package:vitalapp/architecture/storage/text_storage.dart';
  5. import 'package:vitalapp/global.dart';
  6. import 'package:vitalapp/managers/interfaces/system_setting.dart';
  7. import 'package:vitalapp/rpc.dart';
  8. import 'package:vitalapp/store/store.dart';
  9. import 'package:fis_common/logger/logger.dart';
  10. class SystemSettingManager implements ISystemSettingManager {
  11. final _storage =
  12. TextStorage(fileName: "system_settings.cfg", directory: "settings");
  13. SyncSettingsDTO? _cache;
  14. @override
  15. Future<SyncSettingsDTO> getSettings([isRefresh = false]) async {
  16. if (isRefresh) {
  17. // 清空缓存
  18. _cache = null;
  19. // 清空RPC加密信息
  20. rpc.requestEncryptConfig = JsonRpcEncryptConfig();
  21. rpc.responseEncryptConfig = JsonRpcEncryptConfig();
  22. }
  23. if (_cache == null) {
  24. await _loadSettings();
  25. _cache ??= SyncSettingsDTO();
  26. _handleSettingsAbout(_cache!);
  27. }
  28. return _cache!;
  29. }
  30. Future<void> _loadSettings() async {
  31. _cache = await _loadSettingsFromStorage();
  32. if (kIsOnline) {
  33. // 判断是否在线,再请求rpc
  34. SyncSettingsDTO? remoteSettings = await _getSettingsFromRpc();
  35. if (remoteSettings == null) {
  36. return;
  37. }
  38. _cache = remoteSettings;
  39. final jsonText = jsonEncode(remoteSettings.toJson());
  40. await _storage.save(jsonText);
  41. }
  42. }
  43. void _handleSettingsAbout(SyncSettingsDTO dto) {
  44. Store.app.enableEncryptSensitiveInfo = dto.encryptSensitiveInfo;
  45. Store.app.homePageUrl = dto.homePageUrl ?? '';
  46. Store.user.projectType = dto.projectType;
  47. rpc.requestEncryptConfig = JsonRpcEncryptConfig(
  48. enable: dto.enableRequestEncrypt,
  49. encryptMode: dto.requestEncryptMode ?? "",
  50. encryptKey: dto.requestEncryptKey ?? "",
  51. );
  52. rpc.responseEncryptConfig = JsonRpcEncryptConfig(
  53. enable: dto.enableResponseEncrypt,
  54. encryptMode: dto.responseEncryptMode ?? "",
  55. encryptKey: Store.user.token ?? "",
  56. );
  57. }
  58. Future<SyncSettingsDTO?> _getSettingsFromRpc() async {
  59. try {
  60. final request = SyncSettingRequest(token: Store.user.token);
  61. final result = await rpc.vitalSystemSetting.syncSettingAsync(request);
  62. return result;
  63. } catch (e) {
  64. logger.e("SystemSettingManager sync settings from rpc error.", e);
  65. return null;
  66. }
  67. }
  68. Future<SyncSettingsDTO?> _loadSettingsFromStorage() async {
  69. try {
  70. final jsonText = await _storage.read();
  71. if (jsonText == null) {
  72. return null;
  73. }
  74. final jsonMap = jsonDecode(jsonText);
  75. final dto = SyncSettingsDTO.fromJson(jsonMap);
  76. return dto;
  77. } catch (e) {
  78. logger.e("SystemSettingManager load settings from storage error.", e);
  79. return null;
  80. }
  81. }
  82. @override
  83. Future<List<String>> getEnableDeviceKeysAsync() async {
  84. try {
  85. final request = GetEnableDeviceKeysRequest(
  86. token: Store.user.token,
  87. organizationCode: Store.user.organizationCode);
  88. final result =
  89. await rpc.vitalOrganization.getEnableDeviceKeysAsync(request);
  90. return result;
  91. } catch (e) {
  92. logger.e("SystemSettingManager getEnableDeviceKeysAsync error.", e);
  93. return [];
  94. }
  95. }
  96. }