Daisy 7d01b9a056 1,新建邮件模版(电子钥匙、密码)API对接及兼容短信模版
2,删除邮件模版API对接及对应逻辑处理
3,新增计算短信条数函数及更新布局
2024-06-06 13:58:21 +08:00

289 lines
8.4 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 {
final NewSMSTemplateEntity entity = await ApiRepository.to
.getDefaultTemplate(type: state.templateType.value);
if (entity.errorCode!.codeIsSuccessful) {
state.templateList.value = entity.dataList ?? <SMSTemplateData>[];
if (state.templateList.isNotEmpty) {
state.currentTemplate.value = state.templateList.firstWhere(
(SMSTemplateData element) =>
element.name == state.templateList[0].name,
);
state.currentTemplate.refresh();
}
}
}
//新建模板-- 1:电子钥匙 2:密码
Future<void> addSMSTemplate() async {
if (state.templateNameTf.text.isEmpty) {
showToast('请输入模板名称');
return;
}
final LoginEntity entity = await ApiRepository.to.addTemplateService(
type: state.templateType.value,
name: state.templateNameTf.text,
fixedKey: state.currentTemplate.value.fixedKey ?? '',
contentType: state.currentTemplate.value.contentType ?? 0,
regards: state.templateOneTf.text,
tips: state.templateTwoTf.text);
if (entity.errorCode!.codeIsSuccessful) {
showToast('添加成功');
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) {
showToast('修改成功');
Get.back(result: true);
}
}
// 更新短信条数的函数
void updateSmsCost(String template) {
state.smsCost.value = calculateSmsCost(template);
}
int calculateSmsCost(String template) {
final int smsCount = template.length;
if (smsCount <= 70) {
return 1;
} else {
return (smsCount / 67).ceil();
}
}
//构建电子钥匙模板
List<TextSpan> buildElectronicKeySpan({required bool isPreview}) {
//短信模版
if (state.templateType.value == 1) {
return _buildSMSElectronicKey(isPreview);
} else {
return _buildEmailElectronicKey(isPreview);
}
}
List<TextSpan> _buildSMSElectronicKey(bool isPreview) {
final List<TextSpan> textSpans = <TextSpan>[];
// 如果是预览模式,添加预览模板的文本
if (isPreview) {
textSpans.add(
TextSpan(
text: '${state.templateOneTf.text}\n',
style: state.defaultStyle,
),
);
}
// 将模板分割为文本片段
final List<String> textFragments = state.currentTemplate.value.template
?.split(RegularExpression.urlRegExp) ??
<String>[];
// 添加链接文本和普通文本到文本片段列表
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,
),
);
}
}
// 如果是预览模式,添加预览模板的文本
if (isPreview) {
textSpans.add(
TextSpan(
text: '\n${state.templateTwoTf.text}',
style: state.defaultStyle,
),
);
}
return textSpans;
}
List<TextSpan> _buildEmailElectronicKey(bool isPreview) {
final List<TextSpan> textSpans = <TextSpan>[];
//邮件模版
// 如果是预览模式,添加预览模板的文本
if (isPreview) {
textSpans.add(
TextSpan(
text: '${state.templateOneTf.text}\n',
style: state.defaultStyle,
),
);
} else {
String template = state.currentTemplate.value.template ?? '';
template = template.replaceAll('', '\n');
// 定义匹配 ${} 包围的变量的正则表达式
final RegExp variableRegExp = RegExp(r'\{([^}]+)\}');
final Iterable<Match> matches = variableRegExp.allMatches(template);
int start = 0;
for (final Match match in matches) {
// 添加非变量文本
if (match.start > start) {
textSpans.add(
TextSpan(
text: template.substring(start, match.start),
style: state.defaultStyle,
),
);
}
// 添加变量文本
textSpans.add(
TextSpan(
text: match.group(0),
style: state.highStyle,
),
);
start = match.end;
}
// 添加剩余的非变量文本
if (start < template.length) {
textSpans.add(
TextSpan(
text: template.substring(start),
style: state.defaultStyle,
),
);
}
}
return textSpans;
}
List<TextSpan> buildPasswordSpan({required bool isPreview}) {
final List<TextSpan> textSpans = <TextSpan>[];
// 如果是预览模式,添加预览模板的文本
if (isPreview) {
textSpans.add(
TextSpan(
text: '${state.templateOneTf.text}\n',
style: state.defaultStyle,
),
);
}
//短信模版才需要加默认模版
if (state.templateType == 1 ||
(state.templateType == 2 && isPreview == false)) {
// 定义匹配 ${} 包围的变量的正则表达式
final RegExp variableRegExp = RegExp(r'\$\{([^}]+)\}');
final String template = state.currentTemplate.value.template ?? '';
// 对模板进行处理
int startIndex = 0;
for (final Match match in variableRegExp.allMatches(template)) {
// 处理变量之前的文本
final String nonVariableText =
template.substring(startIndex, match.start);
// 替换非变量文本中的字符
final String replacedNonVariableText =
nonVariableText.replaceAllMapped(RegExp(r'|。'), (Match match) {
return '${match.group(0)}\n';
});
textSpans.add(
TextSpan(
text: replacedNonVariableText,
style: state.defaultStyle,
),
);
// 处理变量
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,
),
);
}
}
return textSpans;
}
@override
Future<void> onReady() async {
super.onReady();
if (state.currentTemplate.value.isUpdate == false) {
getDefaultTemplate();
}
}
}