account_repository.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import 'package:get/get.dart';
  2. import 'package:injectable/injectable.dart';
  3. import 'package:location/data/api/atmob_api.dart';
  4. import 'package:location/data/api/request/login_request.dart';
  5. import 'package:location/data/api/request/send_code_request.dart';
  6. import 'package:location/data/bean/member_status_info.dart';
  7. import 'package:location/data/consts/error_code.dart';
  8. import 'package:location/utils/atmob_log.dart';
  9. import 'package:location/utils/http_handler.dart';
  10. import 'package:location/utils/mmkv_util.dart';
  11. import '../api/response/login_response.dart';
  12. @lazySingleton
  13. class AccountRepository {
  14. final AtmobApi atmobApi;
  15. final String keyAccountLoginPhoneNum = 'key_account_login_phone_num';
  16. final String keyAccountLoginToken = 'key_account_login_token';
  17. RxnString loginPhoneNum = RxnString();
  18. RxBool isLogin = RxBool(false);
  19. Rxn<MemberStatusInfo> memberStatusInfo = Rxn<MemberStatusInfo>();
  20. int? _lastRequestCodeTime;
  21. int _errorCodeTimes = 0;
  22. static String? token;
  23. AccountRepository(this.atmobApi) {
  24. token = KVUtil.getString(keyAccountLoginToken, null);
  25. isLogin.bindStream(
  26. loginPhoneNum.map((value) {
  27. return value?.isNotEmpty == true;
  28. }),
  29. );
  30. loginPhoneNum.value = KVUtil.getString(keyAccountLoginPhoneNum, null);
  31. }
  32. Future<void> loginSendCode(String phoneNum) {
  33. final currentTime = DateTime.now().millisecondsSinceEpoch;
  34. // 检查是否在 60 秒内重复请求
  35. if (currentTime - (_lastRequestCodeTime ?? 0) < 60 * 1000) {
  36. throw RequestCodeTooOftenException();
  37. }
  38. return atmobApi
  39. .loginSendCode(SendCodeRequest(phoneNum))
  40. .then(HttpHandler.handle(true))
  41. .then((value) {
  42. _lastRequestCodeTime = currentTime;
  43. _errorCodeTimes = 0;
  44. });
  45. }
  46. Future<LoginResponse> loginUserLogin(
  47. String phoneNum, String verificationCode) {
  48. if (_errorCodeTimes >= 5) {
  49. throw LoginTooOftenException();
  50. }
  51. return atmobApi
  52. .loginUserLogin(LoginRequest(phoneNum, verificationCode))
  53. .then(HttpHandler.handle(true))
  54. .then((response) {
  55. _errorCodeTimes = 0;
  56. onLoginSuccess(phoneNum, response.authToken);
  57. return response;
  58. }).catchError((error) {
  59. if (error is ServerErrorException &&
  60. error.code == ErrorCode.verificationCodeError) {
  61. _errorCodeTimes++;
  62. }
  63. throw error;
  64. });
  65. }
  66. void onLoginSuccess(String phoneNum, String authToken) {
  67. AccountRepository.token = token;
  68. loginPhoneNum.value = phoneNum;
  69. KVUtil.putString(keyAccountLoginPhoneNum, phoneNum);
  70. KVUtil.putString(keyAccountLoginToken, authToken);
  71. refreshMemberStatus();
  72. }
  73. void refreshMemberStatus() {}
  74. }
  75. class RequestCodeTooOftenException implements Exception {
  76. final String message;
  77. /// 可选的构造函数,支持自定义错误信息
  78. RequestCodeTooOftenException([this.message = '请求验证码过于频繁']);
  79. @override
  80. String toString() => message;
  81. }
  82. class LoginTooOftenException implements Exception {
  83. final String message;
  84. /// 可选的构造函数,支持自定义错误信息
  85. LoginTooOftenException([this.message = '登录过于频繁']);
  86. @override
  87. String toString() => message;
  88. }