getAllList(['status' => 1], ['*'], ['sort' => 'desc']); return dataReturn(0, 'success', makeTree($list)); } /** * 添加菜单 * @return array */ public function addMenu($param) { $validate = new AdminNodeValidate(); if (!$validate->check($param)) { throw new ApiException($validate->getError(), -1); } $adminNodeModel = new AdminNode(); // 检查唯一 if ($param['path'] != '#') { $has = $adminNodeModel->checkUnique([ 'path' => $param['path'], 'status' => 1 ]); if (!empty ($has)) { throw new ApiException('该菜单路由已经存在', -1); } } $param['create_time'] = now(); $param['update_time'] = now(); $isCurd = $param['is_curd'] ?? false; unset($param['is_curd']); if ($isCurd) { $curd = [ 'store' => [ 'name' => '增加', ], 'show' => [ 'name' => '详情', ], 'update' => [ 'name' => '编辑', ], 'destroy' => [ 'name' => '删除', ], ]; foreach ($curd as $method => &$value) { $value = array_merge($param, $value, [ 'is_menu' => 1, 'path' => $param['path'] . '/' . $method ]); } $core = $adminNodeModel->create($param); $core->children()->createMany(array_values($curd)); } else { $adminNodeModel->insertOne($param); } return dataReturn(0, '添加成功'); } /** * 编辑菜单 * @return array */ public function editMenu($param) { $validate = new AdminNodeValidate(); if (!$validate->check($param)) { throw new ApiException($validate->getError(), -1); } $adminNodeModel = new AdminNode(); // 检查唯一 $has = $adminNodeModel->checkUnique([ ['path', '=', $param['path']], ['id', '<>', $param['id']], ['path', '<>', '#'], ['status', '=', 1] ]); if (!empty ($has)) { throw new ApiException('该菜单路由已经存在', -1); } unset($param['is_curd']); $adminNodeModel->updateById($param, $param['id']); return dataReturn(0, '编辑成功'); } /** * 删除带单 * @param $id * @return array */ public function delMenu($id) { $adminNodeModel = new AdminNode(); $has = $adminNodeModel->getInfoByWhere(['pid' => $id]); if (!empty ($has)) { throw new ApiException('该菜单下有子菜单,不可删除', -1); } $adminNodeModel->delById($id); return dataReturn(0, '删除成功'); } /** * 获取超管的节点 * @return array */ public function getSuperAdminNode() { $adminNodeModel = new AdminNode(); return $adminNodeModel->getAllList([ 'status' => 1, 'is_menu' => 2, 'is_pc' => 1 ], ['id', 'name', 'path', 'pid', 'icon'], ['sort' => 'desc']); } /** * 获取超管的节点移动端 * @return array */ public function getSuperAdminNodeMobile() { $adminNodeModel = new AdminNode(); return $adminNodeModel->getAllList([ 'status' => 1, 'is_pc' => 2 ], ['id', 'name', 'path', 'pid', 'icon'], ['sort' => 'desc']); } /** * 获取用户的节点 * @return array */ public function getAdminNode() { $admin = admin(); $roles = $admin['roles']; // var_dump($admin); $roles = is_array($roles) ? $roles : [$roles]; $rules = AdminRole::whereIn('id', $roles)->pluck('role_node'); $rule_ids = []; foreach ($rules as $rule_string) { if (!$rule_string) { continue; } $rule_ids = array_merge($rule_ids, explode(',', $rule_string)); } $adminNodeModel = new AdminNode(); $node = $adminNodeModel->select(['id', 'name', 'path', 'pid', 'icon', 'sort'])->where([ 'is_menu' => 2, 'status' => 1, 'is_pc' => 1 ])->whereIn('id', $rule_ids) ->orderBy('sort', 'desc')->get(); return $node ? $node->toArray() : []; } /** * 获取移动端用户的节点 * @return array */ public function getAdminNodeMobile($roles = '') { // var_dump($roles); // if (!$roles) { // $roles = $roles; // } else { // $admin = admin(); // $roles = $admin['roles']; // } $roles = is_array($roles) ? $roles : [$roles]; $rules = AdminRole::whereIn('id', $roles)->pluck('role_node'); $rule_ids = []; foreach ($rules as $rule_string) { if (!$rule_string) { continue; } $rule_ids = array_merge($rule_ids, explode(',', $rule_string)); } $adminNodeModel = new AdminNode(); $node = $adminNodeModel->select(['id', 'name', 'path', 'pid', 'icon'])->where([ 'status' => 1, 'is_pc' => 2 ])->whereIn('id', $rule_ids) ->orderBy('sort', 'desc')->get(); return $node ? $node->toArray() : []; } /** * 获取菜单节点树 * @return array */ public function getNodeTree() { $adminNodeModel = new AdminNode(); $nodeList = $adminNodeModel->getAllList(['status' => 1], ['id', 'name', 'pid'], ['sort' => 'desc']); $tree = makeTree($nodeList); foreach ($tree as $key => $vo) { if ($vo['name'] == '首页') { $tree[$key]['disabled'] = true; foreach ($vo['child'] as $k => $v) { $tree[$key]['child'][$k]['disabled'] = true; } } } return $tree; } }