*/ class MenuController extends AuthController { public $modelClass = Menu::class; /** * @doc-name 菜单列表 */ public function actionList() { // 获取当前用户的身份信息 $user=\Yii::$app->user->identity; // 查询并获取当前用户角色对应的权限ID列表 $rule_id=AuthRole::find()->select(['rule_id'])->where(['role_id'=>$user->role])->column(); // 根据权限ID列表查询对应的路径,并筛选出类型为1(例如:菜单项)的路径列表 $AuthRule_path=AuthRule::find()->select(['path'])->where(['in','id',$rule_id])->andWhere(['type'=>1])->column(); // 查询并获取菜单URL在权限路径列表中且未隐藏的菜单项,并按排序字段升序排列 $menu2s = Menu::find()->where(['in','menuUrl',$AuthRule_path])->andWhere(['hidden'=>0])->orderBy(['sort'=>SORT_ASC])->asArray()->all(); // 使用ArrayHelper的itemsMerge方法对菜单项进行处理,以生成所需的菜单结构 return ArrayHelper::itemsMerge($menu2s,'','menuUrl','parentPath','children'); } 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 ['编辑成功']; } }