import 'package:fis_jsonrpc/encrpyt.dart'; import 'package:fis_jsonrpc/rpc.dart'; import 'package:get/get.dart'; import 'package:vitalapp/architecture/types/index.dart'; import 'package:vitalapp/managers/interfaces/account.dart'; import 'package:vitalapp/managers/interfaces/system_setting.dart'; import 'package:vitalapp/managers/interfaces/template.dart'; import 'package:vitalapp/rpc.dart'; import '../defines.dart'; import '../store.dart'; class UserState extends StateModuleBase { String? _token; String? _account; String? _password; /// 是否自动登录 bool _isAutoLogin = false; final Rx?> _menuPermissionList = Rx(null); final RxString _displayName = ''.obs; final RxList _features = RxList(); final Rx _headImageToken = Rx(''); final Rx _currentSelectPatientInfo = Rx(null); final Rx _isShowUserCard = Rx(false); /// 登录令牌 String? get token => _token; /// 是否登录中 bool get isLogOn => token != null && token!.isNotEmpty; /// 是否记住密码 bool get isAutoLogin => _isAutoLogin; /// 登录账号 String? get account => _account; /// 用户Code String? get userCode => _userInfo?.code; /// 登录密码 String? get password => _password; /// 外显名称 String get displayName => _displayName.value; set displayName(String val) => _displayName.value = val; /// 是否显示用户卡片 bool get isShowUserCard => _isShowUserCard.value; set isShowUserCard(bool val) => _isShowUserCard.value = val; /// 团队名称 String get teamName => _userInfo?.teamName ?? ""; /// 负责医生 String get principalName => _userInfo?.principalName ?? ""; /// 医生电话 String get principalPhone => _userInfo?.principalPhone ?? ""; /// 机构code String get organizationCode => _userInfo?.organizationCode ?? ""; /// 机构名称 String get organizationName => _userInfo?.organizationName ?? ""; /// 机构电话 String get organizationPhone => _userInfo?.organizationPhone ?? ""; /// 医生签名 String get signature => _userInfo?.signature ?? ""; ///头像 String get headImageToken => _headImageToken.value; set headImageToken(String val) => _headImageToken.value = val; /// 当前选中的病人详情 PatientDTO? get currentSelectPatientInfo => _currentSelectPatientInfo.value; set currentSelectPatientInfo(PatientDTO? val) => _currentSelectPatientInfo.value = val; /// 当前用户的菜单权限 List? get menuPermissionList => _menuPermissionList.value; set menuPermissionList(List? val) => _menuPermissionList.value = val; /// 负责区域(村)集合 List get residence => _userInfo?.residence ?.map((e) => StringKVModel(e.code!, e.name!)) .toList() ?? []; /// 用户信息 UserDTO? _userInfo; /// 处理登录 Future handleLogin( String account, String token, String password, bool isAutoLogin, ) async { _token = token; _account = account; _password = password; _isAutoLogin = isAutoLogin; _updateRpcResEncryptKey(token); // await _fetchFeatures();// TODO: await Store.persistent(); await Get.find().saveTemplate(); _menuPermissionList.value = await Get.find().getMenuPermission(); await Get.find().getSettings(true); Future.delayed(const Duration(milliseconds: 300), () { isShowUserCard = true; }); } /// 处理登出 Future handleLogOut() async { _token = null; isShowUserCard = false; _updateRpcResEncryptKey(""); await Store.persistent(); } /// 更新用户信息 Future updateUserInfo(UserDTO dto) async { _userInfo = dto; _headImageToken.value = dto.headImageToken ?? ''; _displayName.value = _userInfo?.realName ?? _userInfo?.userName ?? ""; await Store.persistent(); } /// 更新用户签名 Future updateSignature(String value) async { if (_userInfo != null) { _userInfo!.signature = value; await Store.persistent(); } } @override Future acceptPersistenceJson(Map map) async { // key对应值不为空时,给相应字段赋值 map.pickPersistentProp('token', (x) => _token = x); map.pickPersistentProp('isAutoLogin', (x) => _isAutoLogin = x); map.pickPersistentProp( 'features', (x) => _updateFeatures((x as List).cast()), ); map.pickPersistentProps({ 'account': (x) => _account = x, 'password': (x) => _password = x, 'displayName': (x) => _displayName.value = x, }); map.pickPersistentProp( 'userInfo', (x) { if (x != null) { _userInfo = UserDTO.fromJson(x); _headImageToken.value = _userInfo?.headImageToken ?? ''; _displayName.value = _userInfo?.realName ?? _userInfo?.userName ?? ""; } }, ); } @override Map toPersistenceJson() { return { "token": token, "isAutoLogin": isAutoLogin, "account": account, "password": password, "displayName": displayName, "userInfo": _userInfo, }; } void _updateFeatures(Iterable keys) { _features.value = List.unmodifiable(keys); } void _clearFeatures() => _features.value = []; // 更新RPC响应秘钥 void _updateRpcResEncryptKey(String key) { final originResEncryptCfg = rpc.responseEncryptConfig; rpc.responseEncryptConfig = JsonRpcEncryptConfig( enable: originResEncryptCfg.enable, encryptMode: originResEncryptCfg.encryptMode, encryptKey: key, ); } }