login_controller.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/handler/error_handler.dart';
  7. import 'package:location/resource/string.gen.dart';
  8. import 'package:location/utils/http_handler.dart';
  9. import 'package:location/utils/toast_util.dart';
  10. import '../../data/repositories/account_repository.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. @override
  25. void onReady() {
  26. super.onReady();
  27. }
  28. void onPhoneChanged(String value) {
  29. _phone.value = value;
  30. }
  31. void onCodeChanged(String value) {
  32. _code.value = value;
  33. }
  34. void onBackClick() {
  35. Get.back();
  36. }
  37. void onSendVerificationCode() {
  38. if (_countDown.value != null) {
  39. return;
  40. }
  41. if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
  42. ToastUtil.show(StringName.loginPrintPhoneVerification);
  43. return;
  44. }
  45. if (!isAgreePrivacy) {
  46. ToastUtil.show(StringName.loginAgreePrivacy);
  47. return;
  48. }
  49. accountRepository.loginSendCode(phone).then((value) {
  50. _countDown.value = _countDownTime;
  51. _startCountDown();
  52. }).catchError((error) {
  53. if (error is RequestCodeTooOftenException) {
  54. ToastUtil.show(StringName.loginRequestCodeFrequentlyToast);
  55. return;
  56. }
  57. if (error is ServerErrorException) {
  58. ToastUtil.show(error.message);
  59. } else {
  60. ToastUtil.show(StringName.loginVerificationCodeRequestFailedToast);
  61. }
  62. });
  63. }
  64. void _startCountDown() {
  65. Future.delayed(Duration(seconds: 1), () {
  66. int? time = _countDown.value;
  67. if (time != null) {
  68. _countDown.value = time - 1;
  69. if (time > 0) {
  70. _startCountDown();
  71. } else {
  72. _countDown.value = null;
  73. }
  74. }
  75. });
  76. }
  77. void onPrivacyClick() {
  78. _isAgreePrivacy.value = !_isAgreePrivacy.value;
  79. }
  80. @override
  81. void onClose() {
  82. super.onClose();
  83. _countDown.value = null;
  84. }
  85. void onLoginClick() {
  86. if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
  87. ToastUtil.show(StringName.loginPrintPhoneVerification);
  88. return;
  89. }
  90. if (!isAgreePrivacy) {
  91. ToastUtil.show(StringName.loginAgreePrivacy);
  92. return;
  93. }
  94. if (code.isEmpty) {
  95. ToastUtil.show(StringName.loginPrintVerificationCode);
  96. return;
  97. }
  98. accountRepository.loginUserLogin(phone, code).then((data) {
  99. Get.back();
  100. ToastUtil.show(StringName.loginSuccess);
  101. }).catchError((error) {
  102. if (error is LoginTooOftenException) {
  103. ToastUtil.show(StringName.loginTooOftenToast);
  104. return;
  105. }
  106. if (error is ServerErrorException) {
  107. if (error.code == ErrorCode.verificationCodeError) {
  108. ToastUtil.show(StringName.loginVerificationCodeErrorToast);
  109. } else {
  110. ToastUtil.show(error.message);
  111. }
  112. } else {
  113. ToastUtil.show(StringName.loginFailedToast);
  114. }
  115. });
  116. }
  117. }