2024-08-21 14:12:15 +08:00
|
|
|
|
|
2024-06-04 14:44:04 +08:00
|
|
|
|
import 'dart:async';
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
|
|
import 'package:star_lock/login/login/entity/LoginEntity.dart';
|
|
|
|
|
|
import 'package:star_lock/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesAddSMSTemplate/newSMSTemplate_entity.dart';
|
|
|
|
|
|
import 'package:star_lock/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesAddSMSTemplate/newSMSTemplate_state.dart';
|
|
|
|
|
|
import 'package:star_lock/tools/regularExpression.dart';
|
|
|
|
|
|
|
|
|
|
|
|
import '../../../../network/api_repository.dart';
|
|
|
|
|
|
import '../../../../tools/baseGetXController.dart';
|
|
|
|
|
|
|
|
|
|
|
|
class NewSMSTemplateLogic extends BaseGetXController {
|
|
|
|
|
|
NewSMSTemplateState state = NewSMSTemplateState();
|
|
|
|
|
|
|
|
|
|
|
|
//获取默认模板-- 1:电子钥匙 2:密码
|
|
|
|
|
|
Future<void> getDefaultTemplate() async {
|
2024-07-09 16:04:56 +08:00
|
|
|
|
final NewSMSTemplateEntity entity =
|
|
|
|
|
|
await ApiRepository.to.getDefaultTemplate();
|
2024-06-04 14:44:04 +08:00
|
|
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
2024-07-09 16:04:56 +08:00
|
|
|
|
state.langTemplateList.value = entity.dataList ?? <LangData>[];
|
|
|
|
|
|
|
|
|
|
|
|
state.templateList.value =
|
|
|
|
|
|
entity.dataList![state.selectedLangIndex.value].templates ??
|
|
|
|
|
|
<TemplateData>[];
|
2024-06-04 14:44:04 +08:00
|
|
|
|
if (state.templateList.isNotEmpty) {
|
|
|
|
|
|
state.currentTemplate.value = state.templateList.firstWhere(
|
2024-07-09 16:04:56 +08:00
|
|
|
|
(TemplateData element) =>
|
|
|
|
|
|
element.name ==
|
|
|
|
|
|
state.templateList[state.selectedTemplateIndex.value].name,
|
2024-06-04 14:44:04 +08:00
|
|
|
|
);
|
|
|
|
|
|
state.currentTemplate.refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-05 18:24:59 +08:00
|
|
|
|
//新建模板-- 1:电子钥匙 2:密码
|
2024-06-04 14:44:04 +08:00
|
|
|
|
Future<void> addSMSTemplate() async {
|
|
|
|
|
|
if (state.templateNameTf.text.isEmpty) {
|
|
|
|
|
|
showToast('请输入模板名称');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-06-05 18:24:59 +08:00
|
|
|
|
final LoginEntity entity = await ApiRepository.to.addTemplateService(
|
2024-07-09 16:04:56 +08:00
|
|
|
|
channelType: state.channelType.value,
|
|
|
|
|
|
name: state.templateNameTf.text,
|
|
|
|
|
|
langType: state.currentTemplate.value.langType ?? 0,
|
|
|
|
|
|
regards: state.templateOneTf.text,
|
|
|
|
|
|
tips: state.templateTwoTf.text,
|
|
|
|
|
|
templateType: state.currentTemplate.value.templateType ?? 0,
|
|
|
|
|
|
);
|
2024-06-04 14:44:04 +08:00
|
|
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
2024-08-21 14:12:15 +08:00
|
|
|
|
showToast('添加成功'.tr);
|
2024-06-04 18:09:38 +08:00
|
|
|
|
Get.back(result: true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> updateTemplateInfo() async {
|
|
|
|
|
|
final LoginEntity entity = await ApiRepository.to.updateTemplateInfo(
|
|
|
|
|
|
id: state.currentTemplate.value.id ?? 0,
|
|
|
|
|
|
name: state.templateNameTf.text,
|
|
|
|
|
|
regards: state.templateOneTf.text,
|
|
|
|
|
|
tips: state.templateTwoTf.text);
|
|
|
|
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
2024-08-21 14:12:15 +08:00
|
|
|
|
showToast('修改成功'.tr);
|
2024-06-04 18:09:38 +08:00
|
|
|
|
Get.back(result: true);
|
2024-06-04 14:44:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-06 13:58:21 +08:00
|
|
|
|
// 更新短信条数的函数
|
2024-07-09 16:34:45 +08:00
|
|
|
|
int calculateSmsCost() {
|
|
|
|
|
|
final String smsText =
|
|
|
|
|
|
'${state.templateOneTf.text}\n${state.currentTemplate.value.template}\n${state.templateTwoTf.text}';
|
|
|
|
|
|
final int smsCount = smsText.length;
|
2024-06-06 13:58:21 +08:00
|
|
|
|
if (smsCount <= 70) {
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return (smsCount / 67).ceil();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//构建电子钥匙模板
|
2024-06-04 14:44:04 +08:00
|
|
|
|
List<TextSpan> buildElectronicKeySpan({required bool isPreview}) {
|
2024-06-06 13:58:21 +08:00
|
|
|
|
final List<TextSpan> textSpans = <TextSpan>[];
|
2024-06-04 14:44:04 +08:00
|
|
|
|
// 如果是预览模式,添加预览模板的文本
|
|
|
|
|
|
if (isPreview) {
|
|
|
|
|
|
textSpans.add(
|
|
|
|
|
|
TextSpan(
|
|
|
|
|
|
text: '${state.templateOneTf.text}\n',
|
|
|
|
|
|
style: state.defaultStyle,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2024-07-02 17:42:26 +08:00
|
|
|
|
|
|
|
|
|
|
if (isPreview) {
|
|
|
|
|
|
if (state.currentTemplate.value.generatePreview().isNotEmpty) {
|
|
|
|
|
|
textSpans.add(
|
|
|
|
|
|
TextSpan(
|
|
|
|
|
|
text: state.currentTemplate.value.generatePreview(),
|
|
|
|
|
|
style: state.defaultStyle,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
textSpans.addAll(_buildDefaultTemplate(isPreview));
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
textSpans.addAll(_buildDefaultTemplate(isPreview));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-09 16:04:56 +08:00
|
|
|
|
if (isPreview) {
|
|
|
|
|
|
textSpans.add(
|
|
|
|
|
|
TextSpan(
|
|
|
|
|
|
text: '\n${state.templateTwoTf.text}\n',
|
|
|
|
|
|
style: state.defaultStyle,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-02 17:42:26 +08:00
|
|
|
|
return textSpans;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<TextSpan> _buildDefaultTemplate(bool isPreview) {
|
|
|
|
|
|
final List<TextSpan> textSpans = <TextSpan>[];
|
2024-06-04 14:44:04 +08:00
|
|
|
|
// 将模板分割为文本片段
|
|
|
|
|
|
final List<String> textFragments = state.currentTemplate.value.template
|
|
|
|
|
|
?.split(RegularExpression.urlRegExp) ??
|
2024-06-06 13:58:21 +08:00
|
|
|
|
<String>[];
|
2024-06-04 14:44:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 添加链接文本和普通文本到文本片段列表
|
|
|
|
|
|
for (int i = 0; i < textFragments.length; i++) {
|
|
|
|
|
|
final String textFragment = textFragments[i];
|
|
|
|
|
|
// 添加普通文本
|
|
|
|
|
|
textSpans.add(
|
|
|
|
|
|
TextSpan(
|
|
|
|
|
|
text: textFragment,
|
|
|
|
|
|
style: state.defaultStyle,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
// 如果不是最后一个文本片段,则添加换行符
|
|
|
|
|
|
if (i < textFragments.length - 1) {
|
|
|
|
|
|
textSpans.add(
|
|
|
|
|
|
TextSpan(
|
|
|
|
|
|
text: '\n',
|
|
|
|
|
|
style: state.defaultStyle,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
// 添加链接文本
|
|
|
|
|
|
textSpans.add(
|
|
|
|
|
|
TextSpan(
|
|
|
|
|
|
text: RegularExpression.urlRegExp
|
|
|
|
|
|
.stringMatch(state.currentTemplate.value.template!) ??
|
|
|
|
|
|
'',
|
|
|
|
|
|
style: state.highStyle,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return textSpans;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-09 16:04:56 +08:00
|
|
|
|
List<TextSpan> buildPasswordSpan({required bool isPreview}) {
|
2024-06-04 14:44:04 +08:00
|
|
|
|
final List<TextSpan> textSpans = <TextSpan>[];
|
|
|
|
|
|
|
|
|
|
|
|
// 如果是预览模式,添加预览模板的文本
|
|
|
|
|
|
if (isPreview) {
|
|
|
|
|
|
textSpans.add(
|
|
|
|
|
|
TextSpan(
|
|
|
|
|
|
text: '${state.templateOneTf.text}\n',
|
|
|
|
|
|
style: state.defaultStyle,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
2024-07-09 16:04:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (isPreview) {
|
|
|
|
|
|
if (state.currentTemplate.value.generatePreview().isNotEmpty) {
|
2024-06-06 13:58:21 +08:00
|
|
|
|
textSpans.add(
|
|
|
|
|
|
TextSpan(
|
2024-07-09 16:04:56 +08:00
|
|
|
|
text: state.currentTemplate.value.generatePreview(),
|
2024-06-06 13:58:21 +08:00
|
|
|
|
style: state.defaultStyle,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2024-07-09 16:04:56 +08:00
|
|
|
|
// 在预览模式下,添加预览模板的文本
|
|
|
|
|
|
if (isPreview) {
|
2024-07-02 17:42:26 +08:00
|
|
|
|
textSpans.add(
|
|
|
|
|
|
TextSpan(
|
2024-07-09 16:04:56 +08:00
|
|
|
|
text: '\n${state.templateTwoTf.text}',
|
2024-07-02 17:42:26 +08:00
|
|
|
|
style: state.defaultStyle,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-07-09 16:04:56 +08:00
|
|
|
|
return textSpans;
|
|
|
|
|
|
}
|
2024-06-06 13:58:21 +08:00
|
|
|
|
|
2024-07-09 16:04:56 +08:00
|
|
|
|
// 定义匹配 ${} 包围的变量的正则表达式
|
|
|
|
|
|
final RegExp variableRegExp = RegExp(r'\$\{([^}]+)\}');
|
|
|
|
|
|
final String template = state.currentTemplate.value.template ?? '';
|
2024-06-06 13:58:21 +08:00
|
|
|
|
|
2024-07-09 16:04:56 +08:00
|
|
|
|
// 对模板进行处理
|
|
|
|
|
|
int startIndex = 0;
|
|
|
|
|
|
for (final Match match in variableRegExp.allMatches(template)) {
|
|
|
|
|
|
// 处理变量之前的文本
|
|
|
|
|
|
final String nonVariableText =
|
|
|
|
|
|
template.substring(startIndex, match.start);
|
2024-06-06 13:58:21 +08:00
|
|
|
|
// 替换非变量文本中的字符
|
2024-07-09 16:04:56 +08:00
|
|
|
|
final String replacedNonVariableText =
|
|
|
|
|
|
nonVariableText.replaceAllMapped(RegExp(r',|。'), (Match match) {
|
2024-06-06 13:58:21 +08:00
|
|
|
|
return '${match.group(0)}\n';
|
|
|
|
|
|
});
|
2024-06-04 14:44:04 +08:00
|
|
|
|
textSpans.add(
|
|
|
|
|
|
TextSpan(
|
2024-07-09 16:04:56 +08:00
|
|
|
|
text: replacedNonVariableText,
|
2024-06-04 14:44:04 +08:00
|
|
|
|
style: state.defaultStyle,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
2024-06-06 13:58:21 +08:00
|
|
|
|
|
2024-07-09 16:04:56 +08:00
|
|
|
|
// 处理变量
|
|
|
|
|
|
final String variableText = match.group(0) ?? '';
|
|
|
|
|
|
textSpans.add(
|
|
|
|
|
|
TextSpan(
|
|
|
|
|
|
text: variableText,
|
|
|
|
|
|
style: state.highStyle,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 更新起始索引
|
|
|
|
|
|
startIndex = match.end;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 添加最后一个变量之后的文本
|
|
|
|
|
|
final String remainingText = template.substring(startIndex);
|
|
|
|
|
|
// 替换非变量文本中的字符
|
|
|
|
|
|
final String replacedRemainingText =
|
|
|
|
|
|
remainingText.replaceAllMapped(RegExp(r',|。'), (Match match) {
|
|
|
|
|
|
return '${match.group(0)}\n';
|
|
|
|
|
|
});
|
|
|
|
|
|
textSpans.add(
|
|
|
|
|
|
TextSpan(
|
|
|
|
|
|
text: replacedRemainingText,
|
|
|
|
|
|
style: state.defaultStyle,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 在预览模式下,添加预览模板的文本
|
|
|
|
|
|
if (isPreview) {
|
|
|
|
|
|
textSpans.add(
|
|
|
|
|
|
TextSpan(
|
|
|
|
|
|
text: '\n${state.templateTwoTf.text}',
|
|
|
|
|
|
style: state.defaultStyle,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
2024-06-04 14:44:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return textSpans;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
Future<void> onReady() async {
|
|
|
|
|
|
super.onReady();
|
2024-06-04 18:09:38 +08:00
|
|
|
|
|
2024-07-02 17:42:26 +08:00
|
|
|
|
// if (state.currentTemplate.value.isUpdate == false) {
|
|
|
|
|
|
getDefaultTemplate();
|
|
|
|
|
|
// }
|
2024-06-04 14:44:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|