chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\backend\model;
|
||||
|
||||
use think\model\concern\SoftDelete;
|
||||
use ywxapp\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 前台主导航菜单模型(支持两级下拉).
|
||||
*/
|
||||
class NavMenu extends BaseModel
|
||||
{
|
||||
use SoftDelete;
|
||||
|
||||
protected $deleteTime = 'delete_at';
|
||||
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'strict' => true,
|
||||
'name' => 'common_nav_menu',
|
||||
'autoWriteTimestamp' => 'int',
|
||||
'createTime' => 'create_at',
|
||||
'updateTime' => 'update_at',
|
||||
'defaultSoftDelete' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行时自愈:确保 common_nav_menu 主表存在(install.sql 为事实源).
|
||||
*/
|
||||
public static function ensureSchema(): void
|
||||
{
|
||||
BaseModel::ensureTableFromInstall(BaseModel::currentPrefix(), 'common_nav_menu');
|
||||
}
|
||||
|
||||
protected function initialize()
|
||||
{
|
||||
parent::initialize();
|
||||
self::ensureSchema();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取启用的导航树(两级:父 + 子)。
|
||||
* @return array [{id,title,url,icon,child:[...]}]
|
||||
*/
|
||||
public static function getNavTree(): array
|
||||
{
|
||||
$list = self::where('status', 1)
|
||||
->where('delete_at', 0)
|
||||
->order('sort', 'asc')
|
||||
->order('id', 'asc')
|
||||
->field('id,parent_id,title,url,icon,sort')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$parents = [];
|
||||
$childrenMap = [];
|
||||
foreach ($list as $item) {
|
||||
$item['ctrl'] = strtolower(strtok($item['url'], '/') ?: '');
|
||||
if ((int) $item['parent_id'] === 0) {
|
||||
$item['child'] = [];
|
||||
$parents[] = $item;
|
||||
} else {
|
||||
$childrenMap[$item['parent_id']][] = $item;
|
||||
}
|
||||
}
|
||||
foreach ($parents as &$p) {
|
||||
if (isset($childrenMap[$p['id']])) {
|
||||
$p['child'] = $childrenMap[$p['id']];
|
||||
}
|
||||
}
|
||||
return $parents;
|
||||
}
|
||||
|
||||
/**
|
||||
* 后台列表:返回两级扁平树(含隐藏项).
|
||||
*/
|
||||
public static function getAdminTree(): array
|
||||
{
|
||||
$list = self::where('delete_at', 0)
|
||||
->order('sort', 'asc')
|
||||
->order('id', 'asc')
|
||||
->field('id,parent_id,title,url,icon,sort,status')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$parents = [];
|
||||
$childrenMap = [];
|
||||
foreach ($list as $item) {
|
||||
$item['ctrl'] = strtolower(strtok($item['url'], '/') ?: '');
|
||||
if ((int) $item['parent_id'] === 0) {
|
||||
$item['child'] = [];
|
||||
$parents[] = $item;
|
||||
} else {
|
||||
$childrenMap[$item['parent_id']][] = $item;
|
||||
}
|
||||
}
|
||||
foreach ($parents as &$p) {
|
||||
if (isset($childrenMap[$p['id']])) {
|
||||
$p['child'] = $childrenMap[$p['id']];
|
||||
}
|
||||
}
|
||||
return $parents;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user