130 lines
4.5 KiB
Dart
130 lines
4.5 KiB
Dart
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<void> 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 ?? <CustomSMSTemplateItem>[];
|
||
pageNo++;
|
||
} else {
|
||
if (state.smsTemplateList.isNotEmpty) {
|
||
state.smsTemplateList
|
||
.addAll(entity.data?.list ?? <CustomSMSTemplateItem>[]);
|
||
pageNo++;
|
||
}
|
||
}
|
||
state.smsTemplateList.refresh();
|
||
}
|
||
}
|
||
|
||
// 删除短信模板
|
||
Future<void> deleteSMSTemplateRequest({required int id}) async {
|
||
final LoginEntity entity =
|
||
await ApiRepository.to.deleteTemplateInfo(id: id);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
getSMSTemplateListRequest(isRefresh: true);
|
||
}
|
||
}
|
||
|
||
List<TextSpan> buildElectronicKeySpan(
|
||
{required CustomSMSTemplateItem templateData}) {
|
||
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 = [];
|
||
_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));
|
||
}
|
||
|
||
Future<void> getVipStatus() async {
|
||
final bool? isVip = await Storage.getBool(saveIsVip);
|
||
state.isVip.value = isVip ?? false;
|
||
state.isVip.refresh();
|
||
}
|
||
|
||
@override
|
||
Future<void> onReady() async {
|
||
super.onReady();
|
||
}
|
||
}
|