user.dart 8.3 KB

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