login_controller.dart 4.1 KB

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