account_logoff_cubit.dart 4.56 KB
import 'dart:async';

import 'package:appframe/config/locator.dart';
import 'package:appframe/config/routes.dart';
import 'package:appframe/data/repositories/phone_auth_repository.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AccountLogoffState extends Equatable {
  final String phone;
  final bool showSnackBar;
  final String snackBarMsg;
  final bool allowSend;
  final int seconds;
  final bool isLoading;

  const AccountLogoffState({
    this.phone = '',
    this.showSnackBar = false,
    this.snackBarMsg = '',
    this.allowSend = true,
    this.seconds = 0,
    this.isLoading = false,
  });

  AccountLogoffState copyWith({
    String? phone,
    bool? showSnackBar,
    String? snackBarMsg,
    bool? allowSend,
    int? seconds,
    bool? isLoading,
  }) {
    return AccountLogoffState(
      phone: phone ?? this.phone,
      showSnackBar: showSnackBar ?? this.showSnackBar,
      snackBarMsg: snackBarMsg ?? this.snackBarMsg,
      allowSend: allowSend ?? this.allowSend,
      seconds: seconds ?? this.seconds,
      isLoading: isLoading ?? this.isLoading,
    );
  }

  @override
  List<Object?> get props => [
        phone,
        showSnackBar,
        snackBarMsg,
        allowSend,
        seconds,
        isLoading,
      ];
}

class AccountLogoffCubit extends Cubit<AccountLogoffState> {
  late TextEditingController _codeController;
  Timer? _timer;
  int countdown = 60;

  late final PhoneAuthRepository _phoneAuthRepository;

  TextEditingController get codeController => _codeController;

  AccountLogoffCubit(super.initialState) {
    _codeController = TextEditingController();

    _codeController.text = '';

    _phoneAuthRepository = getIt.get<PhoneAuthRepository>();
  }

  /// 开始倒计时
  void startCountdown() {
    countdown = 60;
    emit(state.copyWith(allowSend: false, seconds: countdown));

    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      countdown--;
      if (countdown <= 0) {
        _timer?.cancel();
        emit(state.copyWith(allowSend: true, seconds: 60));
      } else {
        emit(state.copyWith(seconds: countdown));
      }
    });
  }

  /// 发送验证码
  Future<void> sendVerificationCode() async {
    if (state.allowSend) {
      if (!RegExp(r'^1[3-9][0-9]{9}$').hasMatch(state.phone)) {
        emit(state.copyWith(showSnackBar: true, snackBarMsg: '手机号码信息错误'));
        emit(state.copyWith(showSnackBar: false));
        return;
      }

      var result = await _phoneAuthRepository.verifyCode(state.phone, 1);
      if (result['code'] != 0) {
        emit(state.copyWith(showSnackBar: true, snackBarMsg: result['error']));
        emit(state.copyWith(showSnackBar: false));
        return;
      }

      emit(state.copyWith(allowSend: false, seconds: 60));

      startCountdown();
    }
  }

  /// 注销账户
  Future<void> logoff() async {
    // String verifyCode = _codeController.text;

    if (!RegExp(r'^1[3-9][0-9]{9}$').hasMatch(state.phone)) {
      emit(state.copyWith(showSnackBar: true, snackBarMsg: '手机号码信息错误'));
      emit(state.copyWith(showSnackBar: false));
      return;
    }

    // if (!RegExp(r'^\d{4}$').hasMatch(verifyCode)) {
    //   emit(state.copyWith(showSnackBar: true, snackBarMsg: '请输入正确的验证码'));
    //   emit(state.copyWith(showSnackBar: false));
    //   return;
    // }

    emit(state.copyWith(isLoading: true));

    // var sharedPreferences = getIt.get<SharedPreferences>();
    // var userCode = sharedPreferences.getString('auth_userCode') ?? '';
    // var result = await _phoneAuthRepository.unbindWithVerifyCode(userCode, verifyCode);
    var result = await _phoneAuthRepository.unbindPhone(state.phone);

    emit(state.copyWith(isLoading: false));

    if (result['code'] != 0) {
      emit(state.copyWith(showSnackBar: true, snackBarMsg: result['error']));
      emit(state.copyWith(showSnackBar: false));
      return;
    }

    emit(state.copyWith(showSnackBar: true, snackBarMsg: '操作成功'));
    emit(state.copyWith(showSnackBar: false));

    await Future.delayed(Duration(seconds: 1));

    var sharedPreferences = getIt.get<SharedPreferences>();
    sharedPreferences.getKeys().forEach((key) {
      if (key.startsWith('auth_')) {
        sharedPreferences.remove(key);
      }
    });
    router.go('/loginMain');
  }

  @override
  Future<void> close() async {
    _timer?.cancel();

    try {
      _codeController.dispose();
    } catch (e) {
      print(e);
    }

    await super.close();
  }
}