QinGang_interested/lib/common/route_model.dart

108 lines
3.0 KiB
Dart
Raw Normal View History

2025-12-12 09:11:30 +08:00
// models/route_model.dart
import 'dart:convert';
class RouteModel {
2026-03-06 16:15:20 +08:00
// 映射到后端新字段
final String id;
final String menuName; // 原来的 title
final String menuUrl; // 原来的 path
2025-12-12 09:11:30 +08:00
final String parentId;
final String parentIds;
2026-03-06 16:15:20 +08:00
final String menuPerms;
final int menuType; // 1/2 ...
final String menuAttribution;
final int sort;
final int showFlag; // 1 可见0 隐藏
final Map<String, dynamic> extValues;
final List<RouteModel> children;
2025-12-12 09:11:30 +08:00
RouteModel({
2026-03-06 16:15:20 +08:00
required this.id,
required this.menuName,
required this.menuUrl,
2025-12-12 09:11:30 +08:00
required this.parentId,
required this.parentIds,
2026-03-06 16:15:20 +08:00
required this.menuPerms,
required this.menuType,
required this.menuAttribution,
required this.sort,
required this.showFlag,
required this.extValues,
required this.children,
2025-12-12 09:11:30 +08:00
});
factory RouteModel.fromJson(Map<String, dynamic> json) {
2026-03-06 16:15:20 +08:00
// 安全解析工具
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 = <RouteModel>[];
if (rawChildren is List) {
for (final c in rawChildren) {
if (c is Map<String, dynamic>) {
children.add(RouteModel.fromJson(c));
} else if (c is Map) {
children.add(RouteModel.fromJson(Map<String, dynamic>.from(c)));
}
}
}
// extValues 兼容
Map<String, dynamic> ext = {};
if (json['extValues'] is Map) {
ext = Map<String, dynamic>.from(json['extValues']);
}
2025-12-12 09:11:30 +08:00
return RouteModel(
2026-03-06 16:15:20 +08:00
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,
2025-12-12 09:11:30 +08:00
);
}
2026-03-06 16:15:20 +08:00
Map<String, dynamic> 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;
/// 叶子节点判定(无子节点)
2025-12-12 09:11:30 +08:00
bool get isLeaf => children.isEmpty;
2026-03-06 16:15:20 +08:00
/// 页面标题:优先 menuName如果 extValues 中含 title 则优先使用
String get title {
if (menuName.isNotEmpty) return menuName;
if (extValues.containsKey('title')) return extValues['title']?.toString() ?? '';
return '';
}
2025-12-12 09:11:30 +08:00
}