import 'package:get/get.dart'; import 'package:star_lock/main/lockDetail/messageWarn/addFamily/addFamily_state.dart'; import 'package:star_lock/main/lockDetail/messageWarn/msgNotification/msgNotification/msgNotification_entity.dart'; import 'package:star_lock/main/lockDetail/messageWarn/msgNotification/openDoorNotify/openDoorNotify_entity.dart'; import 'package:star_lock/main/lockDetail/messageWarn/notificationMode/notificationMode_data.dart'; import 'package:star_lock/network/api_repository.dart'; import 'package:star_lock/tools/baseGetXController.dart'; class AddFamilyLogic extends BaseGetXController { final AddFamilyState state = AddFamilyState(); //添加开门通知 Future addLockNoticeSetting() async { final Map settingValue = { 'openDoorId': state.lockUserKeys.value.currentOpenDoorID, 'openDoorType': state.lockUserKeys.value.currentKeyType, 'remark': state.lockUserKeys.value.currentKeyName ?? '', 'noticeWay': getNoticeWayList(), }; final MsgNotificationEntity entity = await ApiRepository.to.addLockNoticeSetting( lockId: state.getLockId.value, noticeType: 10, settingValue: settingValue, ); if (entity.errorCode!.codeIsSuccessful) { showToast('添加成功'.tr); Get.back(result: true); } } List> getNoticeWayList() { return >[ { 'type': 'mail', 'accounts': getEmailAndSMSAccountList(true) }, { 'type': 'sms', 'accounts': getEmailAndSMSAccountList(false) }, ]; } //更新开门通知 Future updateLockNoticeSetting() async { final OpenDoorNotifyEntity entity = await ApiRepository.to.updateLockNoticeSettingAccount( lockNoticeSettingAccountId: state.familyData.value.id!, settingValue: { 'openDoorId': state.isDetail.value ? state.lockUserKeys.value.currentOpenDoorID : state.familyData.value.settingValue!.openDoorId!, 'openDoorType': state.isDetail.value ? state.lockUserKeys.value.currentKeyType : state.familyData.value.settingValue!.openDoorType!, 'remark': state.isDetail.value ? state.lockUserKeys.value.currentKeyName : state.changeNameController.text, 'noticeWay': state.familyData.value.settingValue!.noticeWayList, }, ); if (entity.errorCode!.codeIsSuccessful) { showToast('更新成功'.tr); if (!state.isDetail.value) { state.lockUserKeys.value.currentKeyName = state.changeNameController.text; state.lockUserKeys.refresh(); Get.back(result: true); } else { state.lockUserKeys.refresh(); } } } //删除开门通知 Future deleteLockNoticeSetting() async { final OpenDoorNotifyEntity entity = await ApiRepository.to.deleteLockNoticeSettingAccount( lockNoticeSettingAccountId: state.familyData.value.id!, ); if (entity.errorCode!.codeIsSuccessful) { showToast('删除成功'.tr); Get.back(result: true); } } //获取到家人信息的请求数组 List getEmailAndSMSAccountList(bool isEmail) { final List list = []; List accountList = []; isEmail ? accountList = state.emailReceiverList.value : accountList = state.phoneReceiverList.value; for (int i = 0; i < accountList.length; i++) { final MsgNoticeModeData item = accountList[i]; final Map map = {}; map['countryCode'] = isEmail ? 0 : item.countryCode; map['account'] = isEmail ? item.receiveEmail : item.receivePhone; list.add(map); } return list; } String getEmailListStr(Map val) { String emailListStr = ''; if (val['emailReceiverList'] != null) { state.emailReceiverList.value = val['emailReceiverList']; final List emailReceiverList = state.emailReceiverList.value; for (int i = 0; i < emailReceiverList.length; i++) { final MsgNoticeModeData item = emailReceiverList[i]; emailListStr += item.receiveEmail; // 检查是否为最后一个元素 if (i < emailReceiverList.length - 1) { emailListStr += ','; } } } return emailListStr; } String getPhoneListStr(Map val) { String phoneListStr = ''; if (val['phoneReceiverList'] != null) { state.phoneReceiverList.value = val['phoneReceiverList']; final List phoneReceiverList = state.phoneReceiverList.value; for (int i = 0; i < phoneReceiverList.length; i++) { final MsgNoticeModeData item = phoneReceiverList[i]; phoneListStr += item.receivePhone; // 检查是否为最后一个元素 if (i < phoneReceiverList.length - 1) { phoneListStr += ','; } } } return phoneListStr; } //检查按钮是否可用 bool checkBtnDisable() { final String? keyTypeStr = state.lockUserKeys.value.currentKeyTypeStr; final String? keyName = state.lockUserKeys.value.currentKeyName; if (keyTypeStr == null || keyName == null) { return false; } return keyTypeStr.isNotEmpty && keyName.isNotEmpty; } //当前钥匙类型 1:电子钥匙 2:密码钥匙 3:指纹钥匙 4:卡钥匙 5:人脸钥匙 String getKeyTypeStr() { final int keyType = state.familyData.value.settingValue!.openDoorType!; switch (keyType) { case 1: return '电子钥匙'.tr; case 2: return '密码'.tr; case 3: return '指纹'.tr; case 4: return '卡'.tr; case 5: return '人脸'.tr; default: return ''; } } //根据列表返回值得到邮箱、手机列表 Map> getAccountsMap() { final List mailAccounts = []; final List smsAccounts = []; state.familyData.value.settingValue?.noticeWayList ?.forEach((NoticeWay item) { if (item.type == 'mail') { item.accounts?.forEach((Accounts account) { if (account.account != null) { final MsgNoticeModeData msgNoticeModeData = MsgNoticeModeData(); msgNoticeModeData.receiveEmail = account.account!; mailAccounts.add(msgNoticeModeData); } }); } else if (item.type == 'sms') { item.accounts?.forEach((Accounts account) { if (account.account != null && account.countryCode != null) { final MsgNoticeModeData msgNoticeModeData = MsgNoticeModeData(); msgNoticeModeData.receivePhone = account.account!; msgNoticeModeData.countryCode = account.countryCode!; smsAccounts.add(msgNoticeModeData); } }); } }); return >{ 'emailReceiverList': mailAccounts, 'phoneReceiverList': smsAccounts, }; } @override onInit() { super.onInit(); state.emailListStr.value = getEmailListStr(getAccountsMap()); state.phontListStr.value = getPhoneListStr(getAccountsMap()); } }