user.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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/template.dart';
  7. import 'package:vitalapp/rpc.dart';
  8. import 'package:fis_common/logger/logger.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 = true;
  17. final RxList<UserFeatureDTO> _menuPermissionList = RxList<UserFeatureDTO>();
  18. final RxList<UserFeatureDTO> _operationPermissionList =
  19. RxList<UserFeatureDTO>();
  20. final RxString _displayName = ''.obs;
  21. final RxList<String> _features = RxList<String>();
  22. final Rx<String> _headImageToken = Rx('');
  23. final Rx<PatientDTO?> _currentSelectPatientInfo = Rx(null);
  24. final Rx<VitalProjectTypeEnum> _projectType =
  25. Rx(VitalProjectTypeEnum.VinnoHealth);
  26. final Rx<RegisterPersonInfoDTO?> _currentSelectRegisterPersonInfo = Rx(null);
  27. /// 登录令牌
  28. String? get token => _token;
  29. /// 是否登录中
  30. bool get isLogOn => token != null && token!.isNotEmpty;
  31. /// 是否自动登录
  32. bool get isAutoLogin => _isAutoLogin;
  33. /// 登录账号
  34. String? get account => _account;
  35. /// 用户Code
  36. String? get userCode => _userInfo?.code;
  37. /// 登录密码
  38. String? get password => _password;
  39. /// 外显名称
  40. String get displayName => _displayName.value;
  41. set displayName(String val) => _displayName.value = val;
  42. /// 团队名称
  43. String get teamName => _userInfo?.teamName ?? "";
  44. /// 负责医生
  45. String get principalName => _userInfo?.principalName ?? "";
  46. /// 医生电话
  47. String get principalPhone => _userInfo?.principalPhone ?? "";
  48. /// 机构code
  49. String get organizationCode => _userInfo?.organizationCode ?? "";
  50. /// 机构名称
  51. String get organizationName => _userInfo?.organizationName ?? "";
  52. /// 机构电话
  53. String get organizationPhone => _userInfo?.organizationPhone ?? "";
  54. /// 医生签名
  55. String get signature => _userInfo?.signature ?? "";
  56. ///头像
  57. String get headImageToken => _headImageToken.value;
  58. set headImageToken(String val) => _headImageToken.value = val;
  59. /// 当前是哪个系统(项目类型 0:健康一体机,1:体检工作站,2:体检云端服务)
  60. VitalProjectTypeEnum get projectType => _projectType.value;
  61. set projectType(VitalProjectTypeEnum val) => _projectType.value = val;
  62. /// 当前选中的病人详情
  63. PatientDTO? get currentSelectPatientInfo => _currentSelectPatientInfo.value;
  64. set currentSelectPatientInfo(PatientDTO? val) {
  65. _currentSelectPatientInfo.value = val;
  66. logger.i("UserState currentSelectPatientInfo name:${val?.patientName}.");
  67. }
  68. /// 当前选中的体检者信息
  69. RegisterPersonInfoDTO? get currentSelectRegisterPersonInfo =>
  70. _currentSelectRegisterPersonInfo.value;
  71. set currentSelectRegisterPersonInfo(RegisterPersonInfoDTO? val) {
  72. _currentSelectRegisterPersonInfo.value = val;
  73. logger.i("UserState currentSelectRegisterPersonInfo name:${val?.name}.");
  74. }
  75. /// 当前用户的菜单权限
  76. List<UserFeatureDTO>? get menuPermissionList => _menuPermissionList;
  77. List<UserFeatureDTO>? get operationPermissionList => _operationPermissionList;
  78. /// 权限集合
  79. List<String> get features => _features;
  80. /// 是否有指定权限
  81. ///
  82. /// [key] 权限Key
  83. bool hasFeature(String key) => features.contains(key);
  84. /// 负责区域(村)集合
  85. List<StringKVModel> get residence =>
  86. _userInfo?.residence
  87. ?.map((e) => StringKVModel(e.code!, e.name!))
  88. .toList() ??
  89. [];
  90. /// 用户信息
  91. UserDTO2? _userInfo;
  92. /// 处理登录
  93. Future<void> handleLogin(
  94. String account,
  95. String token,
  96. String password,
  97. bool isAutoLogin,
  98. ) async {
  99. _token = token;
  100. _account = account;
  101. _password = password;
  102. _isAutoLogin = isAutoLogin;
  103. _updateRpcResEncryptKey(token);
  104. final templateController = Get.find<ITemplateManager>();
  105. await templateController.saveTemplate();
  106. await _fetchFeatures();
  107. await Store.persistent();
  108. }
  109. /// 处理登出
  110. Future<void> handleLogOut() async {
  111. _token = null;
  112. _currentSelectPatientInfo.value = null;
  113. _currentSelectRegisterPersonInfo.value = null;
  114. _updateRpcResEncryptKey("");
  115. await Store.persistent();
  116. }
  117. /// 更新用户信息
  118. Future<void> updateUserInfo(UserDTO2 dto) async {
  119. _userInfo = dto;
  120. _headImageToken.value = dto.headImageToken ?? '';
  121. _displayName.value = _userInfo?.realName ?? _userInfo?.userName ?? "";
  122. await Store.persistent();
  123. }
  124. /// 更新用户签名
  125. Future<void> updateSignature(String value) async {
  126. if (_userInfo != null) {
  127. _userInfo!.signature = value;
  128. await Store.persistent();
  129. }
  130. }
  131. @override
  132. Future<void> acceptPersistenceJson(Map<String, dynamic> map) async {
  133. // key对应值不为空时,给相应字段赋值
  134. map.pickPersistentProp('token', (x) => _token = x);
  135. map.pickPersistentProp('isAutoLogin', (x) => _isAutoLogin = x);
  136. map.pickPersistentProp(
  137. 'features',
  138. (x) => _updateFeatures((x as List).cast<String>()),
  139. );
  140. map.pickPersistentProps({
  141. 'account': (x) => _account = x,
  142. 'password': (x) => _password = x,
  143. 'displayName': (x) => _displayName.value = x,
  144. });
  145. map.pickPersistentProp(
  146. 'userInfo',
  147. (x) {
  148. if (x != null) {
  149. _userInfo = UserDTO2.fromJson(x);
  150. _headImageToken.value = _userInfo?.headImageToken ?? '';
  151. _displayName.value = _userInfo?.realName ?? _userInfo?.userName ?? "";
  152. }
  153. },
  154. );
  155. map.pickPersistentProp('selectPatientInfo', (x) {
  156. if (x != null) {
  157. _updateSelectPatientInfo(PatientDTO.fromJson(x));
  158. }
  159. });
  160. map.pickPersistentProp('menuPermissionList', (x) {
  161. if (x != null) {
  162. _updateMenuPermissionList(
  163. (x as List).map((e) => UserFeatureDTO.fromJson(e)));
  164. }
  165. });
  166. map.pickPersistentProp('operationPermissionList', (x) {
  167. if (x != null) {
  168. _updateOperationPermissionList(
  169. (x as List).map((e) => UserFeatureDTO.fromJson(e)));
  170. }
  171. });
  172. }
  173. @override
  174. Map<String, dynamic> toPersistenceJson() {
  175. return {
  176. "token": token,
  177. "isAutoLogin": isAutoLogin,
  178. "account": account,
  179. "password": password,
  180. "displayName": displayName,
  181. "userInfo": _userInfo,
  182. "features": _features,
  183. "selectPatientInfo": _currentSelectPatientInfo,
  184. "menuPermissionList": _menuPermissionList,
  185. "operationPermissionList": _operationPermissionList,
  186. };
  187. }
  188. void _updateFeatures(Iterable<String> features) {
  189. _features.value = List.unmodifiable(features);
  190. }
  191. void _updateMenuPermissionList(Iterable<UserFeatureDTO> permissionList) {
  192. _menuPermissionList.value = List.unmodifiable(permissionList);
  193. }
  194. void _updateOperationPermissionList(Iterable<UserFeatureDTO> permissionList) {
  195. _operationPermissionList.value = List.unmodifiable(permissionList);
  196. }
  197. void _updateSelectPatientInfo(PatientDTO patient) {
  198. _currentSelectPatientInfo.value = patient;
  199. }
  200. void _clearFeatures() => _features.value = [];
  201. // 更新RPC响应秘钥
  202. void _updateRpcResEncryptKey(String key) {
  203. final originResEncryptCfg = rpc.responseEncryptConfig;
  204. rpc.responseEncryptConfig = JsonRpcEncryptConfig(
  205. enable: originResEncryptCfg.enable,
  206. encryptMode: originResEncryptCfg.encryptMode,
  207. encryptKey: key,
  208. );
  209. }
  210. Future<void> _fetchFeatures() async {
  211. final accountController = Get.find<IAccountManager>();
  212. _clearFeatures();
  213. try {
  214. final items = await accountController.getOperationPermission();
  215. final menuPermission = await accountController.getMenuPermission();
  216. _menuPermissionList.value = menuPermission ?? [];
  217. _operationPermissionList.value = items ?? [];
  218. final featureKeys = items!.map((e) => e.code!);
  219. _updateFeatures(featureKeys);
  220. _updateMenuPermissionList(_menuPermissionList);
  221. _updateOperationPermissionList(_operationPermissionList);
  222. } catch (e) {
  223. logger.e("UserState fetch features error.", e);
  224. }
  225. }
  226. }