system_setting.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. rpc.requestEncryptConfig = JsonRpcEncryptConfig(
  47. enable: dto.enableRequestEncrypt,
  48. encryptMode: dto.requestEncryptMode ?? "",
  49. encryptKey: dto.requestEncryptKey ?? "",
  50. );
  51. rpc.responseEncryptConfig = JsonRpcEncryptConfig(
  52. enable: dto.enableResponseEncrypt,
  53. encryptMode: dto.responseEncryptMode ?? "",
  54. encryptKey: Store.user.token ?? "",
  55. );
  56. }
  57. Future<SyncSettingsDTO?> _getSettingsFromRpc() async {
  58. try {
  59. final request = SyncSettingRequest(token: Store.user.token);
  60. final result = await rpc.systemSetting.syncSettingAsync(request);
  61. return result;
  62. } catch (e) {
  63. logger.e("SystemSettingManager sync settings from rpc error.", e);
  64. return null;
  65. }
  66. }
  67. Future<SyncSettingsDTO?> _loadSettingsFromStorage() async {
  68. try {
  69. final jsonText = await _storage.read();
  70. if (jsonText == null) {
  71. return null;
  72. }
  73. final jsonMap = jsonDecode(jsonText);
  74. final dto = SyncSettingsDTO.fromJson(jsonMap);
  75. return dto;
  76. } catch (e) {
  77. logger.e("SystemSettingManager load settings from storage error.", e);
  78. return null;
  79. }
  80. }
  81. @override
  82. Future<List<String>> getEnableDeviceKeysAsync() async {
  83. try {
  84. final request = GetEnableDeviceKeysRequest(
  85. token: Store.user.token,
  86. organizationCode: Store.user.organizationCode);
  87. final result = await rpc.organization.getEnableDeviceKeysAsync(request);
  88. return result;
  89. } catch (e) {
  90. logger.e("SystemSettingManager getEnableDeviceKeysAsync error.", e);
  91. return [];
  92. }
  93. }
  94. }