add
This commit is contained in:
103
app/admin/model/SysMenu.php
Normal file
103
app/admin/model/SysMenu.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use ba\Exception;
|
||||
use think\facade\Db;
|
||||
use think\model;
|
||||
|
||||
/**
|
||||
* 菜单模型
|
||||
*/
|
||||
class SysMenu extends model
|
||||
{
|
||||
// 菜单类型常量
|
||||
const MENU_TYPE_CATALOG = 0; // 目录
|
||||
const MENU_TYPE_MENU = 1; // 菜单
|
||||
const MENU_TYPE_BUTTON = 2; // 按钮
|
||||
|
||||
public static function getMenuList($menu):array
|
||||
{
|
||||
$one_menu = [];
|
||||
$t_menu = [];
|
||||
$p_list = [];
|
||||
foreach ($menu as $k => $v) {
|
||||
if(in_array($v['type'], [0, 1])) {
|
||||
if($v['parent_id'] == 0) {
|
||||
$one_menu[] = $v;
|
||||
}else {
|
||||
$t_menu[] = $v;
|
||||
}
|
||||
}
|
||||
// 权限处理
|
||||
if($v['perms'] ) {
|
||||
if(strpos($v['perms'], ',') !== false) {
|
||||
foreach (explode(',', $v['perms']) as $item) {
|
||||
$p_list[] = $item;
|
||||
}
|
||||
}else {
|
||||
$p_list[] = $v['perms'];
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($one_menu as $k => &$one_value) {
|
||||
foreach ($t_menu as $two_k => $two_value) {
|
||||
if($two_value['parent_id'] == $one_value['menu_id']) {
|
||||
$one_value['list'][] = convertToCamelCase($two_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
$return['menuList'] = convertToCamelCase($one_menu);
|
||||
$return['permissions'] = $p_list;
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static function verifyForm($menu)
|
||||
{
|
||||
// 验证菜单名称
|
||||
if (empty($menu['name'])) {
|
||||
throw new Exception("菜单名称不能为空");
|
||||
}
|
||||
|
||||
// 验证上级菜单
|
||||
if (!isset($menu['parentId'])) {
|
||||
throw new Exception("上级菜单不能为空");
|
||||
}
|
||||
|
||||
// 菜单类型为菜单时,验证 URL
|
||||
if ($menu['type'] == self::MENU_TYPE_MENU) {
|
||||
if (empty($menu['url'])) {
|
||||
throw new Exception("菜单URL不能为空");
|
||||
}
|
||||
}
|
||||
|
||||
// 获取上级菜单类型
|
||||
$parentType = self::MENU_TYPE_CATALOG;
|
||||
if ($menu['parentId'] != 0) {
|
||||
$parentMenu = Db::name('sys_menu')->where(['menu_id' => $menu['parentId']])->find();
|
||||
if (!$parentMenu) {
|
||||
throw new Exception("上级菜单不存在");
|
||||
}
|
||||
$parentType = $parentMenu['type'];
|
||||
}
|
||||
|
||||
// 目录、菜单类型验证
|
||||
if ($menu['type'] == self::MENU_TYPE_CATALOG || $menu['type'] == self::MENU_TYPE_MENU) {
|
||||
if ($parentType != self::MENU_TYPE_CATALOG) {
|
||||
throw new Exception("上级菜单只能为目录类型");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 按钮类型验证
|
||||
if ($menu['type'] == self::MENU_TYPE_BUTTON) {
|
||||
if ($parentType != self::MENU_TYPE_MENU) {
|
||||
throw new Exception("上级菜单只能为菜单类型");
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user