| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- import 'dart:async';
- import 'package:electronic_assistant/base/app_base_request.dart';
- import 'package:electronic_assistant/data/api/atmob_api.dart';
- import 'package:electronic_assistant/data/bean/member_info.dart';
- import 'package:electronic_assistant/data/repositories/task_repository.dart';
- import 'package:electronic_assistant/utils/async_util.dart';
- import 'package:electronic_assistant/utils/cancel_future.dart';
- import 'package:electronic_assistant/utils/event_bus.dart';
- import 'package:electronic_assistant/utils/mmkv_util.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:get/get.dart';
- import '../../resource/string.gen.dart';
- import '../../utils/http_handler.dart';
- import '../api/request/login_request.dart';
- import '../api/request/verification_code_request.dart';
- import '../api/response/login_response.dart';
- import '../api/response/user_info_response.dart';
- import '../consts/error_code.dart';
- const String EventUserLogin = 'EventUserLogin';
- const String EventUserLogout = 'EventUserLogout';
- class AccountRepository {
- final ACCOUNT_TOKEN = 'account_token';
- final ACCOUNT_PHONE = 'account_phone';
- String? _token;
- String? _phone;
- final isLogin = false.obs;
- Rxn<MemberInfo> memberInfo = Rxn();
- CancelableFuture? _getUserInfoFuture;
- AccountRepository._() {
- debugPrint('AccountRepository init');
- _token = KVUtil.getString(ACCOUNT_TOKEN, null);
- _phone = KVUtil.getString(ACCOUNT_PHONE, null);
- if (_token != null && _token!.isNotEmpty) {
- isLogin.value = true;
- Future.delayed(Duration.zero, () {
- refreshUserInfo();
- taskRepository.startUnfinishedTask();
- });
- }
- }
- String? get phone => _phone;
- String? get token => _token;
- Future<void> getVerificationCode(String phone) {
- return atmobApi
- .getVerificationCode(VerificationCodeRequest(phone))
- .then(HttpHandler.handle(true));
- }
- Future<void>? refreshUserInfo() {
- if (_getUserInfoFuture != null) {
- _getUserInfoFuture?.cancel();
- }
- _getUserInfoFuture = AsyncUtil.retryWithExponentialBackoff(
- () => _getUserInfo(), 10, (error) {
- if (error is ServerErrorException) {
- return error.code != ErrorCode.ERROR_CODE_NO_LOGIN;
- }
- return true;
- });
- return _getUserInfoFuture;
- }
- Future<UserInfoResponse> _getUserInfo() {
- return atmobApi
- .userInfo(AppBaseRequest())
- .then(HttpHandler.handle(false))
- .then((response) {
- memberInfo.value = response.memberInfo;
- return response;
- });
- }
- Future<LoginResponse> login(String phone, String code) {
- return atmobApi
- .login(LoginRequest(phone, code))
- .then(HttpHandler.handle(false))
- .then((response) {
- onLoginSuccess(phone, response.authToken);
- return response;
- });
- }
- void onLoginSuccess(String phone, String? token) {
- _token = token;
- _phone = phone;
- KVUtil.putString(ACCOUNT_TOKEN, token);
- KVUtil.putString(ACCOUNT_PHONE, phone);
- isLogin.value = true;
- refreshUserInfo();
- taskRepository.startUnfinishedTask();
- eventBus.emit(EventUserLogin);
- }
- void logout() {
- _phone = null;
- _phone = null;
- KVUtil.putString(ACCOUNT_TOKEN, null);
- KVUtil.putString(ACCOUNT_PHONE, null);
- isLogin.value = false;
- taskRepository.stopTask();
- memberInfo.value = null;
- eventBus.emit(EventUserLogout);
- }
- void cleanLoginInfo() {
- _phone = null;
- _phone = null;
- KVUtil.putString(ACCOUNT_TOKEN, null);
- KVUtil.putString(ACCOUNT_PHONE, null);
- isLogin.value = false;
- }
- getUserSubName(String? phone) {
- String name = StringName.account.tr;
- if (phone == null) {
- return name;
- }
- //后4位
- if (phone.length > 4) {
- phone = phone.substring(phone.length - 4);
- }
- return '$name$phone';
- }
- }
- final accountRepository = AccountRepository._();
|