7 changed files with 178 additions and 105 deletions
@ -1,13 +1,16 @@ |
|||||||
|
|
||||||
获取记录:| |
templates: |
||||||
<M>[获取记录][{{tableName}}({{keyName}}) |
获取记录: |
||||||
][ |
header: "<M>[获取记录][{{tableName}}({{keyName}})" |
||||||
{{#keyFields}} |
body: | |
||||||
{{.}} = @{{.}}{{^@last}}, {{/@last}} |
][ |
||||||
{{/keyFields}} |
{{#keyFields}} |
||||||
] |
{{value}} = @{{value}} {{^isLast}}, {{/isLast}} |
||||||
[继续执行] |
{{/keyFields}} |
||||||
[记录为空][{{tableName}}] |
] |
||||||
{ |
footer: | |
||||||
[报错返回][][{{#keyFields}}{{.}} = @{{.}}{{^@last}}, {{/@last}}{{/keyFields}}] |
[继续执行] |
||||||
} |
[记录为空][{{tableName}}] |
||||||
|
{ |
||||||
|
[报错返回][ERR_???][{{#keyFields}}{{value}} = @{{value}}{{^isLast}}, {{/isLast}}{{/keyFields}}] |
||||||
|
} |
@ -0,0 +1,68 @@ |
|||||||
|
import 'package:flutter/services.dart' show rootBundle; |
||||||
|
import 'package:yaml/yaml.dart'; |
||||||
|
import 'package:mustache_template/mustache_template.dart' as mustache; |
||||||
|
|
||||||
|
class MacroTemplateService { |
||||||
|
final Map<String, dynamic> _templates = {}; |
||||||
|
bool _inited = false; |
||||||
|
bool get inited => _inited; |
||||||
|
|
||||||
|
Future<void> init() async { |
||||||
|
try { |
||||||
|
final configString = await rootBundle.loadString('assets/config/uft_macro_list.yaml'); |
||||||
|
final yamlMap = loadYaml(configString); |
||||||
|
|
||||||
|
if (yamlMap is! YamlMap) { |
||||||
|
throw const FormatException('Invalid YAML structure: root should be a map'); |
||||||
|
} |
||||||
|
|
||||||
|
final templates = yamlMap['templates']; |
||||||
|
if (templates is YamlMap) { |
||||||
|
_templates.clear(); |
||||||
|
templates.forEach((key, value) { |
||||||
|
if (value is YamlMap) { |
||||||
|
_templates[key] = { |
||||||
|
'header': value['header']?.toString() ?? '', |
||||||
|
'body': value['body']?.toString() ?? '', |
||||||
|
'footer': value['footer']?.toString() ?? '', |
||||||
|
}; |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
_inited = true; |
||||||
|
} catch (e) { |
||||||
|
throw Exception('Failed to load macro templates: $e'); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Map<String, dynamic>? getTemplate(String templateName) { |
||||||
|
return _templates[templateName] is Map |
||||||
|
? Map<String, dynamic>.from(_templates[templateName]) |
||||||
|
: null; |
||||||
|
} |
||||||
|
|
||||||
|
String renderTemplate(List<String> templateList, Map<String, dynamic> context) { |
||||||
|
StringBuffer buffer = StringBuffer(); |
||||||
|
for (var templateName in templateList) { |
||||||
|
final template = getTemplate(templateName); |
||||||
|
if (template == null) throw Exception('Template $templateName not found'); |
||||||
|
|
||||||
|
buffer.writeln( |
||||||
|
[ |
||||||
|
_render(template['header'], context), |
||||||
|
_render(template['body'], context), |
||||||
|
_render(template['footer'], context), |
||||||
|
].join('\n'), |
||||||
|
); |
||||||
|
|
||||||
|
buffer.writeln(); |
||||||
|
} |
||||||
|
|
||||||
|
return buffer.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
String _render(String template, Map<String, dynamic> context) { |
||||||
|
final t = mustache.Template(template); |
||||||
|
return t.renderString(context); |
||||||
|
} |
||||||
|
} |
@ -1,51 +0,0 @@ |
|||||||
import 'package:flutter/services.dart' show rootBundle; |
|
||||||
import 'package:yaml/yaml.dart'; |
|
||||||
|
|
||||||
class UftMacroService { |
|
||||||
static final UftMacroService _instance = UftMacroService._internal(); |
|
||||||
factory UftMacroService() => _instance; |
|
||||||
UftMacroService._internal(); |
|
||||||
|
|
||||||
late Map<String, dynamic> _config; |
|
||||||
|
|
||||||
static bool _isInitialized = false; |
|
||||||
|
|
||||||
// 初始化配置 |
|
||||||
Future<void> init() async { |
|
||||||
if (_isInitialized) return; |
|
||||||
final configString = await rootBundle.loadString('assets/config/uft_macro_list.yaml'); |
|
||||||
final yamlMap = loadYaml(configString) as YamlMap; |
|
||||||
|
|
||||||
final globalConfig = yamlMap['global'] as YamlMap? ?? {}; |
|
||||||
|
|
||||||
_config = {...globalConfig}; |
|
||||||
|
|
||||||
_isInitialized = true; |
|
||||||
} |
|
||||||
|
|
||||||
// 获取配置值 |
|
||||||
dynamic get(String key) { |
|
||||||
return _config[key]; |
|
||||||
} |
|
||||||
|
|
||||||
// 获取字符串值 |
|
||||||
String getString(String key) { |
|
||||||
return _config[key] as String; |
|
||||||
} |
|
||||||
|
|
||||||
// 获取布尔值 |
|
||||||
bool getBool(String key) { |
|
||||||
return _config[key] as bool; |
|
||||||
} |
|
||||||
|
|
||||||
// 获取整数 |
|
||||||
int getInt(String key) { |
|
||||||
return _config[key] as int; |
|
||||||
} |
|
||||||
|
|
||||||
// 获取列表 |
|
||||||
List<T> getList<T>(String key) { |
|
||||||
final list = _config[key] as List; |
|
||||||
return list.cast<T>(); |
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue