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: //-- 1: 2:
Future<void> getDefaultTemplate() async { Future<void> getDefaultTemplate() async {
final NewSMSTemplateEntity entity = await ApiRepository.to final NewSMSTemplateEntity entity = await ApiRepository.to
.getDefaultTemplate(type: state.currentTemplate.value.type ?? 0); .getDefaultTemplate(type: state.templateType.value);
if (entity.errorCode!.codeIsSuccessful) { if (entity.errorCode!.codeIsSuccessful) {
state.templateList.value = entity.dataList ?? <SMSTemplateData>[]; state.templateList.value = entity.dataList ?? <SMSTemplateData>[];
if (state.templateList.isNotEmpty) { 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) { if (isPreview) {
textSpans.add( textSpans.add(
@ -74,7 +97,7 @@ class NewSMSTemplateLogic extends BaseGetXController {
// //
final List<String> textFragments = state.currentTemplate.value.template final List<String> textFragments = state.currentTemplate.value.template
?.split(RegularExpression.urlRegExp) ?? ?.split(RegularExpression.urlRegExp) ??
[]; <String>[];
// //
for (int i = 0; i < textFragments.length; i++) { 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; 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; int startIndex = 0;
for (final Match match in variableRegExp.allMatches(template)) { for (final Match match in variableRegExp.allMatches(template)) {
// //
final String nonVariableText = final String nonVariableText =
template.substring(startIndex, match.start); 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 = final String replacedRemainingText =
nonVariableText.replaceAllMapped(RegExp(r'|。'), (Match match) { remainingText.replaceAllMapped(RegExp(r'|。'), (Match match) {
return '${match.group(0)}\n'; return '${match.group(0)}\n';
}); });
textSpans.add( textSpans.add(
TextSpan( TextSpan(
text: replacedNonVariableText, text: replacedRemainingText,
style: state.defaultStyle, style: state.defaultStyle,
), ),
); );
// //
final String variableText = match.group(0) ?? ''; if (isPreview) {
textSpans.add( textSpans.add(
TextSpan( TextSpan(
text: variableText, text: '\n${state.templateTwoTf.text}',
style: state.highStyle, style: state.defaultStyle,
), ),
); );
}
//
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; return textSpans;

View File

@ -28,7 +28,7 @@ class _NewSMSTemplatePageState extends State<NewSMSTemplatePage> {
return Scaffold( return Scaffold(
backgroundColor: AppColors.mainBackgroundColor, backgroundColor: AppColors.mainBackgroundColor,
appBar: TitleAppBar( appBar: TitleAppBar(
barTitle: state.templateType.value == 1 ? '新建短信模版'.tr : '新建邮件模版'.tr, barTitle: state.templateType == 1 ? '新建短信模版'.tr : '新建邮件模版'.tr,
haveBack: true, haveBack: true,
backgroundColor: AppColors.mainColor, backgroundColor: AppColors.mainColor,
), ),
@ -109,18 +109,24 @@ class _NewSMSTemplatePageState extends State<NewSMSTemplatePage> {
height: 100, height: 100,
child: _buildTextField(state.templateOneTf), 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), SizedBox(height: 10.h),
Container( Obx(() => Visibility(
margin: EdgeInsets.symmetric(horizontal: 25.w, vertical: 25.h), visible: state.templateType.value == 1,
height: 100, child: Container(
child: Stack( margin: EdgeInsets.symmetric(horizontal: 25.w, vertical: 25.h),
alignment: Alignment.bottomRight, height: 100,
children: <Widget>[ child: Stack(
_buildTextField(state.templateTwoTf), alignment: Alignment.bottomRight,
], children: <Widget>[
), _buildTextField(state.templateTwoTf),
), ],
),
))),
], ],
), ),
); );
@ -144,7 +150,9 @@ class _NewSMSTemplatePageState extends State<NewSMSTemplatePage> {
), ),
), ),
Obx(() => Container( Obx(() => Container(
width: 1.sw - 50.w,
margin: EdgeInsets.only(left: 25.w, right: 25.w), margin: EdgeInsets.only(left: 25.w, right: 25.w),
padding: EdgeInsets.only(left: 20.w, right: 20.w, top: 10.h),
decoration: BoxDecoration( decoration: BoxDecoration(
color: const Color(0xFFF5F5F5), color: const Color(0xFFF5F5F5),
borderRadius: BorderRadius.circular(10.h), borderRadius: BorderRadius.circular(10.h),
@ -158,7 +166,7 @@ class _NewSMSTemplatePageState extends State<NewSMSTemplatePage> {
bottom: 25.h, bottom: 25.h,
), ),
child: Text( child: Text(
'预计产生短信条数:2', '预计产生短信条数:${state.smsCost.value}',
style: TextStyle( style: TextStyle(
color: Colors.grey, color: Colors.grey,
fontSize: 20.sp, fontSize: 20.sp,
@ -176,19 +184,12 @@ class _NewSMSTemplatePageState extends State<NewSMSTemplatePage> {
state.currentTemplate.value.template!.isEmpty) { state.currentTemplate.value.template!.isEmpty) {
return const SizedBox.shrink(); // SizedBox return const SizedBox.shrink(); // SizedBox
} else { } else {
return Row( return RichText(
children: <Widget>[ text: TextSpan(
Padding( children: state.currentTemplate.value.typeName == '电子钥匙'
padding: EdgeInsets.symmetric(horizontal: 25.w, vertical: 10.h), ? logic.buildElectronicKeySpan(isPreview: isPreview)
child: Obx(() => RichText( : logic.buildPasswordSpan(isPreview: isPreview),
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) { onChanged: (String value) {
setState(() { setState(() {
controller.text = value; controller.text = value;
logic.updateSmsCost(value); //
}); });
}, },
); );

View File

@ -30,7 +30,8 @@ class NewSMSTemplateState {
RxBool isVip = false.obs; RxBool isVip = false.obs;
RxList<SMSTemplateData> templateList = <SMSTemplateData>[].obs; RxList<SMSTemplateData> templateList = <SMSTemplateData>[].obs;
Rx<SMSTemplateData> currentTemplate = SMSTemplateData().obs; Rx<SMSTemplateData> currentTemplate = SMSTemplateData().obs; //
RxBool isShowDate = false.obs; RxBool isShowDate = false.obs; //
RxInt templateType = 0.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 { class CustomSMSTemplateListLogic extends BaseGetXController {
CustomSMSTemplateListState state = CustomSMSTemplateListState(); CustomSMSTemplateListState state = CustomSMSTemplateListState();
// //
Future<void> getSMSTemplateListRequest({required bool isRefresh}) async { Future<void> getSMSTemplateListRequest({required bool isRefresh}) async {
//
if (isRefresh) { if (isRefresh) {
state.smsTemplateList.clear(); state.smsTemplateList.clear();
pageNo = 1; pageNo = 1;
@ -31,7 +30,7 @@ class CustomSMSTemplateListLogic extends BaseGetXController {
} }
} }
// //
Future<void> deleteSMSTemplateRequest({required int id}) async { Future<void> deleteSMSTemplateRequest({required int id}) async {
final LoginEntity entity = final LoginEntity entity =
await ApiRepository.to.deleteTemplateInfo(id: id); await ApiRepository.to.deleteTemplateInfo(id: id);
@ -42,131 +41,75 @@ class CustomSMSTemplateListLogic extends BaseGetXController {
List<TextSpan> buildElectronicKeySpan( List<TextSpan> buildElectronicKeySpan(
{required CustomSMSTemplateItem templateData}) { {required CustomSMSTemplateItem templateData}) {
final List<TextSpan> textSpans = <TextSpan>[]; final List<TextSpan> textSpans = [];
_addTextSpan(textSpans, '${templateData.regards}\n', state.defaultStyle);
textSpans.add( _buildTextSpansFromTemplate(
TextSpan( textSpans, templateData.template, state.defaultStyle, state.highStyle);
text: '${templateData.regards}\n', _addTextSpan(textSpans, '\n${templateData.tips}', state.defaultStyle);
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,
),
);
return textSpans; return textSpans;
} }
List<TextSpan> buildPasswordSpan( List<TextSpan> buildPasswordSpan(
{required CustomSMSTemplateItem templateData}) { {required CustomSMSTemplateItem templateData}) {
final List<TextSpan> textSpans = <TextSpan>[]; final List<TextSpan> textSpans = [];
_addTextSpan(textSpans, '${templateData.regards}\n', state.defaultStyle);
textSpans.add( _buildPasswordTextSpans(
TextSpan( textSpans, templateData.template, state.defaultStyle, state.highStyle);
text: '${templateData.regards}\n', _addTextSpan(textSpans, '\n${templateData.tips}', state.defaultStyle);
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,
),
);
return textSpans; 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 @override
onReady() async { Future<void> onReady() async {
super.onReady();
var isVip = await Storage.getBool(saveIsVip); var isVip = await Storage.getBool(saveIsVip);
state.isVip.value = isVip ?? false; state.isVip.value = isVip ?? false;
} }