QinGang_interested/lib/common/route_model.dart

108 lines
3.0 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 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<String, dynamic> extValues;
final List<RouteModel> 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<String, dynamic> 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 = <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']);
}
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<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;
/// 叶子节点判定(无子节点)
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 '';
}
}