login_controller.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:get/get.dart';
  3. import 'package:injectable/injectable.dart';
  4. import 'package:keyboard/base/base_controller.dart';
  5. import 'package:keyboard/dialog/login/login_dialog.dart';
  6. import '../../data/consts/error_code.dart';
  7. import '../../data/consts/event_report.dart';
  8. import '../../data/repository/account_repository.dart';
  9. import '../../dialog/privacy_agreement_dialog.dart';
  10. import '../../handler/event_handler.dart';
  11. import '../../handler/wechat_login_service.dart';
  12. import '../../resource/string.gen.dart';
  13. import '../../utils/error_handler.dart';
  14. import '../../utils/http_handler.dart';
  15. import '../../utils/toast_util.dart';
  16. @injectable
  17. class LoginController extends BaseController {
  18. final AccountRepository accountRepository;
  19. final WechatLoginService wechatLoginService;
  20. final RxString _phone = ''.obs;
  21. final RxString _code = ''.obs;
  22. String get phone => _phone.value;
  23. String get code => _code.value;
  24. final int _countDownTime = 60;
  25. final RxnInt _countDown = RxnInt();
  26. int? get countDown => _countDown.value;
  27. final RxBool _isAgree = false.obs;
  28. bool get isAgree => _isAgree.value;
  29. final RxBool _isFirstSend = true.obs;
  30. bool get isFirstSend => _isFirstSend.value;
  31. LoginController(this.accountRepository, this.wechatLoginService);
  32. @override
  33. void onInit() {
  34. super.onInit();
  35. }
  36. void onPhoneChanged(String value) {
  37. _phone.value = value;
  38. }
  39. void onCodeChanged(String value) {
  40. _code.value = value;
  41. }
  42. void onBackClick() {
  43. Get.back();
  44. }
  45. void clickAgree() {
  46. _isAgree.value = !_isAgree.value;
  47. }
  48. void onSendVerificationCode() async {
  49. if (_countDown.value != null) {
  50. return;
  51. }
  52. if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
  53. ToastUtil.show(StringName.loginPrintPhoneVerification);
  54. return;
  55. }
  56. if (!_isAgree.value) {
  57. PrivacyAgreementDialog.show(
  58. btnConfirm: () async {
  59. _isAgree.value = true;
  60. onSendVerificationCode();
  61. },
  62. );
  63. return;
  64. }
  65. try {
  66. await accountRepository.loginSendCode(phone);
  67. _countDown.value = _countDownTime;
  68. _startCountDown();
  69. } catch (error) {
  70. if (error is RequestCodeTooOftenException) {
  71. ToastUtil.show(StringName.loginRequestCodeFrequentlyToast);
  72. } else if (error is ServerErrorException) {
  73. ToastUtil.show(error.message);
  74. } else {
  75. ToastUtil.show(StringName.loginVerificationCodeRequestFailedToast);
  76. }
  77. }
  78. }
  79. void onLoginClick() {
  80. if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
  81. ToastUtil.show(StringName.loginPrintPhoneVerification);
  82. return;
  83. }
  84. if (!_isAgree.value) {
  85. PrivacyAgreementDialog.show(
  86. btnConfirm: () async {
  87. _isAgree.value = true;
  88. onLoginClick();
  89. },
  90. );
  91. ToastUtil.show(StringName.loginAgreePrivacy);
  92. return;
  93. }
  94. if (code.isEmpty) {
  95. ToastUtil.show(StringName.loginPrintVerificationCode);
  96. return;
  97. }
  98. accountRepository
  99. .loginUserLogin(phone, code)
  100. .then((data) {
  101. EventHandler.report(EventId.event_04003);
  102. Get.back();
  103. ToastUtil.show(StringName.loginSuccess);
  104. })
  105. .catchError((error) {
  106. EventHandler.report(EventId.event_04004);
  107. if (error is LoginTooOftenException) {
  108. ToastUtil.show(StringName.loginTooOftenToast);
  109. return;
  110. }
  111. if (error is ServerErrorException) {
  112. if (error.code == ErrorCode.verificationCodeError) {
  113. ToastUtil.show(StringName.loginVerificationCodeErrorToast);
  114. } else {
  115. ToastUtil.show(error.message);
  116. }
  117. } else {
  118. ErrorHandler.toastError(error);
  119. }
  120. });
  121. }
  122. void clickBack() {
  123. EventHandler.report(EventId.event_04005);
  124. Get.back();
  125. }
  126. void _startCountDown() {
  127. Future.delayed(Duration(seconds: 1), () {
  128. int? time = _countDown.value;
  129. _isFirstSend.value = false;
  130. if (time != null) {
  131. _countDown.value = time - 1;
  132. if (time > 0) {
  133. _startCountDown();
  134. } else {
  135. _countDown.value = null;
  136. }
  137. }
  138. });
  139. }
  140. @override
  141. void onClose() {
  142. super.onClose();
  143. _countDown.value = null;
  144. }
  145. void clickWxLogin() async {
  146. if (!_isAgree.value) {
  147. Get.back();
  148. LoginDialog.show();
  149. return;
  150. }
  151. }
  152. }