login_controller.dart 4.1 KB

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