Files
xk-api-yii/admin/controllers/MenuController.php

78 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2024-09-19 11:32:39 +08:00
<?php
namespace admin\controllers;
use common\helpers\ArrayHelper;
2024-12-19 19:20:27 +08:00
use common\models\oldDb\AuthRole;
use common\models\oldDb\AuthRule;
use common\models\oldDb\Menu;
2024-09-19 11:32:39 +08:00
use yii\base\Exception;
/**
* 后台菜单控制器
* @author longfei <phphome@qq.com>
*/
class MenuController extends AuthController
{
public $modelClass = Menu::class;
/**
* @doc-name 菜单列表
*/
public function actionList()
{
2024-12-12 13:45:41 +08:00
// 获取当前用户的身份信息
2024-09-19 11:32:39 +08:00
$user=\Yii::$app->user->identity;
2024-12-12 13:45:41 +08:00
// 查询并获取当前用户角色对应的权限ID列表
2024-09-19 11:32:39 +08:00
$rule_id=AuthRole::find()->select(['rule_id'])->where(['role_id'=>$user->role])->column();
2024-12-12 13:45:41 +08:00
// 根据权限ID列表查询对应的路径并筛选出类型为1例如菜单项的路径列表
2024-09-19 11:32:39 +08:00
$AuthRule_path=AuthRule::find()->select(['path'])->where(['in','id',$rule_id])->andWhere(['type'=>1])->column();
2024-12-12 13:45:41 +08:00
// 查询并获取菜单URL在权限路径列表中且未隐藏的菜单项并按排序字段升序排列
2024-09-19 11:32:39 +08:00
$menu2s = Menu::find()->where(['in','menuUrl',$AuthRule_path])->andWhere(['hidden'=>0])->orderBy(['sort'=>SORT_ASC])->asArray()->all();
2024-12-12 13:45:41 +08:00
// 使用ArrayHelper的itemsMerge方法对菜单项进行处理以生成所需的菜单结构
2024-09-23 10:27:52 +08:00
return ArrayHelper::itemsMerge($menu2s,'','menuUrl','parentPath','children');
2024-09-19 11:32:39 +08:00
}
public function actionSave(){
$menuUrl = $this->post('menuUrl');
$model = Menu::findOne($menuUrl);
if(!$model){
$model = new Menu();
}
$model->load($this->post());
if( $model->load($this->post(),'') && $model->save()){
return $model->getAttributes();
}else{
throw new Exception("参数不对");
}
}
public function actionDel(){
$menuUrl = $this->post('menuUrl');
$model = Menu::findOne($menuUrl);
if (!$model) throw new \yii\db\Exception('菜单不存在');
if($model->delete()){
Menu::updateAll(['parentPath'=>''],['parentPath'=>$menuUrl]);
}
return ['删除成功'];
}
/**
* @doc-name 编辑菜单
*/
public function actionEdit()
{
$data= $this->post();
$menuUrl = $this->post('menuUrl');
$model = Menu::findOne($menuUrl);
if (!$model) throw new \yii\db\Exception('菜单不存在');
$model->attributes=$data;
$model->saveOrFail();
return ['编辑成功'];
}
}