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

73 lines
2.0 KiB
PHP
Raw Normal View History

2024-09-19 11:32:39 +08:00
<?php
namespace admin\controllers;
use common\helpers\ArrayHelper;
use common\models\AuthRole;
use common\models\AuthRule;
use common\models\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;
$rule_id=AuthRole::find()->select(['rule_id'])->where(['role_id'=>$user->role])->column();
$AuthRule_path=AuthRule::find()->select(['path'])->where(['in','id',$rule_id])->andWhere(['type'=>1])->column();
$menu2s = Menu::find()->where(['in','menuUrl',$AuthRule_path])->andWhere(['hidden'=>0])->orderBy(['sort'=>SORT_ASC])->asArray()->all();
return ArrayHelper::itemsMerge($menu2s,0,'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 ['编辑成功'];
}
}