import 'package:flutter/cupertino.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; import 'package:star_lock/appRouters.dart'; import 'package:star_lock/app_settings/app_colors.dart'; import 'package:star_lock/main/lockDetail/messageWarn/addFamily/addFamily_state.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(); //添加开门通知 void addLockNoticeSetting() async { var entity = await ApiRepository.to.addLockNoticeSetting( lockId: state.getLockId.value, noticeType: 10, settingValue: { 'openDoorId': state.lockUserKeys.value.currentOpenDoorID, 'openDoorType': state.lockUserKeys.value.currentKeyType, 'remark': state.lockUserKeys.value.currentKeyName ?? '', 'noticeWay': [ {'type': 'mail', 'accounts': getEmailAndSMSAccountList(true)}, {'type': 'sms', 'accounts': getEmailAndSMSAccountList(false)} ] }, ); if (entity.errorCode!.codeIsSuccessful) { showToast('添加成功'.tr); Get.back(result: true); } } //更新开门通知 void updateLockNoticeSetting() async { var entity = await ApiRepository.to.updateLockNoticeSettingAccount( lockNoticeSettingAccountId: state.familyData.value.id!, settingValue: { 'openDoorId': state.familyData.value.settingValue!.openDoorId!, 'openDoorType': state.familyData.value.settingValue!.openDoorType!, 'remark': state.changeNameController.text, 'noticeWay': state.familyData.value.settingValue!.noticeWayList, }, ); if (entity.errorCode!.codeIsSuccessful) { showToast('更新成功'.tr); state.lockUserKeys.value.currentKeyName = state.changeNameController.text; Get.back(result: true); } } //删除开门通知 void deleteLockNoticeSetting() async { var entity = await ApiRepository.to.deleteLockNoticeSettingAccount( lockNoticeSettingAccountId: state.familyData.value.id!, ); if (entity.errorCode!.codeIsSuccessful) { showToast('删除成功'.tr); Get.back(result: true); } } //获取到家人信息的请求数组 List getEmailAndSMSAccountList(bool isEmail) { List list = []; List accountList = []; isEmail ? accountList = state.emailReceiverList.value : accountList = state.phoneReceiverList.value; for (int i = 0; i < accountList.length; i++) { MsgNoticeModeData item = accountList[i]; 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']; List emailReceiverList = state.emailReceiverList.value; for (int i = 0; i < emailReceiverList.length; i++) { 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']; List phoneReceiverList = state.phoneReceiverList.value; for (int i = 0; i < phoneReceiverList.length; i++) { MsgNoticeModeData item = phoneReceiverList[i]; phoneListStr += item.receivePhone; // 检查是否为最后一个元素 if (i < phoneReceiverList.length - 1) { phoneListStr += ','; } } } return phoneListStr; } //检查按钮是否可用 bool checkBtnDisable() { if ((state.emailListStr.value.isEmpty || state.phontListStr.value.isEmpty) || state.lockUserKeys.value.currentKeyTypeStr!.isEmpty || state.lockUserKeys.value.currentKeyName!.isEmpty) { return false; } else { return true; } } //当前钥匙类型 1:电子钥匙 2:密码钥匙 3:指纹钥匙 4:卡钥匙 5:人脸钥匙 String getKeyTypeStr() { int keyType = state.familyData.value.settingValue!.openDoorType!; switch (keyType) { case 1: return '电子钥匙'; case 2: return '密码'; case 3: return '指纹'; case 4: return '卡'; case 5: return '人脸'; default: return ''; } } //根据列表返回值得到邮箱、手机列表 Map> getAccountsMap() { List mailAccounts = []; List smsAccounts = []; if (state.familyData.value.settingValue != null) { for (NoticeWay item in state.familyData.value.settingValue!.noticeWayList!) { if (item.type == 'mail' && item.accounts != null) { for (Accounts account in item.accounts!) { if (account.account != null) { MsgNoticeModeData msgNoticeModeData = MsgNoticeModeData(); msgNoticeModeData.receiveEmail = account.account!; mailAccounts.add(msgNoticeModeData); } } } else if (item.type == 'sms' && item.accounts != null) { for (Accounts account in item.accounts!) { if (account.account != null && account.countryCode != null) { 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()); } }