import 'dart:convert'; import 'package:fis_jsonrpc/encrpyt.dart'; import 'package:fis_jsonrpc/rpc.dart'; import 'package:vitalapp/architecture/storage/text_storage.dart'; import 'package:vitalapp/global.dart'; import 'package:vitalapp/managers/interfaces/system_setting.dart'; import 'package:vitalapp/rpc.dart'; import 'package:vitalapp/store/store.dart'; import 'package:fis_common/logger/logger.dart'; class SystemSettingManager implements ISystemSettingManager { final _storage = TextStorage(fileName: "system_settings.cfg", directory: "settings"); SyncSettingsDTO? _cache; @override Future getSettings([isRefresh = false]) async { if (isRefresh) { // 清空缓存 _cache = null; // 清空RPC加密信息 rpc.requestEncryptConfig = JsonRpcEncryptConfig(); rpc.responseEncryptConfig = JsonRpcEncryptConfig(); } if (_cache == null) { await _loadSettings(); _cache ??= SyncSettingsDTO(); _handleSettingsAbout(_cache!); } return _cache!; } Future _loadSettings() async { _cache = await _loadSettingsFromStorage(); if (kIsOnline) { // 判断是否在线,再请求rpc SyncSettingsDTO? remoteSettings = await _getSettingsFromRpc(); if (remoteSettings == null) { return; } _cache = remoteSettings; final jsonText = jsonEncode(remoteSettings.toJson()); await _storage.save(jsonText); } } void _handleSettingsAbout(SyncSettingsDTO dto) { Store.app.enableEncryptSensitiveInfo = dto.encryptSensitiveInfo; Store.app.homePageUrl = dto.homePageUrl ?? ''; Store.user.projectType = dto.projectType; rpc.requestEncryptConfig = JsonRpcEncryptConfig( enable: dto.enableRequestEncrypt, encryptMode: dto.requestEncryptMode ?? "", encryptKey: dto.requestEncryptKey ?? "", ); rpc.responseEncryptConfig = JsonRpcEncryptConfig( enable: dto.enableResponseEncrypt, encryptMode: dto.responseEncryptMode ?? "", encryptKey: Store.user.token ?? "", ); } Future _getSettingsFromRpc() async { try { final request = SyncSettingRequest(token: Store.user.token); final result = await rpc.vitalSystemSetting.syncSettingAsync(request); return result; } catch (e) { logger.e("SystemSettingManager sync settings from rpc error.", e); return null; } } Future _loadSettingsFromStorage() async { try { final jsonText = await _storage.read(); if (jsonText == null) { return null; } final jsonMap = jsonDecode(jsonText); final dto = SyncSettingsDTO.fromJson(jsonMap); return dto; } catch (e) { logger.e("SystemSettingManager load settings from storage error.", e); return null; } } @override Future> getEnableDeviceKeysAsync() async { try { final request = GetEnableDeviceKeysRequest( token: Store.user.token, organizationCode: Store.user.organizationCode); final result = await rpc.vitalOrganization.getEnableDeviceKeysAsync(request); return result; } catch (e) { logger.e("SystemSettingManager getEnableDeviceKeysAsync error.", e); return []; } } }