235 lines
8.5 KiB
Dart
Executable File
235 lines
8.5 KiB
Dart
Executable File
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:star_lock/apm/apm_helper.dart';
|
|
import 'package:star_lock/appRouters.dart';
|
|
import 'package:star_lock/app_settings/app_settings.dart';
|
|
import 'package:star_lock/blue/blue_manage.dart';
|
|
import 'package:star_lock/blue/io_tool/manager_event_bus.dart';
|
|
import 'package:star_lock/common/XSConstantMacro/XSConstantMacro.dart';
|
|
import 'package:star_lock/login/login/entity/LoginEntity.dart';
|
|
import 'package:star_lock/mine/mine/starLockMine_state.dart';
|
|
import 'package:star_lock/mine/minePersonInfo/minePersonInfoPage/minePersonInfo_entity.dart';
|
|
import 'package:star_lock/network/start_chart_api.dart';
|
|
import 'package:star_lock/talk/starChart/entity/star_chart_register_node_entity.dart';
|
|
import 'package:star_lock/tools/appFirstEnterHandle.dart';
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
|
import 'package:star_lock/translations/current_locale_tool.dart';
|
|
|
|
import '../../main/lockMian/lockMain/lockMain_logic.dart';
|
|
import '../../mine/mine/starLockMine_logic.dart';
|
|
import '../../network/api_repository.dart';
|
|
import '../../tools/dateTool.dart';
|
|
import '../../tools/eventBusEventManage.dart';
|
|
import '../../tools/jverify_one_click_login.dart';
|
|
import '../../tools/showTipView.dart';
|
|
import '../../tools/storage.dart';
|
|
import '../register/entity/checkIP_entity.dart';
|
|
import 'starLock_login_state.dart';
|
|
|
|
class StarLockLoginLogic extends BaseGetXController {
|
|
final StarLockLoginState state = StarLockLoginState();
|
|
final StarLockMineState stateMyLogic = Get.put(StarLockMineLogic()).state;
|
|
int indexFocusNode = noneFocusNode;
|
|
static int noneFocusNode = 0;
|
|
static int emailOrPhoneFocusNode = 1;
|
|
static int pwdFocusNode = 2;
|
|
|
|
//检查焦点状态
|
|
void changeInputFocusNode() {
|
|
Future<void>.delayed(const Duration(milliseconds: 100), () {
|
|
if (indexFocusNode == noneFocusNode) {
|
|
return;
|
|
}
|
|
if (indexFocusNode == emailOrPhoneFocusNode) {
|
|
state.emailOrPhoneFocusNode.requestFocus();
|
|
} else if (indexFocusNode == pwdFocusNode) {
|
|
state.pwdFocusNode.requestFocus();
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> login() async {
|
|
FocusScope.of(Get.context!).requestFocus(FocusNode()); //关闭键盘
|
|
final LoginEntity entity = await ApiRepository.to.login(
|
|
loginType: '1',
|
|
password: state.pwd.value,
|
|
countryCode: state.countryCode.value,
|
|
username: state.emailOrPhone.value,
|
|
deviceInfo: state.deviceInfoMap);
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
ApmHelper.instance.trackEvent('login_result', {
|
|
'account': state.emailOrPhone.value,
|
|
'date': DateTool().getNowDateWithType(1),
|
|
'login_res': '成功',
|
|
});
|
|
|
|
//如已有星图配置则保存 无需重复注册节点及重复绑定
|
|
if (entity.data!.starchart != null) {
|
|
final Starchart starChart = entity.data!.starchart!;
|
|
// 获取星图url
|
|
if (starChart != null &&
|
|
starChart.scdUrl != null &&
|
|
starChart.scdUrl != '') {
|
|
StartChartApi.to.startChartHost =
|
|
starChart!.scdUrl ?? StartChartApi.to.startChartHost;
|
|
}
|
|
final StarChartRegisterNodeEntity starChartRegisterNodeEntity =
|
|
StarChartRegisterNodeEntity(
|
|
peer: PeerData(
|
|
id: starChart.starchartId,
|
|
publicKey: starChart.starchartPeerPublicKey,
|
|
privateKey: starChart.starchartPeerPrivateKey,
|
|
),
|
|
);
|
|
Storage.saveStarChartRegisterNodeInfo(starChartRegisterNodeEntity);
|
|
entity.data!.updateStarchart(starChartRegisterNodeEntity);
|
|
}
|
|
await Storage.saveLoginData(entity.data);
|
|
Storage.setBool(saveIsVip, entity.data!.isVip == 1);
|
|
eventBus.fire(MineInfoChangeRefreshUI());
|
|
if (Get.isRegistered<LockMainLogic>()) {
|
|
Get.find<LockMainLogic>().getStarLockInfo(isUnShowLoading: true);
|
|
}
|
|
Get.offNamedUntil(Routers.starLockMain, (Route route) => false);
|
|
BlueManage().scanDevices.clear(); //清除设备缓存
|
|
} else {
|
|
ApmHelper.instance.trackEvent('login_result', {
|
|
'account': state.emailOrPhone.value,
|
|
'date': DateTool().getNowDateWithType(1),
|
|
'login_res': '${entity.errorCode}--${entity.errorMsg}',
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> oneClickLoginAction() async {
|
|
await JverifyOneClickLoginManage().loginAuth((e) async {
|
|
final int? code = e.code;
|
|
final String? content = e.message;
|
|
// final String operator = map['operator'];
|
|
AppLog.log('1111code:$code content:$content');
|
|
switch (code) {
|
|
case 6000:
|
|
final LoginEntity entity = await ApiRepository.to.oneClickLogin(
|
|
loginType: '3',
|
|
loginToken: content ?? '',
|
|
deviceInfo: state.deviceInfoMap);
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
ApmHelper.instance.trackEvent('login_result', {
|
|
'account': state.emailOrPhone.value,
|
|
'date': DateTool().getNowDateWithType(1),
|
|
'login_res': '成功',
|
|
});
|
|
|
|
Storage.saveLoginData(entity.data);
|
|
Storage.setBool(saveIsVip, entity.data!.isVip == 1);
|
|
eventBus.fire(MineInfoChangeRefreshUI());
|
|
if (Get.isRegistered<LockMainLogic>()) {
|
|
Get.find<LockMainLogic>().getStarLockInfo(isUnShowLoading: true);
|
|
}
|
|
Get.offNamedUntil(Routers.starLockMain, (Route route) => false);
|
|
BlueManage().scanDevices.clear(); //清除设备缓存
|
|
} else {
|
|
ApmHelper.instance.trackEvent('login_result', {
|
|
'account': state.emailOrPhone.value,
|
|
'date': DateTool().getNowDateWithType(1),
|
|
'login_res': '${entity.errorCode}--${entity.errorMsg}',
|
|
});
|
|
}
|
|
break;
|
|
case 6002:
|
|
showToast('取消一键登录'.tr);
|
|
break;
|
|
default:
|
|
showToast('一键登录失败,请重试'.tr);
|
|
break;
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> checkIpAction() async {
|
|
final CheckIPEntity entity = await ApiRepository.to.checkIpAction(ip: '');
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
if (state.countryName != entity.data!.name) {
|
|
ShowTipView().showSureAlertDialog(
|
|
'国家地区的选择将影响数据安全,你当前选择的是' + state.countryName + '请确认后再继续'.tr,
|
|
tipTitle: '确认国家或地区'.tr,
|
|
sureStr: '我知道了'.tr);
|
|
}
|
|
}
|
|
}
|
|
|
|
void changeAgreeState() {
|
|
_resetCanNext();
|
|
}
|
|
|
|
void exchangeFistShow() =>
|
|
state.passwordShow.value = !state.passwordShow.value;
|
|
|
|
void checkNext(TextEditingController controller) {
|
|
changeInput(controller);
|
|
}
|
|
|
|
void changeInput(TextEditingController controller) {
|
|
if (controller == state.emailOrPhoneController) {
|
|
state.emailOrPhone.value = controller.text;
|
|
}
|
|
if (controller == state.pwdController) {
|
|
state.pwd.value = controller.text;
|
|
}
|
|
_resetCanNext();
|
|
}
|
|
|
|
void _resetCanNext() {
|
|
state.canNext.value = state.pwdIsOK && state.isEmailOrPhone;
|
|
}
|
|
|
|
late StreamSubscription _agreePrivacySubscription;
|
|
|
|
void _initEventListen() {
|
|
_agreePrivacySubscription = eventBus
|
|
.on<AgreePrivacyAgreement>()
|
|
.listen((AgreePrivacyAgreement event) async {
|
|
/// 检查ip如果属于国内才进行初始化
|
|
final CheckIPEntity entity = await ApiRepository.to.checkIpAction(ip: '');
|
|
String currentLanguage =
|
|
CurrentLocaleTool.getCurrentLocaleString(); // 当前选择语言
|
|
// 判断如果ip是国内的且选的是中文才初始化一键登录
|
|
if (entity.data!.abbreviation?.toLowerCase() == 'cn' &&
|
|
currentLanguage == 'zh_CN') {
|
|
// 初始化一键登录服务
|
|
await JverifyOneClickLoginManage();
|
|
state.isCheckVerifyEnable.value =
|
|
await JverifyOneClickLoginManage().checkVerifyEnable();
|
|
AppLog.log('一键登录初始化认证结果:${state.isCheckVerifyEnable.value}');
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
Future<void> onInit() async {
|
|
_initEventListen();
|
|
super.onInit();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// 取消事件监听
|
|
_agreePrivacySubscription.cancel();
|
|
state.onClose();
|
|
super.onClose();
|
|
}
|
|
|
|
//刷新设备信息
|
|
Future<void> flushedDeviceInfo() async {
|
|
eventBus.fire(AgreePrivacyAgreement());
|
|
XSConstantMacro().getDeviceInfoData().then((Map<String, dynamic> data) {
|
|
state.deviceInfoMap.value = data;
|
|
}).catchError((dynamic error) {
|
|
// 适当处理错误
|
|
AppLog.log('获取设备信息时出错: $error');
|
|
});
|
|
}
|
|
}
|