// models/route_model.dart import 'dart:convert'; class RouteModel { // 映射到后端新字段 final String id; final String menuName; // 原来的 title final String menuUrl; // 原来的 path final String parentId; final String parentIds; final String menuPerms; final int menuType; // 1/2 ... final String menuAttribution; final int sort; final int showFlag; // 1 可见,0 隐藏 final Map extValues; final List children; RouteModel({ required this.id, required this.menuName, required this.menuUrl, required this.parentId, required this.parentIds, required this.menuPerms, required this.menuType, required this.menuAttribution, required this.sort, required this.showFlag, required this.extValues, required this.children, }); factory RouteModel.fromJson(Map json) { // 安全解析工具 String _s(dynamic v) => v == null ? '' : v.toString(); int _i(dynamic v) { if (v == null) return 0; if (v is int) return v; return int.tryParse(v.toString()) ?? 0; } final rawChildren = json['children']; final children = []; if (rawChildren is List) { for (final c in rawChildren) { if (c is Map) { children.add(RouteModel.fromJson(c)); } else if (c is Map) { children.add(RouteModel.fromJson(Map.from(c))); } } } // extValues 兼容 Map ext = {}; if (json['extValues'] is Map) { ext = Map.from(json['extValues']); } return RouteModel( id: _s(json['id']), menuName: _s(json['menuName']), menuUrl: _s(json['menuUrl']), parentId: _s(json['parentId']), parentIds: _s(json['parentIds']), menuPerms: _s(json['menuPerms']), menuType: _i(json['menuType']), menuAttribution: _s(json['menuAttribution']), sort: _i(json['sort']), showFlag: _i(json['showFlag']), extValues: ext, children: children, ); } Map toJson() => { 'id': id, 'menuName': menuName, 'menuUrl': menuUrl, 'parentId': parentId, 'parentIds': parentIds, 'menuPerms': menuPerms, 'menuType': menuType, 'menuAttribution': menuAttribution, 'sort': sort, 'showFlag': showFlag, 'extValues': extValues, 'children': children.map((c) => c.toJson()).toList(), }; /// 是否可见(供显示逻辑判断) bool get visible => showFlag == 1; /// 是否是菜单项(接口好像用 menuType 表示层级/类型) bool get isMenu => menuType == 2; /// 叶子节点判定(无子节点) bool get isLeaf => children.isEmpty; /// 页面标题:优先 menuName,如果 extValues 中含 title 则优先使用 String get title { if (menuName.isNotEmpty) return menuName; if (extValues.containsKey('title')) return extValues['title']?.toString() ?? ''; return ''; } }