account_repository.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'dart:async';
  2. import 'package:electronic_assistant/base/app_base_request.dart';
  3. import 'package:electronic_assistant/data/api/atmob_api.dart';
  4. import 'package:electronic_assistant/data/bean/member_info.dart';
  5. import 'package:electronic_assistant/data/repositories/task_repository.dart';
  6. import 'package:electronic_assistant/utils/async_util.dart';
  7. import 'package:electronic_assistant/utils/cancel_future.dart';
  8. import 'package:electronic_assistant/utils/event_bus.dart';
  9. import 'package:electronic_assistant/utils/mmkv_util.dart';
  10. import 'package:flutter/cupertino.dart';
  11. import 'package:get/get.dart';
  12. import '../../resource/string.gen.dart';
  13. import '../../utils/http_handler.dart';
  14. import '../api/request/login_request.dart';
  15. import '../api/request/verification_code_request.dart';
  16. import '../api/response/login_response.dart';
  17. import '../api/response/user_info_response.dart';
  18. import '../consts/error_code.dart';
  19. const String EventUserLogin = 'EventUserLogin';
  20. const String EventUserLogout = 'EventUserLogout';
  21. class AccountRepository {
  22. final ACCOUNT_TOKEN = 'account_token';
  23. final ACCOUNT_PHONE = 'account_phone';
  24. String? _token;
  25. String? _phone;
  26. final isLogin = false.obs;
  27. Rxn<MemberInfo> memberInfo = Rxn();
  28. CancelableFuture? _getUserInfoFuture;
  29. AccountRepository._() {
  30. debugPrint('AccountRepository init');
  31. _token = KVUtil.getString(ACCOUNT_TOKEN, null);
  32. _phone = KVUtil.getString(ACCOUNT_PHONE, null);
  33. if (_token != null && _token!.isNotEmpty) {
  34. isLogin.value = true;
  35. Future.delayed(Duration.zero, () {
  36. refreshUserInfo();
  37. taskRepository.startUnfinishedTask();
  38. });
  39. }
  40. }
  41. String? get phone => _phone;
  42. String? get token => _token;
  43. Future<void> getVerificationCode(String phone) {
  44. return atmobApi
  45. .getVerificationCode(VerificationCodeRequest(phone))
  46. .then(HttpHandler.handle(true));
  47. }
  48. Future<void>? refreshUserInfo() {
  49. if (_getUserInfoFuture != null) {
  50. _getUserInfoFuture?.cancel();
  51. }
  52. _getUserInfoFuture = AsyncUtil.retryWithExponentialBackoff(
  53. () => _getUserInfo(), 10, (error) {
  54. if (error is ServerErrorException) {
  55. return error.code != ErrorCode.ERROR_CODE_NO_LOGIN;
  56. }
  57. return true;
  58. });
  59. return _getUserInfoFuture;
  60. }
  61. Future<UserInfoResponse> _getUserInfo() {
  62. return atmobApi
  63. .userInfo(AppBaseRequest())
  64. .then(HttpHandler.handle(false))
  65. .then((response) {
  66. memberInfo.value = response.memberInfo;
  67. return response;
  68. });
  69. }
  70. Future<LoginResponse> login(String phone, String code) {
  71. return atmobApi
  72. .login(LoginRequest(phone, code))
  73. .then(HttpHandler.handle(false))
  74. .then((response) {
  75. onLoginSuccess(phone, response.authToken);
  76. return response;
  77. });
  78. }
  79. void onLoginSuccess(String phone, String? token) {
  80. _token = token;
  81. _phone = phone;
  82. KVUtil.putString(ACCOUNT_TOKEN, token);
  83. KVUtil.putString(ACCOUNT_PHONE, phone);
  84. isLogin.value = true;
  85. refreshUserInfo();
  86. taskRepository.startUnfinishedTask();
  87. eventBus.emit(EventUserLogin);
  88. }
  89. void logout() {
  90. _phone = null;
  91. _phone = null;
  92. KVUtil.putString(ACCOUNT_TOKEN, null);
  93. KVUtil.putString(ACCOUNT_PHONE, null);
  94. isLogin.value = false;
  95. taskRepository.stopTask();
  96. memberInfo.value = null;
  97. eventBus.emit(EventUserLogout);
  98. }
  99. void cleanLoginInfo() {
  100. _phone = null;
  101. _phone = null;
  102. KVUtil.putString(ACCOUNT_TOKEN, null);
  103. KVUtil.putString(ACCOUNT_PHONE, null);
  104. isLogin.value = false;
  105. }
  106. getUserSubName(String? phone) {
  107. String name = StringName.account.tr;
  108. if (phone == null) {
  109. return name;
  110. }
  111. //后4位
  112. if (phone.length > 4) {
  113. phone = phone.substring(phone.length - 4);
  114. }
  115. return '$name$phone';
  116. }
  117. }
  118. final accountRepository = AccountRepository._();