1,新建邮件模版(电子钥匙、密码)API对接及兼容短信模版

2,删除邮件模版API对接及对应逻辑处理
3,新增计算短信条数函数及更新布局
This commit is contained in:
Daisy 2024-06-06 13:58:21 +08:00
parent fe5b3089a3
commit 7d01b9a056
4 changed files with 227 additions and 199 deletions

View File

@ -15,7 +15,7 @@ class NewSMSTemplateLogic extends BaseGetXController {
//-- 1: 2:
Future<void> 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 ?? <SMSTemplateData>[];
if (state.templateList.isNotEmpty) {
@ -59,9 +59,32 @@ class NewSMSTemplateLogic extends BaseGetXController {
}
}
List<TextSpan> buildElectronicKeySpan({required bool isPreview}) {
final List<TextSpan> textSpans = <TextSpan>[];
//
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(
@ -74,7 +97,7 @@ class NewSMSTemplateLogic extends BaseGetXController {
//
final List<String> textFragments = state.currentTemplate.value.template
?.split(RegularExpression.urlRegExp) ??
[];
<String>[];
//
for (int i = 0; i < textFragments.length; i++) {
@ -115,7 +138,62 @@ class NewSMSTemplateLogic extends BaseGetXController {
),
);
}
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;
}
@ -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;

View File

@ -28,7 +28,7 @@ class _NewSMSTemplatePageState extends State<NewSMSTemplatePage> {
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<NewSMSTemplatePage> {
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: <Widget>[
_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: <Widget>[
_buildTextField(state.templateTwoTf),
],
),
))),
],
),
);
@ -144,7 +150,9 @@ class _NewSMSTemplatePageState extends State<NewSMSTemplatePage> {
),
),
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<NewSMSTemplatePage> {
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<NewSMSTemplatePage> {
state.currentTemplate.value.template!.isEmpty) {
return const SizedBox.shrink(); // SizedBox
} else {
return Row(
children: <Widget>[
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<NewSMSTemplatePage> {
onChanged: (String value) {
setState(() {
controller.text = value;
logic.updateSmsCost(value); //
});
},
);

View File

@ -30,7 +30,8 @@ class NewSMSTemplateState {
RxBool isVip = false.obs;
RxList<SMSTemplateData> templateList = <SMSTemplateData>[].obs;
Rx<SMSTemplateData> currentTemplate = SMSTemplateData().obs;
RxBool isShowDate = false.obs;
RxInt templateType = 0.obs;
Rx<SMSTemplateData> currentTemplate = SMSTemplateData().obs; //
RxBool isShowDate = false.obs; //
RxInt templateType = 0.obs; //1 2
RxInt smsCost = 0.obs; //
}

View File

@ -12,9 +12,8 @@ import '../../../../tools/baseGetXController.dart';
class CustomSMSTemplateListLogic extends BaseGetXController {
CustomSMSTemplateListState state = CustomSMSTemplateListState();
//
//
Future<void> getSMSTemplateListRequest({required bool isRefresh}) async {
//
if (isRefresh) {
state.smsTemplateList.clear();
pageNo = 1;
@ -31,7 +30,7 @@ class CustomSMSTemplateListLogic extends BaseGetXController {
}
}
//
//
Future<void> deleteSMSTemplateRequest({required int id}) async {
final LoginEntity entity =
await ApiRepository.to.deleteTemplateInfo(id: id);
@ -42,131 +41,75 @@ class CustomSMSTemplateListLogic extends BaseGetXController {
List<TextSpan> buildElectronicKeySpan(
{required CustomSMSTemplateItem templateData}) {
final List<TextSpan> textSpans = <TextSpan>[];
textSpans.add(
TextSpan(
text: '${templateData.regards}\n',
style: state.defaultStyle,
),
);
//
final List<String> 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<TextSpan> 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<TextSpan> buildPasswordSpan(
{required CustomSMSTemplateItem templateData}) {
final List<TextSpan> textSpans = <TextSpan>[];
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<TextSpan> 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<TextSpan> textSpans, String? template,
TextStyle defaultStyle, TextStyle highStyle) {
final List<String> 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<TextSpan> 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<TextSpan> textSpans, String text, TextStyle style) {
textSpans.add(TextSpan(text: text, style: style));
}
@override
onReady() async {
Future<void> onReady() async {
super.onReady();
var isVip = await Storage.getBool(saveIsVip);
state.isVip.value = isVip ?? false;
}