123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- 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<List<UserFeatureDTO>?> _menuPermissionList = Rx(null);
- final RxString _displayName = ''.obs;
- final RxList<String> _features = RxList<String>();
- final Rx<String> _headImageToken = Rx('');
- final Rx<PatientDTO?> _currentSelectPatientInfo = Rx(null);
- final Rx<bool> _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<UserFeatureDTO>? get menuPermissionList => _menuPermissionList.value;
- set menuPermissionList(List<UserFeatureDTO>? val) =>
- _menuPermissionList.value = val;
- /// 负责区域(村)集合
- List<StringKVModel> get residence =>
- _userInfo?.residence
- ?.map((e) => StringKVModel(e.code!, e.name!))
- .toList() ??
- [];
- /// 用户信息
- UserDTO? _userInfo;
- /// 处理登录
- Future<void> 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<ITemplateManager>().saveTemplate();
- _menuPermissionList.value =
- await Get.find<IAccountManager>().getMenuPermission();
- await Get.find<ISystemSettingManager>().getSettings(true);
- Future.delayed(const Duration(milliseconds: 300), () {
- isShowUserCard = true;
- });
- }
- /// 处理登出
- Future<void> handleLogOut() async {
- _token = null;
- isShowUserCard = false;
- _updateRpcResEncryptKey("");
- await Store.persistent();
- }
- /// 更新用户信息
- Future<void> updateUserInfo(UserDTO dto) async {
- _userInfo = dto;
- _headImageToken.value = dto.headImageToken ?? '';
- _displayName.value = _userInfo?.realName ?? _userInfo?.userName ?? "";
- await Store.persistent();
- }
- /// 更新用户签名
- Future<void> updateSignature(String value) async {
- if (_userInfo != null) {
- _userInfo!.signature = value;
- await Store.persistent();
- }
- }
- @override
- Future<void> acceptPersistenceJson(Map<String, dynamic> map) async {
- // key对应值不为空时,给相应字段赋值
- map.pickPersistentProp('token', (x) => _token = x);
- map.pickPersistentProp('isAutoLogin', (x) => _isAutoLogin = x);
- map.pickPersistentProp(
- 'features',
- (x) => _updateFeatures((x as List).cast<String>()),
- );
- 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<String, dynamic> toPersistenceJson() {
- return {
- "token": token,
- "isAutoLogin": isAutoLogin,
- "account": account,
- "password": password,
- "displayName": displayName,
- "userInfo": _userInfo,
- };
- }
- void _updateFeatures(Iterable<String> 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,
- );
- }
- }
|