import 'dart:async'; import 'package:flutter/material.dart'; import 'package:star_lock/login/login/entity/LoginEntity.dart'; import 'package:star_lock/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesListSMSTemplate/customSMSTemplateList_entity.dart'; import 'package:star_lock/mine/valueAddedServices/valueAddedServicesSMSTemplate/valueAddedServicesListSMSTemplate/customSMSTemplateList_state.dart'; import 'package:star_lock/tools/regularExpression.dart'; import 'package:star_lock/tools/storage.dart'; import '../../../../network/api_repository.dart'; import '../../../../tools/baseGetXController.dart'; class CustomSMSTemplateListLogic extends BaseGetXController { CustomSMSTemplateListState state = CustomSMSTemplateListState(); // 获取短信模板列表 Future getSMSTemplateListRequest({required bool isRefresh}) async { if (isRefresh) { state.smsTemplateList.clear(); pageNo = 1; } final CustomSMSTemplateListEntity entity = await ApiRepository.to .getSMSTemplateList( channelType: state.channelType.value, pageNo: pageNo, pageSize: int.parse(pageSize)); if (entity.errorCode!.codeIsSuccessful) { if (pageNo == 1) { state.smsTemplateList.value = entity.data?.list ?? []; pageNo++; } else { if (state.smsTemplateList.isNotEmpty) { state.smsTemplateList .addAll(entity.data?.list ?? []); pageNo++; } } state.smsTemplateList.refresh(); } } // 删除短信模板 Future deleteSMSTemplateRequest({required int id}) async { final LoginEntity entity = await ApiRepository.to.deleteTemplateInfo(id: id); if (entity.errorCode!.codeIsSuccessful) { getSMSTemplateListRequest(isRefresh: true); } } List buildElectronicKeySpan( {required CustomSMSTemplateItem templateData}) { 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 = []; _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)); } Future getVipStatus() async { final bool? isVip = await Storage.getBool(saveIsVip); state.isVip.value = isVip ?? false; state.isVip.refresh(); } @override Future onReady() async { super.onReady(); } }