login_controller.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. @injectable
  11. class LoginController extends BaseController {
  12. final RxString _phone = ''.obs;
  13. final RxString _code = ''.obs;
  14. String get phone => _phone.value;
  15. String get code => _code.value;
  16. final int _countDownTime = 60;
  17. final RxnInt _countDown = RxnInt();
  18. int? get countDown => _countDown.value;
  19. final RxBool _isAgreePrivacy = false.obs;
  20. bool get isAgreePrivacy => _isAgreePrivacy.value;
  21. final AccountRepository accountRepository;
  22. LoginController(this.accountRepository);
  23. @override
  24. void onReady() {
  25. super.onReady();
  26. }
  27. void onPhoneChanged(String value) {
  28. _phone.value = value;
  29. }
  30. void onCodeChanged(String value) {
  31. _code.value = value;
  32. }
  33. void onBackClick() {
  34. Get.back();
  35. }
  36. void onSendVerificationCode() {
  37. if (_countDown.value != null) {
  38. return;
  39. }
  40. if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
  41. ToastUtil.show(StringName.loginPrintPhoneVerification);
  42. return;
  43. }
  44. if (!isAgreePrivacy) {
  45. ToastUtil.show(StringName.loginAgreePrivacy);
  46. return;
  47. }
  48. accountRepository.loginSendCode(phone).then((value) {
  49. _countDown.value = _countDownTime;
  50. _startCountDown();
  51. }).catchError((error) {
  52. if (error is RequestCodeTooOftenException) {
  53. ToastUtil.show(StringName.loginRequestCodeFrequentlyToast);
  54. return;
  55. }
  56. if (error is ServerErrorException) {
  57. ToastUtil.show(error.message);
  58. } else {
  59. ToastUtil.show(StringName.loginVerificationCodeRequestFailedToast);
  60. }
  61. });
  62. }
  63. void _startCountDown() {
  64. Future.delayed(Duration(seconds: 1), () {
  65. int? time = _countDown.value;
  66. if (time != null) {
  67. _countDown.value = time - 1;
  68. if (time > 0) {
  69. _startCountDown();
  70. } else {
  71. _countDown.value = null;
  72. }
  73. }
  74. });
  75. }
  76. void onPrivacyClick() {
  77. _isAgreePrivacy.value = !_isAgreePrivacy.value;
  78. }
  79. @override
  80. void onClose() {
  81. super.onClose();
  82. _countDown.value = null;
  83. }
  84. void onLoginClick() {
  85. if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
  86. ToastUtil.show(StringName.loginPrintPhoneVerification);
  87. return;
  88. }
  89. if (!isAgreePrivacy) {
  90. ToastUtil.show(StringName.loginAgreePrivacy);
  91. return;
  92. }
  93. if (code.isEmpty) {
  94. ToastUtil.show(StringName.loginPrintVerificationCode);
  95. return;
  96. }
  97. accountRepository.loginUserLogin(phone, code).then((data) {
  98. Get.back();
  99. ToastUtil.show(StringName.loginSuccess);
  100. }).catchError((error) {
  101. if (error is LoginTooOftenException) {
  102. ToastUtil.show(StringName.loginTooOftenToast);
  103. return;
  104. }
  105. if (error is ServerErrorException) {
  106. if (error.code == ErrorCode.verificationCodeError) {
  107. ToastUtil.show(StringName.loginVerificationCodeErrorToast);
  108. } else {
  109. ToastUtil.show(error.message);
  110. }
  111. } else {
  112. ToastUtil.show(StringName.loginFailedToast);
  113. }
  114. });
  115. }
  116. }