From 7d01b9a05620fa0abf14a07b4ab238a40bb9c0a7 Mon Sep 17 00:00:00 2001 From: Daisy <> Date: Thu, 6 Jun 2024 13:58:21 +0800 Subject: [PATCH] =?UTF-8?q?1=EF=BC=8C=E6=96=B0=E5=BB=BA=E9=82=AE=E4=BB=B6?= =?UTF-8?q?=E6=A8=A1=E7=89=88=EF=BC=88=E7=94=B5=E5=AD=90=E9=92=A5=E5=8C=99?= =?UTF-8?q?=E3=80=81=E5=AF=86=E7=A0=81=EF=BC=89API=E5=AF=B9=E6=8E=A5?= =?UTF-8?q?=E5=8F=8A=E5=85=BC=E5=AE=B9=E7=9F=AD=E4=BF=A1=E6=A8=A1=E7=89=88?= =?UTF-8?q?=202=EF=BC=8C=E5=88=A0=E9=99=A4=E9=82=AE=E4=BB=B6=E6=A8=A1?= =?UTF-8?q?=E7=89=88API=E5=AF=B9=E6=8E=A5=E5=8F=8A=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=A4=84=E7=90=86=203=EF=BC=8C=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E8=AE=A1=E7=AE=97=E7=9F=AD=E4=BF=A1=E6=9D=A1=E6=95=B0?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=8F=8A=E6=9B=B4=E6=96=B0=E5=B8=83=E5=B1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../newSMSTemplate_logic.dart | 184 +++++++++++++----- .../newSMSTemplate_page.dart | 54 ++--- .../newSMSTemplate_state.dart | 7 +- .../customSMSTemplateList_logic.dart | 181 ++++++----------- 4 files changed, 227 insertions(+), 199 deletions(-) diff --git a/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesAddSMSTemplate/newSMSTemplate_logic.dart b/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesAddSMSTemplate/newSMSTemplate_logic.dart index 0bd6c527..0c149a5d 100644 --- a/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesAddSMSTemplate/newSMSTemplate_logic.dart +++ b/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesAddSMSTemplate/newSMSTemplate_logic.dart @@ -15,7 +15,7 @@ class NewSMSTemplateLogic extends BaseGetXController { //获取默认模板-- 1:电子钥匙 2:密码 Future getDefaultTemplate() async { final NewSMSTemplateEntity entity = await ApiRepository.to - .getDefaultTemplate(type: state.currentTemplate.value.type ?? 0); + .getDefaultTemplate(type: state.templateType.value); if (entity.errorCode!.codeIsSuccessful) { state.templateList.value = entity.dataList ?? []; if (state.templateList.isNotEmpty) { @@ -59,9 +59,32 @@ class NewSMSTemplateLogic extends BaseGetXController { } } - List buildElectronicKeySpan({required bool isPreview}) { - final List textSpans = []; + // 更新短信条数的函数 + 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 buildElectronicKeySpan({required bool isPreview}) { + //短信模版 + if (state.templateType.value == 1) { + return _buildSMSElectronicKey(isPreview); + } else { + return _buildEmailElectronicKey(isPreview); + } + } + + List _buildSMSElectronicKey(bool isPreview) { + final List textSpans = []; // 如果是预览模式,添加预览模板的文本 if (isPreview) { textSpans.add( @@ -74,7 +97,7 @@ class NewSMSTemplateLogic extends BaseGetXController { // 将模板分割为文本片段 final List textFragments = state.currentTemplate.value.template ?.split(RegularExpression.urlRegExp) ?? - []; + []; // 添加链接文本和普通文本到文本片段列表 for (int i = 0; i < textFragments.length; i++) { @@ -115,7 +138,62 @@ class NewSMSTemplateLogic extends BaseGetXController { ), ); } + return textSpans; + } + List _buildEmailElectronicKey(bool isPreview) { + final List textSpans = []; + + //邮件模版 + // 如果是预览模式,添加预览模板的文本 + 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 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; } @@ -132,64 +210,68 @@ class NewSMSTemplateLogic extends BaseGetXController { ); } - // 定义匹配 ${} 包围的变量的正则表达式 - final RegExp variableRegExp = RegExp(r'\$\{([^}]+)\}'); + //短信模版才需要加默认模版 + if (state.templateType == 1 || + (state.templateType == 2 && isPreview == false)) { + // 定义匹配 ${} 包围的变量的正则表达式 + final RegExp variableRegExp = RegExp(r'\$\{([^}]+)\}'); - final String template = state.currentTemplate.value.template ?? ''; + 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); + // 对模板进行处理 + 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 replacedNonVariableText = - nonVariableText.replaceAllMapped(RegExp(r',|。'), (Match match) { + final String replacedRemainingText = + remainingText.replaceAllMapped(RegExp(r',|。'), (Match match) { return '${match.group(0)}\n'; }); textSpans.add( TextSpan( - text: replacedNonVariableText, + text: replacedRemainingText, 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, - ), - ); + // 在预览模式下,添加预览模板的文本 + if (isPreview) { + textSpans.add( + TextSpan( + text: '\n${state.templateTwoTf.text}', + style: state.defaultStyle, + ), + ); + } } return textSpans; diff --git a/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesAddSMSTemplate/newSMSTemplate_page.dart b/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesAddSMSTemplate/newSMSTemplate_page.dart index b61a7e2e..afee8c2f 100755 --- a/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesAddSMSTemplate/newSMSTemplate_page.dart +++ b/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesAddSMSTemplate/newSMSTemplate_page.dart @@ -28,7 +28,7 @@ class _NewSMSTemplatePageState extends State { return Scaffold( backgroundColor: AppColors.mainBackgroundColor, appBar: TitleAppBar( - barTitle: state.templateType.value == 1 ? '新建短信模版'.tr : '新建邮件模版'.tr, + barTitle: state.templateType == 1 ? '新建短信模版'.tr : '新建邮件模版'.tr, haveBack: true, backgroundColor: AppColors.mainColor, ), @@ -109,18 +109,24 @@ class _NewSMSTemplatePageState extends State { height: 100, child: _buildTextField(state.templateOneTf), ), - Obx(() => _buildTemplateWithType(isPreview: false)), + Obx(() => Container( + width: 1.sw - 50.w, + margin: EdgeInsets.only(left: 25.w, right: 25.w), + child: _buildTemplateWithType(isPreview: false), + )), SizedBox(height: 10.h), - Container( - margin: EdgeInsets.symmetric(horizontal: 25.w, vertical: 25.h), - height: 100, - child: Stack( - alignment: Alignment.bottomRight, - children: [ - _buildTextField(state.templateTwoTf), - ], - ), - ), + Obx(() => Visibility( + visible: state.templateType.value == 1, + child: Container( + margin: EdgeInsets.symmetric(horizontal: 25.w, vertical: 25.h), + height: 100, + child: Stack( + alignment: Alignment.bottomRight, + children: [ + _buildTextField(state.templateTwoTf), + ], + ), + ))), ], ), ); @@ -144,7 +150,9 @@ class _NewSMSTemplatePageState extends State { ), ), Obx(() => Container( + width: 1.sw - 50.w, margin: EdgeInsets.only(left: 25.w, right: 25.w), + padding: EdgeInsets.only(left: 20.w, right: 20.w, top: 10.h), decoration: BoxDecoration( color: const Color(0xFFF5F5F5), borderRadius: BorderRadius.circular(10.h), @@ -158,7 +166,7 @@ class _NewSMSTemplatePageState extends State { bottom: 25.h, ), child: Text( - '预计产生短信条数:2', + '预计产生短信条数:${state.smsCost.value}', style: TextStyle( color: Colors.grey, fontSize: 20.sp, @@ -176,19 +184,12 @@ class _NewSMSTemplatePageState extends State { state.currentTemplate.value.template!.isEmpty) { return const SizedBox.shrink(); // 如果为空,返回一个空的 SizedBox } else { - return Row( - children: [ - Padding( - padding: EdgeInsets.symmetric(horizontal: 25.w, vertical: 10.h), - child: Obx(() => RichText( - text: TextSpan( - children: state.currentTemplate.value.typeName == '电子钥匙' - ? logic.buildElectronicKeySpan(isPreview: isPreview) - : logic.buildPasswordSpan(isPreview: isPreview), - ), - )), - ), - ], + return RichText( + text: TextSpan( + children: state.currentTemplate.value.typeName == '电子钥匙' + ? logic.buildElectronicKeySpan(isPreview: isPreview) + : logic.buildPasswordSpan(isPreview: isPreview), + ), ); } } @@ -213,6 +214,7 @@ class _NewSMSTemplatePageState extends State { onChanged: (String value) { setState(() { controller.text = value; + logic.updateSmsCost(value); // 更新短信条数 }); }, ); diff --git a/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesAddSMSTemplate/newSMSTemplate_state.dart b/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesAddSMSTemplate/newSMSTemplate_state.dart index 2da43402..d2b540d2 100644 --- a/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesAddSMSTemplate/newSMSTemplate_state.dart +++ b/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesAddSMSTemplate/newSMSTemplate_state.dart @@ -30,7 +30,8 @@ class NewSMSTemplateState { RxBool isVip = false.obs; RxList templateList = [].obs; - Rx currentTemplate = SMSTemplateData().obs; - RxBool isShowDate = false.obs; - RxInt templateType = 0.obs; + Rx currentTemplate = SMSTemplateData().obs; //当前模板信息 + RxBool isShowDate = false.obs; //是否显示日期 + RxInt templateType = 0.obs; //1:短信 2:邮件 + RxInt smsCost = 0.obs; //短信条数 } diff --git a/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesListSMSTemplate/customSMSTemplateList_logic.dart b/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesListSMSTemplate/customSMSTemplateList_logic.dart index 12c38ebf..af8e5d31 100644 --- a/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesListSMSTemplate/customSMSTemplateList_logic.dart +++ b/lib/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesListSMSTemplate/customSMSTemplateList_logic.dart @@ -12,9 +12,8 @@ import '../../../../tools/baseGetXController.dart'; class CustomSMSTemplateListLogic extends BaseGetXController { CustomSMSTemplateListState state = CustomSMSTemplateListState(); - //获取短信模板列表 + // 获取短信模板列表 Future getSMSTemplateListRequest({required bool isRefresh}) async { - // 如果是下拉刷新,清空已有数据 if (isRefresh) { state.smsTemplateList.clear(); pageNo = 1; @@ -31,7 +30,7 @@ class CustomSMSTemplateListLogic extends BaseGetXController { } } - //删除短信模版 + // 删除短信模板 Future deleteSMSTemplateRequest({required int id}) async { final LoginEntity entity = await ApiRepository.to.deleteTemplateInfo(id: id); @@ -42,131 +41,75 @@ class CustomSMSTemplateListLogic extends BaseGetXController { List buildElectronicKeySpan( {required CustomSMSTemplateItem templateData}) { - final List textSpans = []; - - textSpans.add( - TextSpan( - text: '${templateData.regards}\n', - style: state.defaultStyle, - ), - ); - // 将模板分割为文本片段 - final List textFragments = - templateData.template?.split(RegularExpression.urlRegExp) ?? []; - - // 添加链接文本和普通文本到文本片段列表 - 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(templateData.template!) ?? - '', - style: state.highStyle, - ), - ); - } - } - - textSpans.add( - TextSpan( - text: '\n${templateData.tips}', - style: state.defaultStyle, - ), - ); - + final List textSpans = []; + _addTextSpan(textSpans, '${templateData.regards}\n', state.defaultStyle); + _buildTextSpansFromTemplate( + textSpans, templateData.template, state.defaultStyle, state.highStyle); + _addTextSpan(textSpans, '\n${templateData.tips}', state.defaultStyle); return textSpans; } List buildPasswordSpan( {required CustomSMSTemplateItem templateData}) { - final List textSpans = []; - - textSpans.add( - TextSpan( - text: '${templateData.regards}\n', - style: state.defaultStyle, - ), - ); - - // 定义匹配 ${} 包围的变量的正则表达式 - final RegExp variableRegExp = RegExp(r'\$\{([^}]+)\}'); - - final String template = templateData.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, - ), - ); - - textSpans.add( - TextSpan( - text: '\n${templateData.tips}', - style: state.defaultStyle, - ), - ); - + final List textSpans = []; + _addTextSpan(textSpans, '${templateData.regards}\n', state.defaultStyle); + _buildPasswordTextSpans( + textSpans, templateData.template, state.defaultStyle, state.highStyle); + _addTextSpan(textSpans, '\n${templateData.tips}', state.defaultStyle); return textSpans; } + void _buildTextSpansFromTemplate(List textSpans, String? template, + TextStyle defaultStyle, TextStyle highStyle) { + final List textFragments = + template?.split(RegularExpression.urlRegExp) ?? []; + for (int i = 0; i < textFragments.length; i++) { + _addTextSpan(textSpans, textFragments[i], defaultStyle); + if (i < textFragments.length - 1) { + _addTextSpan(textSpans, '\n', defaultStyle); + _addTextSpan( + textSpans, + RegularExpression.urlRegExp.stringMatch(template!) ?? '', + highStyle); + } + } + } + + void _buildPasswordTextSpans(List textSpans, String? template, + TextStyle defaultStyle, TextStyle highStyle) { + final RegExp variableRegExp = RegExp(r'\$\{([^}]+)\}'); + final String text = template ?? ''; + int startIndex = 0; + + for (final Match match in variableRegExp.allMatches(text)) { + _addTextSpan( + textSpans, + text + .substring(startIndex, match.start) + .replaceAllMapped(RegExp(r',|。'), (Match match) { + return '${match.group(0)}\n'; + }), + defaultStyle); + _addTextSpan(textSpans, match.group(0) ?? '', highStyle); + startIndex = match.end; + } + + _addTextSpan( + textSpans, + text.substring(startIndex).replaceAllMapped(RegExp(r',|。'), + (Match match) { + return '${match.group(0)}\n'; + }), + defaultStyle); + } + + void _addTextSpan(List textSpans, String text, TextStyle style) { + textSpans.add(TextSpan(text: text, style: style)); + } + @override - onReady() async { + Future onReady() async { + super.onReady(); var isVip = await Storage.getBool(saveIsVip); state.isVip.value = isVip ?? false; }