2024-06-13 09:12:47 +08:00
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
2024-06-12 13:53:59 +08:00
|
|
|
|
import 'package:get/get.dart';
|
2024-06-11 17:55:00 +08:00
|
|
|
|
import 'package:star_lock/main/lockDetail/electronicKey/sendEmailNotification/sendEmailNotification_entity.dart';
|
|
|
|
|
|
import 'package:star_lock/main/lockDetail/electronicKey/sendEmailNotification/sendEmailNotification_state.dart';
|
|
|
|
|
|
import 'package:star_lock/network/api_repository.dart';
|
|
|
|
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
|
|
|
|
|
import 'package:star_lock/tools/commonDataManage.dart';
|
2024-06-13 09:12:47 +08:00
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2024-06-11 17:55:00 +08:00
|
|
|
|
|
|
|
|
|
|
class SendEmailNotificationLogic extends BaseGetXController {
|
|
|
|
|
|
final SendEmailNotificationState state = SendEmailNotificationState();
|
|
|
|
|
|
|
2025-02-28 14:52:04 +08:00
|
|
|
|
onInit() async {
|
|
|
|
|
|
super.onInit();
|
|
|
|
|
|
getKeyNoticeTemplate();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-11 17:55:00 +08:00
|
|
|
|
//获取电子钥匙通知模板 渠道:1短信 2邮箱
|
2025-02-28 14:52:04 +08:00
|
|
|
|
void getKeyNoticeTemplate() async {
|
2024-07-08 18:18:20 +08:00
|
|
|
|
final SendEmailNotificationEntity entity;
|
|
|
|
|
|
if (state.unlockType.value == 1) {
|
|
|
|
|
|
entity = await ApiRepository.to.getKeyNoticeTemplate(
|
|
|
|
|
|
lockId: CommonDataManage().currentKeyInfo.lockId ?? 0,
|
|
|
|
|
|
keyId: state.getKeyId.value,
|
|
|
|
|
|
channelType: state.channelType.value);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
entity = await ApiRepository.to.getPwdNoticeTemplate(
|
|
|
|
|
|
lockId: CommonDataManage().currentKeyInfo.lockId ?? 0,
|
|
|
|
|
|
keyboardPwdId: state.getKeyId.value,
|
|
|
|
|
|
channelType: state.channelType.value);
|
|
|
|
|
|
}
|
2024-06-11 17:55:00 +08:00
|
|
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
|
|
|
|
state.emailTemplateList.value =
|
|
|
|
|
|
entity.data?.list ?? <EmailNotificationItem>[];
|
2024-06-12 13:53:59 +08:00
|
|
|
|
state.currentNotifyItem.value = state.emailTemplateList.first;
|
2024-06-12 16:49:39 +08:00
|
|
|
|
state.templateContentController.text =
|
|
|
|
|
|
state.currentNotifyItem.value.template ?? '';
|
2024-06-11 17:55:00 +08:00
|
|
|
|
state.emailTemplateList.refresh();
|
2024-06-12 13:53:59 +08:00
|
|
|
|
state.currentNotifyItem.refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-12 16:49:39 +08:00
|
|
|
|
//发送短信、邮件通知 channelType--1短信 2邮件 opisAPKFileName
|
2024-06-12 13:53:59 +08:00
|
|
|
|
Future<void> keyNoticeSubmitRequest() async {
|
2024-06-12 16:49:39 +08:00
|
|
|
|
if (state.receiverController.text.isEmpty &&
|
|
|
|
|
|
state.getReceiver.value.isEmpty) {
|
2024-08-21 18:31:19 +08:00
|
|
|
|
showToast('请输入接收者'.tr);
|
2024-06-12 16:49:39 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-07-08 18:18:20 +08:00
|
|
|
|
final SendEmailNotificationEntity entity;
|
|
|
|
|
|
if (state.unlockType.value == 1) {
|
|
|
|
|
|
entity = await ApiRepository.to.keyNoticeSubmit(
|
|
|
|
|
|
receiverName: state.getReceiver.value.isEmpty
|
|
|
|
|
|
? state.receiverController.text
|
|
|
|
|
|
: state.getReceiver.value,
|
|
|
|
|
|
lockId: CommonDataManage().currentKeyInfo.lockId ?? 0,
|
|
|
|
|
|
keyId: state.getKeyId.value,
|
|
|
|
|
|
channelType: state.channelType.value,
|
|
|
|
|
|
openDoorType: state.unlockType.value,
|
|
|
|
|
|
templateType: state.currentNotifyItem.value.type ?? '',
|
|
|
|
|
|
countryCode: state.countryCode.value,
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
entity = await ApiRepository.to.pwdNoticeSubmit(
|
|
|
|
|
|
receiverName: state.getReceiver.value.isEmpty
|
|
|
|
|
|
? state.receiverController.text
|
|
|
|
|
|
: state.getReceiver.value,
|
|
|
|
|
|
lockId: CommonDataManage().currentKeyInfo.lockId ?? 0,
|
|
|
|
|
|
keyboardPwdId: state.getKeyId.value,
|
|
|
|
|
|
channelType: state.channelType.value,
|
|
|
|
|
|
openDoorType: state.unlockType.value,
|
|
|
|
|
|
templateType: state.currentNotifyItem.value.type ?? '',
|
|
|
|
|
|
countryCode: state.countryCode.value,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2024-06-12 13:53:59 +08:00
|
|
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
2024-08-21 18:31:19 +08:00
|
|
|
|
showToast('发送成功'.tr);
|
2024-06-12 13:53:59 +08:00
|
|
|
|
Get.back();
|
2024-06-11 17:55:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-06-13 09:12:47 +08:00
|
|
|
|
|
2024-06-13 17:35:14 +08:00
|
|
|
|
//发送邮件、短信
|
2024-06-13 09:12:47 +08:00
|
|
|
|
Future<void> sendPersonalSMSOrEmail() async {
|
2024-06-13 17:35:14 +08:00
|
|
|
|
if (state.receiverController.text.isEmpty &&
|
|
|
|
|
|
state.getReceiver.value.isEmpty) {
|
2024-08-21 18:31:19 +08:00
|
|
|
|
showToast('请输入接收者账号'.tr);
|
2024-06-13 17:35:14 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-13 09:12:47 +08:00
|
|
|
|
if (state.channelType.value == 1) {
|
2024-06-13 17:35:14 +08:00
|
|
|
|
Uri smsUri;
|
2024-06-13 09:12:47 +08:00
|
|
|
|
//短信
|
|
|
|
|
|
final String phoneNumber = state.getReceiver.value.isEmpty
|
|
|
|
|
|
? state.receiverController.text
|
|
|
|
|
|
: state.getReceiver.value;
|
|
|
|
|
|
final String message = state.templateContentController.text;
|
2024-06-13 17:35:14 +08:00
|
|
|
|
|
|
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
|
|
smsUri = Uri(
|
|
|
|
|
|
scheme: 'sms',
|
|
|
|
|
|
path: phoneNumber,
|
|
|
|
|
|
query: 'body=${Uri.encodeComponent(message)}',
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
smsUri =
|
|
|
|
|
|
Uri.parse('sms:$phoneNumber&body=${Uri.encodeComponent(message)}');
|
|
|
|
|
|
}
|
2024-06-13 09:12:47 +08:00
|
|
|
|
|
|
|
|
|
|
if (await canLaunchUrl(smsUri)) {
|
|
|
|
|
|
await launchUrl(smsUri);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw 'Could not launch $smsUri';
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (state.channelType.value == 2) {
|
|
|
|
|
|
//邮箱
|
|
|
|
|
|
final Uri emailUri = Uri(
|
|
|
|
|
|
scheme: 'mailto',
|
|
|
|
|
|
path: state.getReceiver.value.isEmpty
|
|
|
|
|
|
? state.receiverController.text
|
|
|
|
|
|
: state.getReceiver.value,
|
|
|
|
|
|
queryParameters: <String, String>{
|
|
|
|
|
|
'subject': state.currentNotifyItem.value.name ?? '',
|
|
|
|
|
|
'body': state.templateContentController.text,
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (await canLaunchUrl(emailUri)) {
|
|
|
|
|
|
await launchUrl(emailUri);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw 'Could not launch $emailUri';
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-06-11 17:55:00 +08:00
|
|
|
|
}
|