user.dart 8.9 KB

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