| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import 'package:get/get.dart';
- import 'package:injectable/injectable.dart';
- import 'package:location/data/api/atmob_api.dart';
- import 'package:location/data/api/request/login_request.dart';
- import 'package:location/data/api/request/send_code_request.dart';
- import 'package:location/data/bean/member_status_info.dart';
- import 'package:location/data/consts/error_code.dart';
- import 'package:location/utils/atmob_log.dart';
- import 'package:location/utils/http_handler.dart';
- import 'package:location/utils/mmkv_util.dart';
- import '../api/response/login_response.dart';
- @lazySingleton
- class AccountRepository {
- final AtmobApi atmobApi;
- final String keyAccountLoginPhoneNum = 'key_account_login_phone_num';
- final String keyAccountLoginToken = 'key_account_login_token';
- RxnString loginPhoneNum = RxnString();
- RxBool isLogin = RxBool(false);
- Rxn<MemberStatusInfo> memberStatusInfo = Rxn<MemberStatusInfo>();
- int? _lastRequestCodeTime;
- int _errorCodeTimes = 0;
- static String? token;
- AccountRepository(this.atmobApi) {
- token = KVUtil.getString(keyAccountLoginToken, null);
- isLogin.bindStream(
- loginPhoneNum.map((value) {
- return value?.isNotEmpty == true;
- }),
- );
- loginPhoneNum.value = KVUtil.getString(keyAccountLoginPhoneNum, null);
- }
- Future<void> loginSendCode(String phoneNum) {
- final currentTime = DateTime.now().millisecondsSinceEpoch;
- // 检查是否在 60 秒内重复请求
- if (currentTime - (_lastRequestCodeTime ?? 0) < 60 * 1000) {
- throw RequestCodeTooOftenException();
- }
- return atmobApi
- .loginSendCode(SendCodeRequest(phoneNum))
- .then(HttpHandler.handle(true))
- .then((value) {
- _lastRequestCodeTime = currentTime;
- _errorCodeTimes = 0;
- });
- }
- Future<LoginResponse> loginUserLogin(
- String phoneNum, String verificationCode) {
- if (_errorCodeTimes >= 5) {
- throw LoginTooOftenException();
- }
- return atmobApi
- .loginUserLogin(LoginRequest(phoneNum, verificationCode))
- .then(HttpHandler.handle(true))
- .then((response) {
- _errorCodeTimes = 0;
- onLoginSuccess(phoneNum, response.authToken);
- return response;
- }).catchError((error) {
- if (error is ServerErrorException &&
- error.code == ErrorCode.verificationCodeError) {
- _errorCodeTimes++;
- }
- throw error;
- });
- }
- void onLoginSuccess(String phoneNum, String authToken) {
- AccountRepository.token = token;
- loginPhoneNum.value = phoneNum;
- KVUtil.putString(keyAccountLoginPhoneNum, phoneNum);
- KVUtil.putString(keyAccountLoginToken, authToken);
- refreshMemberStatus();
- }
- void refreshMemberStatus() {}
- }
- class RequestCodeTooOftenException implements Exception {
- final String message;
- /// 可选的构造函数,支持自定义错误信息
- RequestCodeTooOftenException([this.message = '请求验证码过于频繁']);
- @override
- String toString() => message;
- }
- class LoginTooOftenException implements Exception {
- final String message;
- /// 可选的构造函数,支持自定义错误信息
- LoginTooOftenException([this.message = '登录过于频繁']);
- @override
- String toString() => message;
- }
|