user.dart 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import 'package:fis_jsonrpc/encrpyt.dart';
  2. import 'package:fis_jsonrpc/rpc.dart';
  3. import 'package:get/get.dart';
  4. import 'package:vitalapp/architecture/types/index.dart';
  5. import 'package:vitalapp/managers/interfaces/account.dart';
  6. import 'package:vitalapp/managers/interfaces/system_setting.dart';
  7. import 'package:vitalapp/managers/interfaces/template.dart';
  8. import 'package:vitalapp/rpc.dart';
  9. import '../defines.dart';
  10. import '../store.dart';
  11. class UserState extends StateModuleBase {
  12. String? _token;
  13. String? _account;
  14. String? _password;
  15. /// 是否自动登录
  16. bool _isAutoLogin = false;
  17. final Rx<List<UserFeatureDTO>?> _menuPermissionList = Rx(null);
  18. final RxString _displayName = ''.obs;
  19. final RxList<String> _features = RxList<String>();
  20. final Rx<String> _headImageToken = Rx('');
  21. final Rx<PatientDTO?> _currentSelectPatientInfo = Rx(null);
  22. final Rx<bool> _isShowUserCard = Rx(false);
  23. /// 登录令牌
  24. String? get token => _token;
  25. /// 是否登录中
  26. bool get isLogOn => token != null && token!.isNotEmpty;
  27. /// 是否记住密码
  28. bool get isAutoLogin => _isAutoLogin;
  29. /// 登录账号
  30. String? get account => _account;
  31. /// 用户Code
  32. String? get userCode => _userInfo?.code;
  33. /// 登录密码
  34. String? get password => _password;
  35. /// 外显名称
  36. String get displayName => _displayName.value;
  37. set displayName(String val) => _displayName.value = val;
  38. /// 是否显示用户卡片
  39. bool get isShowUserCard => _isShowUserCard.value;
  40. set isShowUserCard(bool val) => _isShowUserCard.value = val;
  41. /// 团队名称
  42. String get teamName => _userInfo?.teamName ?? "";
  43. /// 负责医生
  44. String get principalName => _userInfo?.principalName ?? "";
  45. /// 医生电话
  46. String get principalPhone => _userInfo?.principalPhone ?? "";
  47. /// 机构code
  48. String get organizationCode => _userInfo?.organizationCode ?? "";
  49. /// 机构名称
  50. String get organizationName => _userInfo?.organizationName ?? "";
  51. /// 机构电话
  52. String get organizationPhone => _userInfo?.organizationPhone ?? "";
  53. /// 医生签名
  54. String get signature => _userInfo?.signature ?? "";
  55. ///头像
  56. String get headImageToken => _headImageToken.value;
  57. set headImageToken(String val) => _headImageToken.value = val;
  58. /// 当前选中的病人详情
  59. PatientDTO? get currentSelectPatientInfo => _currentSelectPatientInfo.value;
  60. set currentSelectPatientInfo(PatientDTO? val) =>
  61. _currentSelectPatientInfo.value = val;
  62. /// 当前用户的菜单权限
  63. List<UserFeatureDTO>? get menuPermissionList => _menuPermissionList.value;
  64. set menuPermissionList(List<UserFeatureDTO>? val) =>
  65. _menuPermissionList.value = val;
  66. /// 负责区域(村)集合
  67. List<StringKVModel> get residence =>
  68. _userInfo?.residence
  69. ?.map((e) => StringKVModel(e.code!, e.name!))
  70. .toList() ??
  71. [];
  72. /// 用户信息
  73. UserDTO? _userInfo;
  74. /// 处理登录
  75. Future<void> handleLogin(
  76. String account,
  77. String token,
  78. String password,
  79. bool isAutoLogin,
  80. ) async {
  81. _token = token;
  82. _account = account;
  83. _password = password;
  84. _isAutoLogin = isAutoLogin;
  85. _updateRpcResEncryptKey(token);
  86. // await _fetchFeatures();// TODO:
  87. await Store.persistent();
  88. await Get.find<ITemplateManager>().saveTemplate();
  89. _menuPermissionList.value =
  90. await Get.find<IAccountManager>().getMenuPermission();
  91. await Get.find<ISystemSettingManager>().getSettings(true);
  92. Future.delayed(const Duration(milliseconds: 300), () {
  93. isShowUserCard = true;
  94. });
  95. }
  96. /// 处理登出
  97. Future<void> handleLogOut() async {
  98. _token = null;
  99. isShowUserCard = false;
  100. _updateRpcResEncryptKey("");
  101. await Store.persistent();
  102. }
  103. /// 更新用户信息
  104. Future<void> updateUserInfo(UserDTO dto) async {
  105. _userInfo = dto;
  106. _headImageToken.value = dto.headImageToken ?? '';
  107. _displayName.value = _userInfo?.realName ?? _userInfo?.userName ?? "";
  108. await Store.persistent();
  109. }
  110. /// 更新用户签名
  111. Future<void> updateSignature(String value) async {
  112. if (_userInfo != null) {
  113. _userInfo!.signature = value;
  114. await Store.persistent();
  115. }
  116. }
  117. @override
  118. Future<void> acceptPersistenceJson(Map<String, dynamic> map) async {
  119. // key对应值不为空时,给相应字段赋值
  120. map.pickPersistentProp('token', (x) => _token = x);
  121. map.pickPersistentProp('isAutoLogin', (x) => _isAutoLogin = x);
  122. map.pickPersistentProp(
  123. 'features',
  124. (x) => _updateFeatures((x as List).cast<String>()),
  125. );
  126. map.pickPersistentProps({
  127. 'account': (x) => _account = x,
  128. 'password': (x) => _password = x,
  129. 'displayName': (x) => _displayName.value = x,
  130. });
  131. map.pickPersistentProp(
  132. 'userInfo',
  133. (x) {
  134. if (x != null) {
  135. _userInfo = UserDTO.fromJson(x);
  136. _headImageToken.value = _userInfo?.headImageToken ?? '';
  137. _displayName.value = _userInfo?.realName ?? _userInfo?.userName ?? "";
  138. }
  139. },
  140. );
  141. }
  142. @override
  143. Map<String, dynamic> toPersistenceJson() {
  144. return {
  145. "token": token,
  146. "isAutoLogin": isAutoLogin,
  147. "account": account,
  148. "password": password,
  149. "displayName": displayName,
  150. "userInfo": _userInfo,
  151. };
  152. }
  153. void _updateFeatures(Iterable<String> keys) {
  154. _features.value = List.unmodifiable(keys);
  155. }
  156. void _clearFeatures() => _features.value = [];
  157. // 更新RPC响应秘钥
  158. void _updateRpcResEncryptKey(String key) {
  159. final originResEncryptCfg = rpc.responseEncryptConfig;
  160. rpc.responseEncryptConfig = JsonRpcEncryptConfig(
  161. enable: originResEncryptCfg.enable,
  162. encryptMode: originResEncryptCfg.encryptMode,
  163. encryptKey: key,
  164. );
  165. }
  166. }