app-starlock/lib/mine/minePersonInfo/minePersonInfoEmail/mineBindPhoneOrEmail_logic.dart

106 lines
3.5 KiB
Dart
Executable File

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:star_lock/appRouters.dart';
import 'package:star_lock/login/login/entity/LoginData.dart';
import 'package:star_lock/login/register/entity/SendValidationCodeEntity.dart';
import 'package:star_lock/main/lockDetail/passwordKey/passwordKeyList/passwordKeyListEntity.dart';
import 'package:star_lock/mine/minePersonInfo/minePersonInfoEmail/mineBindPhoneOrEmail_state.dart';
import 'package:star_lock/network/api_repository.dart';
import 'package:star_lock/tools/baseGetXController.dart';
import '../../../tools/eventBusEventManage.dart';
import '../../../tools/storage.dart';
class MineBindPhoneOrEmailLogic extends BaseGetXController {
final MineBindPhoneOrEmailState state = MineBindPhoneOrEmailState();
late Timer _timer;
void _startTimer() {
_timer = Timer.periodic(1.seconds, (Timer timer) {
if (state.currentSecond > 1) {
state.currentSecond--;
} else {
_cancelTimer();
state.currentSecond = state.totalSeconds;
}
state.resetResend();
});
}
void _cancelTimer() {
_timer.cancel();
}
//获取验证码请求
Future<void> sendValidationCode() async {
final SendValidationCodeEntity entity = await ApiRepository.to.sendValidationCodeAuth(
countryCode: state.channel.value == '1' ? state.countryCode.value : '',
account: state.inputAccount.value,
channel: state.channel.value,
codeType: state.codeType.value,
xWidth: state.xWidth.value.toString());
if (entity.errorCode!.codeIsSuccessful) {
_startTimer();
}
}
//绑定邮箱请求
Future<void> bindEmailRequest() async {
final PasswordKeyListEntity entity = await ApiRepository.to.bindEmail(
state.inputAccount.value,
state.verificationCode.value,
state.unbindToken.value);
if (entity.errorCode!.codeIsSuccessful) {
showToast('邮箱绑定成功'.tr, something: () async {
final LoginData? loginData = await Storage.getLoginData();
loginData!.email = state.inputAccount.value;
Storage.saveLoginData(loginData);
eventBus.fire(MineInfoChangeRefreshUI());
Get.until((Route route) => route.settings.name == Routers.minePersonInfoPage);
});
}
}
//绑定手机请求
Future<void> bindMobileRequest() async {
final PasswordKeyListEntity entity = await ApiRepository.to.bindPhone(
state.countryCode.value,
state.inputAccount.value,
state.verificationCode.value,
state.unbindToken.value);
if (entity.errorCode!.codeIsSuccessful) {
showToast('手机绑定成功'.tr, something: () async {
final LoginData? loginData = await Storage.getLoginData();
loginData!.mobile = state.inputAccount.value;
Storage.saveLoginData(loginData);
eventBus.fire(MineInfoChangeRefreshUI());
Get.until((Route route) => route.settings.name == Routers.minePersonInfoPage);
});
}
}
void checkNext(TextEditingController controller) {
changeInput(controller);
}
void changeInput(TextEditingController controller) {
if (controller == state.accountController) {
state.inputAccount.value = controller.text;
state.accountIsOK.value = state.inputAccount.value.isNotEmpty;
}
if (controller == state.codeController) {
state.verificationCode.value = controller.text;
}
_resetCanSub();
}
void _resetCanSub() {
state.canSub.value = state.codeIsOK;
}
}