import 'package:fis_jsonrpc/rpc.dart'; import 'package:flutter/foundation.dart'; import 'package:vitalapp/architecture/utils/common_util.dart'; import 'package:vitalapp/rpc.dart'; import 'package:vitalapp/store/store.dart'; import 'package:fis_common/helpers/encrypt.dart'; import 'package:fis_common/logger/logger.dart'; import 'interfaces/account.dart'; class AccountManager implements IAccountManager { @override Future login(String account, String password, bool isAutoLogin) async { int code = 0; try { final symmetryEncryptPassword = password.isEmpty ? '' : FEncryptHelper.symmetryEncrypt(password); final result = await rpc.vitalLogin.commonLoginAsync( CommonLoginRequest2( anyAccount: account, password: symmetryEncryptPassword, loginSource: LoginSource2.Android, ), ); await Store.user .handleLogin(account, result.token!, password, isAutoLogin); } catch (e) { // TODO 待提取封装公用函数 code = -1; if (e is JsonRpcException) { if (e is JsonRpcNetworkException) { code = -1001; // 网络异常 if (e.message?.toLowerCase().contains("timeout") == true) { code = -1002; // 超时 } } else if (e.data?.code != null) { code = e.data.code!; } else if (e.code != null) { code = e.code!; } } } return code; } @override Future logout() async { await Store.user.handleLogOut(); try { await rpc.login.loginOutAsync( TokenRequest(token: Store.user.token), ); } catch (e) { logger.e("AccountManager call logout error", e); } if (!kIsWeb) { CommonUtil.idCardRecognition.release(); } return true; } @override Future getUserDetailAsync() async { try { return await rpc.vitalUser.getUserDetailAsync( GetUserRequest( token: Store.user.token, code: Store.user.userCode, ), ); } catch (e) { return null; } } @override Future updateUserAsync(UserDTO2 dto) async { try { var request = UpdateUserRequest( token: Store.user.token, code: dto.code, realName: dto.realName, phone: dto.phone, headImageToken: dto.headImageToken, ); var result = await rpc.vitalUser.updateUserAsync(request); if (result) { await Store.user.updateUserInfo(dto); } return result; } catch (e) {} return false; } @override Future getUserPasswordDetailAsync() async { try { var result = await rpc.vitalUserPassword.getUserPasswordDetailAsync( GetUserPasswordRequest( token: Store.user.token, code: Store.user.userCode)); return result.secretPassword ?? ''; } catch (e) {} return ''; } @override Future updateUserPasswordAsync(String secretPassword) async { try { var result = await rpc.vitalUserPassword.updateUserPasswordAsync( UpdateUserPasswordRequest( token: Store.user.token, code: Store.user.userCode, secretPassword: secretPassword)); return result; } catch (e) {} return false; } @override Future?> getMenuPermission() async { try { var result = await rpc.vitalUser.getMenuPermissionAsync( GetMenuPermissionRequest( token: Store.user.token, appType: ApplicationTypeEnum.App, ), ); for (var element in result) { print(element.toJson()); } return result; } catch (e) { logger.e("AccountManager call getMenuPermission error", e); } return null; } @override Future?> getOperationPermission() async { try { var request = GetOperationPermissionRequest( token: Store.user.token, appType: ApplicationTypeEnum.App, ); var result = await rpc.vitalUser.getOperationPermissionAsync(request); // var json = jsonEncode(request); // print(jsonEncode(request.toJson())); for (var element in result) { print(element.toJson()); logger.i('新增权限${element.toJson()}'); } return result; } catch (e) { logger.e("AccountManager call getMenuPermission error", e); } return null; } }