TaoLer/app/admin/controller/Menu.php

67 lines
2.0 KiB
PHP
Raw Normal View History

2020-01-01 13:17:19 +08:00
<?php
namespace app\admin\controller;
use app\common\controller\AdminController;
2022-11-25 22:44:04 +08:00
use think\facade\Db;
2023-03-16 22:30:36 +08:00
use taoser\think\Auth;
2020-01-01 13:17:19 +08:00
class Menu extends AdminController
{
public function index(){
return view();
}
2022-11-25 22:44:04 +08:00
/**
* 动态菜单并排序
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getMenuNavbar()
{
2023-03-16 22:30:36 +08:00
// 用户菜单权限
$auth = new Auth();
2022-11-25 22:44:04 +08:00
$pid = empty(input('id')) ? 0 : input('id');
2023-03-16 22:30:36 +08:00
$data = Db::name('auth_rule')->field('id,title,icon,name,sort')->where(['pid'=>$pid,'status'=> 1, 'ismenu'=>1, 'delete_time'=> 0])->select();
2022-11-25 22:44:04 +08:00
$tree = [];
foreach ($data as $k => $v) {
$hasChild = $this->hasChildren($v['id']);
if($hasChild) {
$v['hasChildren'] = 1;
} else {
$v['hasChildren'] = 0;
}
2023-03-16 22:30:36 +08:00
if ($auth->check($v['name'], session('admin_id')) || session('admin_id') == 1) {
$tree[] = ['id'=>$v['id'],'text'=>$v['title'],'icon'=>$v['icon'],'hasChildren'=>$v['hasChildren'],'href'=>(string) url($v['name']),'sort'=>$v['sort']];
}
2022-11-25 22:44:04 +08:00
}
// 排序
$cmf_arr = array_column($tree, 'sort');
array_multisort($cmf_arr, SORT_ASC, $tree);
return json($tree);
}
/**
* 是否有子菜单
* @param $pid
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function hasChildren($pid)
{
$data = Db::name('auth_rule')->field('pid')->where(['delete_time'=> 0,'status'=> 1,'ismenu'=>1,'pid'=>$pid])->select()->toArray();
if(count($data)) {
return true;
} else {
return false;
}
}
2020-01-01 13:17:19 +08:00
}