login_controller.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 '../../data/consts/error_code.dart';
  6. import '../../data/repository/account_repository.dart';
  7. import '../../dialog/privacy_agreement_dialog.dart';
  8. import '../../handler/wechat_login_service.dart';
  9. import '../../resource/string.gen.dart';
  10. import '../../utils/http_handler.dart';
  11. import '../../utils/toast_util.dart';
  12. @injectable
  13. class LoginController extends BaseController {
  14. final AccountRepository accountRepository;
  15. final WechatLoginService wechatLoginService;
  16. final RxString _phone = ''.obs;
  17. final RxString _code = ''.obs;
  18. String get phone => _phone.value;
  19. String get code => _code.value;
  20. final int _countDownTime = 60;
  21. final RxnInt _countDown = RxnInt();
  22. int? get countDown => _countDown.value;
  23. final RxBool _isAgree = false.obs;
  24. bool get isAgree => _isAgree.value;
  25. final RxBool _isFirstSend = true.obs;
  26. bool get isFirstSend => _isFirstSend.value;
  27. LoginController(this.accountRepository, this.wechatLoginService);
  28. @override
  29. void onInit() {
  30. super.onInit();
  31. }
  32. void onPhoneChanged(String value) {
  33. _phone.value = value;
  34. }
  35. void onCodeChanged(String value) {
  36. _code.value = value;
  37. }
  38. void onBackClick() {
  39. Get.back();
  40. }
  41. void clickAgree() {
  42. _isAgree.value = !_isAgree.value;
  43. }
  44. void onSendVerificationCode() async {
  45. if (_countDown.value != null) {
  46. return;
  47. }
  48. if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
  49. ToastUtil.show(StringName.loginPrintPhoneVerification);
  50. return;
  51. }
  52. if (!_isAgree.value) {
  53. PrivacyAgreementDialog.show(
  54. btnConfirm: () async {
  55. _isAgree.value = true;
  56. onSendVerificationCode();
  57. },
  58. );
  59. return;
  60. }
  61. try {
  62. await accountRepository.loginSendCode(phone);
  63. _countDown.value = _countDownTime;
  64. _startCountDown();
  65. } catch (error) {
  66. if (error is RequestCodeTooOftenException) {
  67. ToastUtil.show(StringName.loginRequestCodeFrequentlyToast);
  68. } else if (error is ServerErrorException) {
  69. ToastUtil.show(error.message);
  70. } else {
  71. ToastUtil.show(StringName.loginVerificationCodeRequestFailedToast);
  72. }
  73. }
  74. }
  75. void onLoginClick() {
  76. if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
  77. ToastUtil.show(StringName.loginPrintPhoneVerification);
  78. return;
  79. }
  80. if (!_isAgree.value) {
  81. PrivacyAgreementDialog.show(
  82. btnConfirm: () async {
  83. _isAgree.value = true;
  84. onLoginClick();
  85. },
  86. );
  87. ToastUtil.show(StringName.loginAgreePrivacy);
  88. return;
  89. }
  90. if (code.isEmpty) {
  91. ToastUtil.show(StringName.loginPrintVerificationCode);
  92. return;
  93. }
  94. accountRepository
  95. .loginUserLogin(phone, code)
  96. .then((data) {
  97. Get.back();
  98. ToastUtil.show(StringName.loginSuccess);
  99. })
  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. void _startCountDown() {
  117. Future.delayed(Duration(seconds: 1), () {
  118. int? time = _countDown.value;
  119. _isFirstSend.value = false;
  120. if (time != null) {
  121. _countDown.value = time - 1;
  122. if (time > 0) {
  123. _startCountDown();
  124. } else {
  125. _countDown.value = null;
  126. }
  127. }
  128. });
  129. }
  130. @override
  131. void onClose() {
  132. super.onClose();
  133. _countDown.value = null;
  134. }
  135. void clickWxLogin() async {
  136. if (!_isAgree.value) {
  137. PrivacyAgreementDialog.show(
  138. btnConfirm: () async {
  139. _isAgree.value = true;
  140. clickWxLogin();
  141. },
  142. );
  143. return;
  144. }
  145. wechatLoginService.login(
  146. onSuccess: (code) {
  147. debugPrint("登录成功 code: $code");
  148. accountRepository.wechatLogin(code).then((data) {
  149. Get.back();
  150. ToastUtil.show(StringName.loginSuccess);
  151. }).catchError((error) {
  152. if (error is ServerErrorException) {
  153. if (error.code == ErrorCode.verificationCodeError) {
  154. ToastUtil.show(StringName.loginVerificationCodeErrorToast);
  155. } else {
  156. ToastUtil.show(error.message);
  157. }
  158. } else {
  159. ToastUtil.show(StringName.loginFailedToast);
  160. }
  161. });
  162. },
  163. onError: (code, msg) {
  164. ToastUtil.show("微信登录失败:$msg");
  165. },
  166. onCancel: () {
  167. ToastUtil.show("用户取消登录");
  168. },
  169. );
  170. }
  171. }