login_controller.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import 'package:get/get.dart';
  2. import 'package:get/get_core/src/get_main.dart';
  3. import 'package:injectable/injectable.dart';
  4. import 'package:location/base/base_controller.dart';
  5. import 'package:location/data/consts/error_code.dart';
  6. import 'package:location/resource/string.gen.dart';
  7. import 'package:location/utils/http_handler.dart';
  8. import 'package:location/utils/toast_util.dart';
  9. import '../../data/repositories/account_repository.dart';
  10. import '../../utils/de_bounce.dart';
  11. @injectable
  12. class LoginController extends BaseController {
  13. final RxString _phone = ''.obs;
  14. final RxString _code = ''.obs;
  15. String get phone => _phone.value;
  16. String get code => _code.value;
  17. final int _countDownTime = 60;
  18. final RxnInt _countDown = RxnInt();
  19. int? get countDown => _countDown.value;
  20. final RxBool _isAgreePrivacy = false.obs;
  21. bool get isAgreePrivacy => _isAgreePrivacy.value;
  22. final AccountRepository accountRepository;
  23. LoginController(this.accountRepository);
  24. final Debounce _saveDebounce = Debounce(debounceTime: 1000);
  25. bool isRequestLogin = false;
  26. @override
  27. void onReady() {
  28. super.onReady();
  29. }
  30. void onPhoneChanged(String value) {
  31. _phone.value = value;
  32. }
  33. void onCodeChanged(String value) {
  34. _code.value = value;
  35. }
  36. void onBackClick() {
  37. Get.back();
  38. }
  39. void onSendVerificationCode() {
  40. _saveDebounce.onClick(() {
  41. sendVerificationCode();
  42. });
  43. }
  44. void sendVerificationCode() {
  45. if (_countDown.value != null) {
  46. return;
  47. }
  48. if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
  49. ToastUtil.show(StringName.loginPrintPhoneVerification);
  50. return;
  51. }
  52. if (!isAgreePrivacy) {
  53. ToastUtil.show(StringName.loginAgreePrivacy);
  54. return;
  55. }
  56. accountRepository.loginSendCode(phone).then((value) {
  57. _countDown.value = _countDownTime;
  58. _startCountDown();
  59. }).catchError((error) {
  60. if (error is RequestCodeTooOftenException) {
  61. ToastUtil.show(StringName.loginRequestCodeFrequentlyToast);
  62. return;
  63. }
  64. if (error is ServerErrorException) {
  65. ToastUtil.show(error.message);
  66. } else {
  67. ToastUtil.show(StringName.loginVerificationCodeRequestFailedToast);
  68. }
  69. });
  70. }
  71. void _startCountDown() {
  72. Future.delayed(Duration(seconds: 1), () {
  73. int? time = _countDown.value;
  74. if (time != null) {
  75. _countDown.value = time - 1;
  76. if (time > 0) {
  77. _startCountDown();
  78. } else {
  79. _countDown.value = null;
  80. }
  81. }
  82. });
  83. }
  84. void onPrivacyClick() {
  85. _isAgreePrivacy.value = !_isAgreePrivacy.value;
  86. }
  87. @override
  88. void onClose() {
  89. super.onClose();
  90. _countDown.value = null;
  91. }
  92. void onLoginClick() {
  93. _saveDebounce.onClick(() {
  94. login();
  95. });
  96. }
  97. void login() {
  98. if (isRequestLogin) {
  99. return;
  100. }
  101. if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
  102. ToastUtil.show(StringName.loginPrintPhoneVerification);
  103. return;
  104. }
  105. if (!isAgreePrivacy) {
  106. ToastUtil.show(StringName.loginAgreePrivacy);
  107. return;
  108. }
  109. if (code.isEmpty) {
  110. ToastUtil.show(StringName.loginPrintVerificationCode);
  111. return;
  112. }
  113. isRequestLogin = true;
  114. accountRepository.loginUserLogin(phone, code).then((data) {
  115. Get.back();
  116. ToastUtil.show(StringName.loginSuccess);
  117. }).catchError((error) {
  118. isRequestLogin = false;
  119. if (error is LoginTooOftenException) {
  120. ToastUtil.show(StringName.loginTooOftenToast);
  121. return;
  122. }
  123. if (error is ServerErrorException) {
  124. if (error.code == ErrorCode.verificationCodeError) {
  125. ToastUtil.show(StringName.loginVerificationCodeErrorToast);
  126. } else {
  127. ToastUtil.show(error.message);
  128. }
  129. } else {
  130. ToastUtil.show(StringName.loginFailedToast);
  131. }
  132. });
  133. }
  134. }