login_controller.dart 6.5 KB

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