login_controller.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/loading_dialog.dart';
  6. import 'package:keyboard/dialog/login/login_dialog.dart';
  7. import 'package:sign_in_with_apple/sign_in_with_apple.dart';
  8. import '../../data/consts/error_code.dart';
  9. import '../../data/consts/event_report.dart';
  10. import '../../data/repository/account_repository.dart';
  11. import '../../dialog/privacy_agreement_dialog.dart';
  12. import '../../handler/event_handler.dart';
  13. import '../../handler/wechat_login_service.dart';
  14. import '../../resource/string.gen.dart';
  15. import '../../utils/error_handler.dart';
  16. import '../../utils/http_handler.dart';
  17. import '../../utils/toast_util.dart';
  18. @injectable
  19. class LoginController extends BaseController {
  20. final AccountRepository accountRepository;
  21. final WechatLoginService wechatLoginService;
  22. final RxString _phone = ''.obs;
  23. final RxString _code = ''.obs;
  24. String get phone => _phone.value;
  25. String get code => _code.value;
  26. final int _countDownTime = 60;
  27. final RxnInt _countDown = RxnInt();
  28. int? get countDown => _countDown.value;
  29. final RxBool _isAgree = false.obs;
  30. bool get isAgree => _isAgree.value;
  31. final RxBool _isFirstSend = true.obs;
  32. bool get isFirstSend => _isFirstSend.value;
  33. LoginController(this.accountRepository, this.wechatLoginService);
  34. @override
  35. void onInit() {
  36. super.onInit();
  37. }
  38. void onPhoneChanged(String value) {
  39. _phone.value = value;
  40. }
  41. void onCodeChanged(String value) {
  42. _code.value = value;
  43. }
  44. void onBackClick() {
  45. Get.back();
  46. }
  47. void clickAgree() {
  48. _isAgree.value = !_isAgree.value;
  49. }
  50. void onSendVerificationCode() async {
  51. if (_countDown.value != null) {
  52. return;
  53. }
  54. if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
  55. ToastUtil.show(StringName.loginPrintPhoneVerification);
  56. return;
  57. }
  58. if (!_isAgree.value) {
  59. PrivacyAgreementDialog.show(
  60. btnConfirm: () async {
  61. _isAgree.value = true;
  62. onSendVerificationCode();
  63. },
  64. );
  65. return;
  66. }
  67. try {
  68. await accountRepository.loginSendCode(phone);
  69. _countDown.value = _countDownTime;
  70. _startCountDown();
  71. } catch (error) {
  72. if (error is RequestCodeTooOftenException) {
  73. ToastUtil.show(StringName.loginRequestCodeFrequentlyToast);
  74. } else if (error is ServerErrorException) {
  75. ToastUtil.show(error.message);
  76. } else {
  77. ToastUtil.show(StringName.loginVerificationCodeRequestFailedToast);
  78. }
  79. }
  80. }
  81. void onLoginClick() {
  82. if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
  83. ToastUtil.show(StringName.loginPrintPhoneVerification);
  84. return;
  85. }
  86. if (!_isAgree.value) {
  87. PrivacyAgreementDialog.show(
  88. btnConfirm: () async {
  89. _isAgree.value = true;
  90. onLoginClick();
  91. },
  92. );
  93. ToastUtil.show(StringName.loginAgreePrivacy);
  94. return;
  95. }
  96. if (code.isEmpty) {
  97. ToastUtil.show(StringName.loginPrintVerificationCode);
  98. return;
  99. }
  100. accountRepository
  101. .loginUserLogin(phone, code)
  102. .then((data) {
  103. EventHandler.report(EventId.event_04003);
  104. Get.back();
  105. ToastUtil.show(StringName.loginSuccess);
  106. })
  107. .catchError((error) {
  108. EventHandler.report(EventId.event_04004);
  109. if (error is LoginTooOftenException) {
  110. ToastUtil.show(StringName.loginTooOftenToast);
  111. return;
  112. }
  113. if (error is ServerErrorException) {
  114. if (error.code == ErrorCode.verificationCodeError) {
  115. ToastUtil.show(StringName.loginVerificationCodeErrorToast);
  116. } else {
  117. ToastUtil.show(error.message);
  118. }
  119. } else {
  120. ErrorHandler.toastError(error);
  121. }
  122. });
  123. }
  124. void clickBack() {
  125. EventHandler.report(EventId.event_04005);
  126. Get.back();
  127. }
  128. void _startCountDown() {
  129. Future.delayed(Duration(seconds: 1), () {
  130. int? time = _countDown.value;
  131. _isFirstSend.value = false;
  132. if (time != null) {
  133. _countDown.value = time - 1;
  134. if (time > 0) {
  135. _startCountDown();
  136. } else {
  137. _countDown.value = null;
  138. }
  139. }
  140. });
  141. }
  142. @override
  143. void onClose() {
  144. super.onClose();
  145. _countDown.value = null;
  146. }
  147. void clickWxLogin() async {
  148. if (!_isAgree.value) {
  149. Get.back();
  150. LoginDialog.show();
  151. return;
  152. }
  153. }
  154. // 苹果登录点击
  155. void clickAppleLogin() async {
  156. if (!_isAgree.value) {
  157. PrivacyAgreementDialog.show(
  158. btnConfirm: () async {
  159. _isAgree.value = true;
  160. clickAppleLogin();
  161. },
  162. );
  163. ToastUtil.show(StringName.loginAgreePrivacy);
  164. return;
  165. }
  166. CustomLoadingDialog.show();
  167. AuthorizationCredentialAppleID credential;
  168. try {
  169. credential = await SignInWithApple.getAppleIDCredential(
  170. scopes: [
  171. AppleIDAuthorizationScopes.email,
  172. AppleIDAuthorizationScopes.fullName,
  173. ],
  174. );
  175. } catch (error) {
  176. CustomLoadingDialog.hide();
  177. ToastUtil.show(StringName.loginFailedToast);
  178. return;
  179. }
  180. var userIdentity = credential.userIdentifier ?? "";
  181. var authorizationCode = credential.authorizationCode ?? "";
  182. var identityToken = credential.identityToken ?? "";
  183. accountRepository
  184. .appleLogin(userIdentity, authorizationCode, identityToken)
  185. .then((data) {
  186. CustomLoadingDialog.hide();
  187. EventHandler.report(EventId.event_04003);
  188. Get.back();
  189. ToastUtil.show(StringName.loginSuccess);
  190. })
  191. .catchError((error) {
  192. CustomLoadingDialog.hide();
  193. EventHandler.report(EventId.event_04004);
  194. if (error is LoginTooOftenException) {
  195. ToastUtil.show(StringName.loginTooOftenToast);
  196. return;
  197. }
  198. if (error is ServerErrorException) {
  199. if (error.code == ErrorCode.verificationCodeError) {
  200. ToastUtil.show(StringName.loginVerificationCodeErrorToast);
  201. } else {
  202. ToastUtil.show(error.message);
  203. }
  204. } else {
  205. ErrorHandler.toastError(error);
  206. }
  207. });
  208. }
  209. }