| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- import 'package:flutter/cupertino.dart';
- import 'package:get/get.dart';
- import 'package:injectable/injectable.dart';
- import 'package:keyboard/base/base_controller.dart';
- import 'package:keyboard/dialog/loading_dialog.dart';
- import 'package:keyboard/dialog/login/login_dialog.dart';
- import 'package:sign_in_with_apple/sign_in_with_apple.dart';
- import 'package:wechat_kit/wechat_kit.dart';
- import '../../data/consts/error_code.dart';
- import '../../data/consts/event_report.dart';
- import '../../data/repository/account_repository.dart';
- import '../../dialog/privacy_agreement_dialog.dart';
- import '../../handler/event_handler.dart';
- import '../../handler/wechat_login_service.dart';
- import '../../resource/string.gen.dart';
- import '../../utils/error_handler.dart';
- import '../../utils/http_handler.dart';
- import '../../utils/toast_util.dart';
- @injectable
- class LoginController extends BaseController {
- final AccountRepository accountRepository;
- final WechatLoginService wechatLoginService;
- final RxString _phone = ''.obs;
- final RxString _code = ''.obs;
- String get phone => _phone.value;
- String get code => _code.value;
- final int _countDownTime = 60;
- final RxnInt _countDown = RxnInt();
- int? get countDown => _countDown.value;
- final RxBool _isAgree = false.obs;
- bool get isAgree => _isAgree.value;
- final RxBool _isFirstSend = true.obs;
- final RxBool isInstallWechat = false.obs;
- bool get isFirstSend => _isFirstSend.value;
- LoginController(this.accountRepository, this.wechatLoginService);
- @override
- void onInit() {
- super.onInit();
- checkIsInstallWechat();
- }
- void onPhoneChanged(String value) {
- _phone.value = value;
- }
- void onCodeChanged(String value) {
- _code.value = value;
- }
- void onBackClick() {
- Get.back();
- }
- void clickAgree() {
- _isAgree.value = !_isAgree.value;
- }
- void onSendVerificationCode() async {
- if (_countDown.value != null) {
- return;
- }
- if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
- ToastUtil.show(StringName.loginPrintPhoneVerification);
- return;
- }
- if (!_isAgree.value) {
- PrivacyAgreementDialog.show(
- btnConfirm: () async {
- _isAgree.value = true;
- onSendVerificationCode();
- },
- );
- return;
- }
- try {
- await accountRepository.loginSendCode(phone);
- _countDown.value = _countDownTime;
- _startCountDown();
- } catch (error) {
- if (error is RequestCodeTooOftenException) {
- ToastUtil.show(StringName.loginRequestCodeFrequentlyToast);
- } else if (error is ServerErrorException) {
- ToastUtil.show(error.message);
- } else {
- ToastUtil.show(StringName.loginVerificationCodeRequestFailedToast);
- }
- }
- }
- void onLoginClick() {
- if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
- ToastUtil.show(StringName.loginPrintPhoneVerification);
- return;
- }
- if (!_isAgree.value) {
- PrivacyAgreementDialog.show(
- btnConfirm: () async {
- _isAgree.value = true;
- onLoginClick();
- },
- );
- ToastUtil.show(StringName.loginAgreePrivacy);
- return;
- }
- if (code.isEmpty) {
- ToastUtil.show(StringName.loginPrintVerificationCode);
- return;
- }
- accountRepository
- .loginUserLogin(phone, code)
- .then((data) {
- EventHandler.report(EventId.event_04003);
- Get.back();
- ToastUtil.show(StringName.loginSuccess);
- })
- .catchError((error) {
- EventHandler.report(EventId.event_04004);
- if (error is LoginTooOftenException) {
- ToastUtil.show(StringName.loginTooOftenToast);
- return;
- }
- if (error is ServerErrorException) {
- if (error.code == ErrorCode.verificationCodeError) {
- ToastUtil.show(StringName.loginVerificationCodeErrorToast);
- } else {
- ToastUtil.show(error.message);
- }
- } else {
- ErrorHandler.toastError(error);
- }
- });
- }
- void clickBack() {
- EventHandler.report(EventId.event_04005);
- Get.back();
- }
- void _startCountDown() {
- Future.delayed(Duration(seconds: 1), () {
- int? time = _countDown.value;
- _isFirstSend.value = false;
- if (time != null) {
- _countDown.value = time - 1;
- if (time > 0) {
- _startCountDown();
- } else {
- _countDown.value = null;
- }
- }
- });
- }
- @override
- void onClose() {
- super.onClose();
- _countDown.value = null;
- }
- void checkIsInstallWechat() async {
- isInstallWechat.value = await WechatKitPlatform.instance.isInstalled();
- }
- void clickWxLogin() async {
- if (!_isAgree.value) {
- Get.back();
- LoginDialog.show();
- return;
- }
- }
- // 苹果登录点击
- void clickAppleLogin() async {
- if (!_isAgree.value) {
- PrivacyAgreementDialog.show(
- btnConfirm: () async {
- _isAgree.value = true;
- clickAppleLogin();
- },
- );
- ToastUtil.show(StringName.loginAgreePrivacy);
- return;
- }
- CustomLoadingDialog.show();
- AuthorizationCredentialAppleID credential;
- try {
- credential = await SignInWithApple.getAppleIDCredential(
- scopes: [
- AppleIDAuthorizationScopes.email,
- AppleIDAuthorizationScopes.fullName,
- ],
- );
- } catch (error) {
- CustomLoadingDialog.hide();
- ToastUtil.show(StringName.loginFailedToast);
- return;
- }
- var userIdentity = credential.userIdentifier ?? "";
- var authorizationCode = credential.authorizationCode ?? "";
- var identityToken = credential.identityToken ?? "";
- accountRepository
- .appleLogin(userIdentity, authorizationCode, identityToken)
- .then((data) {
- CustomLoadingDialog.hide();
- EventHandler.report(EventId.event_04003);
- Get.back();
- ToastUtil.show(StringName.loginSuccess);
- })
- .catchError((error) {
- CustomLoadingDialog.hide();
- EventHandler.report(EventId.event_04004);
- if (error is LoginTooOftenException) {
- ToastUtil.show(StringName.loginTooOftenToast);
- return;
- }
- if (error is ServerErrorException) {
- if (error.code == ErrorCode.verificationCodeError) {
- ToastUtil.show(StringName.loginVerificationCodeErrorToast);
- } else {
- ToastUtil.show(error.message);
- }
- } else {
- ErrorHandler.toastError(error);
- }
- });
- }
- }
|