93 lines
2.2 KiB
PHP
93 lines
2.2 KiB
PHP
<?php
|
|
/**
|
|
* 商业使用授权协议
|
|
*
|
|
* Copyright (c) 2025 [云泽网]. 保留所有权利.
|
|
*
|
|
* 本软件仅供评估使用。任何商业用途必须获得书面授权许可。
|
|
* 未经授权商业使用本软件属于侵权行为,将承担法律责任。
|
|
*
|
|
* 授权购买请联系: 357099073@qq.com
|
|
* 官方网站: https://www.yunzer.cn
|
|
*
|
|
* 评估用户须知:
|
|
* 1. 禁止移除版权声明
|
|
* 2. 禁止用于生产环境
|
|
* 3. 禁止转售或分发
|
|
*/
|
|
|
|
namespace app\admin\model;
|
|
|
|
use think\Model;
|
|
|
|
class AdminSysMenu extends Model
|
|
{
|
|
// 设置表名
|
|
protected $name = 'menu';
|
|
|
|
// 设置主键
|
|
protected $pk = 'smid';
|
|
|
|
// 自动时间戳
|
|
protected $autoWriteTimestamp = true;
|
|
protected $createTime = 'create_time';
|
|
protected $updateTime = 'update_time';
|
|
|
|
// 字段类型转换
|
|
protected $type = [
|
|
'smid' => 'integer',
|
|
'parent_id' => 'integer',
|
|
'type' => 'integer',
|
|
'sort' => 'integer',
|
|
'status' => 'integer',
|
|
'create_time' => 'integer',
|
|
'update_time' => 'integer'
|
|
];
|
|
|
|
// 允许写入的字段
|
|
protected $allowField = [
|
|
'parent_id', 'type', 'label', 'icon_class', 'sort', 'src', 'status'
|
|
];
|
|
|
|
/**
|
|
* 获取菜单树形结构
|
|
* @return array
|
|
*/
|
|
public static function getMenuTree()
|
|
{
|
|
// 获取所有启用的菜单
|
|
$menus = self::where('status', 1)
|
|
->order('sort', 'desc')
|
|
->order('smid', 'asc')
|
|
->select()
|
|
->toArray();
|
|
|
|
return self::buildTree($menus);
|
|
}
|
|
|
|
/**
|
|
* 构建树形结构
|
|
* @param array $menus 菜单数组
|
|
* @param int $parent_id 父级ID
|
|
* @return array
|
|
*/
|
|
public static function buildTree($menus, $parent_id = 0)
|
|
{
|
|
$tree = [];
|
|
|
|
foreach ($menus as $menu) {
|
|
if ($menu['parent_id'] == $parent_id) {
|
|
$children = self::buildTree($menus, $menu['smid']);
|
|
if ($children) {
|
|
$menu['children'] = $children;
|
|
} else {
|
|
$menu['children'] = [];
|
|
}
|
|
$tree[] = $menu;
|
|
}
|
|
}
|
|
|
|
return $tree;
|
|
}
|
|
}
|