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

78 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace admin\controllers;
use common\helpers\ArrayHelper;
use common\models\oldDb\AuthRole;
use common\models\oldDb\AuthRule;
use common\models\oldDb\Menu;
use yii\base\Exception;
/**
* 后台菜单控制器
* @author longfei <phphome@qq.com>
*/
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 ['编辑成功'];
}
}