account.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import 'package:fis_jsonrpc/rpc.dart';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:vitalapp/architecture/utils/common_util.dart';
  4. import 'package:vitalapp/rpc.dart';
  5. import 'package:vitalapp/store/store.dart';
  6. import 'package:fis_common/helpers/encrypt.dart';
  7. import 'package:fis_common/logger/logger.dart';
  8. import 'interfaces/account.dart';
  9. class AccountManager implements IAccountManager {
  10. @override
  11. Future<int> login(String account, String password, bool isAutoLogin) async {
  12. int code = 0;
  13. try {
  14. final symmetryEncryptPassword =
  15. password.isEmpty ? '' : FEncryptHelper.symmetryEncrypt(password);
  16. final result = await rpc.vitalLogin.commonLoginAsync(
  17. CommonLoginRequest2(
  18. anyAccount: account,
  19. password: symmetryEncryptPassword,
  20. loginSource: LoginSource.Pad,
  21. ),
  22. );
  23. await Store.user
  24. .handleLogin(account, result.token!, password, isAutoLogin);
  25. } catch (e) {
  26. // TODO 待提取封装公用函数
  27. code = -1;
  28. if (e is JsonRpcException) {
  29. if (e is JsonRpcNetworkException) {
  30. code = -1001; // 网络异常
  31. if (e.message?.toLowerCase().contains("timeout") == true) {
  32. code = -1002; // 超时
  33. }
  34. } else if (e.data?.code != null) {
  35. code = e.data.code!;
  36. } else if (e.code != null) {
  37. code = e.code!;
  38. }
  39. }
  40. }
  41. return code;
  42. }
  43. @override
  44. Future<bool> logout() async {
  45. await Store.user.handleLogOut();
  46. try {
  47. await rpc.login.loginOutAsync(
  48. TokenRequest(token: Store.user.token),
  49. );
  50. } catch (e) {
  51. logger.e("AccountManager call logout error", e);
  52. }
  53. if (!kIsWeb) {
  54. CommonUtil.idCardRecognition.release();
  55. }
  56. return true;
  57. }
  58. @override
  59. Future<UserDTO2?> getUserDetailAsync() async {
  60. try {
  61. return await rpc.vitalUser.getUserDetailAsync(
  62. GetUserRequest(
  63. token: Store.user.token,
  64. code: Store.user.userCode,
  65. ),
  66. );
  67. } catch (e) {
  68. return null;
  69. }
  70. }
  71. @override
  72. Future<bool> updateUserAsync(UserDTO2 dto) async {
  73. try {
  74. var request = UpdateUserRequest(
  75. token: Store.user.token,
  76. code: dto.code,
  77. realName: dto.realName,
  78. phone: dto.phone,
  79. headImageToken: dto.headImageToken,
  80. );
  81. var result = await rpc.vitalUser.updateUserAsync(request);
  82. if (result) {
  83. await Store.user.updateUserInfo(dto);
  84. }
  85. return result;
  86. } catch (e) {}
  87. return false;
  88. }
  89. @override
  90. Future<String> getUserPasswordDetailAsync() async {
  91. try {
  92. var result = await rpc.vitalUserPassword.getUserPasswordDetailAsync(
  93. GetUserPasswordRequest(
  94. token: Store.user.token, code: Store.user.userCode));
  95. return result.secretPassword ?? '';
  96. } catch (e) {}
  97. return '';
  98. }
  99. @override
  100. Future<bool> updateUserPasswordAsync(String secretPassword) async {
  101. try {
  102. var result = await rpc.vitalUserPassword.updateUserPasswordAsync(
  103. UpdateUserPasswordRequest(
  104. token: Store.user.token,
  105. code: Store.user.userCode,
  106. secretPassword: secretPassword));
  107. return result;
  108. } catch (e) {}
  109. return false;
  110. }
  111. @override
  112. Future<List<UserFeatureDTO>?> getMenuPermission() async {
  113. try {
  114. var result = await rpc.vitalUser.getMenuPermissionAsync(
  115. GetMenuPermissionRequest(
  116. token: Store.user.token,
  117. appType: ApplicationTypeEnum.App,
  118. ),
  119. );
  120. for (var element in result) {
  121. print(element.toJson());
  122. }
  123. return result;
  124. } catch (e) {
  125. logger.e("AccountManager call getMenuPermission error", e);
  126. }
  127. return null;
  128. }
  129. @override
  130. Future<List<UserFeatureDTO>?> getOperationPermission() async {
  131. try {
  132. var request = GetOperationPermissionRequest(
  133. token: Store.user.token,
  134. appType: ApplicationTypeEnum.App,
  135. );
  136. var result = await rpc.vitalUser.getOperationPermissionAsync(request);
  137. // var json = jsonEncode(request);
  138. // print(jsonEncode(request.toJson()));
  139. for (var element in result) {
  140. print(element.toJson());
  141. logger.i('新增权限${element.toJson()}');
  142. }
  143. return result;
  144. } catch (e) {
  145. logger.e("AccountManager call getMenuPermission error", e);
  146. }
  147. return null;
  148. }
  149. }