mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-01 15:32:48 +08:00
109 lines
3.3 KiB
PHP
109 lines
3.3 KiB
PHP
<?php
|
||
|
||
// +----------------------------------------------------------------------
|
||
// | EasyAdmin
|
||
// +----------------------------------------------------------------------
|
||
// | PHP交流群: 763822524
|
||
// +----------------------------------------------------------------------
|
||
// | 开源协议 https://mit-license.org
|
||
// +----------------------------------------------------------------------
|
||
// | github开源项目:https://github.com/zhongshaofa/EasyAdmin
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace app\common\service;
|
||
|
||
use app\common\constants\MenuConstant;
|
||
use think\facade\Db;
|
||
|
||
class MenuService
|
||
{
|
||
|
||
/**
|
||
* 管理员ID
|
||
* @var integer
|
||
*/
|
||
protected $adminId;
|
||
|
||
public function __construct($adminId)
|
||
{
|
||
$this->adminId = $adminId;
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 获取首页信息
|
||
* @return array|\think\Model|null
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getHomeInfo()
|
||
{
|
||
$data = Db::name('system_menu')
|
||
->field('title,icon,href')
|
||
->where("delete_time", 0)
|
||
->where('pid', MenuConstant::HOME_PID)
|
||
->find();
|
||
!empty($data) && $data['href'] = __url($data['href']);
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 获取后台菜单树信息
|
||
* @return mixed
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getMenuTree()
|
||
{
|
||
/** @var AuthService $authService */
|
||
$authServer = app(AuthService::class, ['adminId' => $this->adminId]);
|
||
return $this->buildMenuChild(0, $this->getMenuData(), $authServer);
|
||
}
|
||
|
||
private function buildMenuChild($pid, $menuList, AuthService $authServer)
|
||
{
|
||
$treeList = [];
|
||
foreach ($menuList as &$v) {
|
||
$check = empty($v['href']) ? true : $authServer->checkNode($v['href']);
|
||
!empty($v['href']) && $v['href'] = __url($v['href']);
|
||
if ($pid == $v['pid'] && $check) {
|
||
$node = $v;
|
||
$child = $this->buildMenuChild($v['id'], $menuList, $authServer);
|
||
if (!empty($child)) {
|
||
$node['child'] = $child;
|
||
}
|
||
if (!empty($v['href']) || !empty($child)) {
|
||
$treeList[] = $node;
|
||
}
|
||
}
|
||
}
|
||
return $treeList;
|
||
}
|
||
|
||
/**
|
||
* 获取所有菜单数据
|
||
* @return \think\Collection
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
protected function getMenuData()
|
||
{
|
||
$menuData = Db::name('system_menu')
|
||
->field('id,pid,title,icon,href,target')
|
||
->where("delete_time", 0)
|
||
->where([
|
||
['status', '=', '1'],
|
||
['pid', '<>', MenuConstant::HOME_PID],
|
||
])
|
||
->order([
|
||
'sort' => 'desc',
|
||
'id' => 'asc',
|
||
])
|
||
->select();
|
||
return $menuData;
|
||
}
|
||
}
|