You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
5.1 KiB
188 lines
5.1 KiB
5 years ago
|
<?php
|
||
|
|
||
|
namespace frontend\controllers;
|
||
|
|
||
|
use common\libs\MyLib;
|
||
|
use common\models\MenuT;
|
||
|
use common\models\UserT;
|
||
|
use Yii;
|
||
|
use yii\web\Response;
|
||
|
|
||
|
class MenuController extends \yii\web\Controller
|
||
|
{
|
||
|
public $my = null;
|
||
|
|
||
|
public function init()
|
||
|
{
|
||
|
parent::init();
|
||
|
|
||
|
$cookie = Yii::$app->request->cookies;
|
||
|
$user_id = MyLib::encrypt($cookie->get('aid'),'DECODE');
|
||
|
|
||
|
if($user_id != 0)
|
||
|
{
|
||
|
$this->my = UserT::findOne(['id'=>$user_id]);
|
||
|
} else {
|
||
|
Yii::$app->response->redirect('/common/login')->send();
|
||
|
exit;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function actionIndex()
|
||
|
{
|
||
|
$menu_items = $this->my->getAllMenus();
|
||
|
return $this->render('index',[
|
||
|
'menu_items' => $menu_items
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
public function actionEdit()
|
||
|
{
|
||
|
$request = Yii::$app->request;
|
||
|
$id = $request->get('id',0);
|
||
|
if($id > 0) {
|
||
|
$info = MenuT::findOne(['id'=>$id]);
|
||
|
} else {
|
||
|
$info = new MenuT();
|
||
|
$row = MenuT::find()
|
||
|
->orderBy('order_id DESC')
|
||
|
->one();
|
||
|
$order_id = 1;
|
||
|
if(isset($row)) {
|
||
|
$order_id = $row->order_id + 1;
|
||
|
}
|
||
|
$info->order_id = $order_id;
|
||
|
}
|
||
|
|
||
|
$menu_items = MenuT::find()
|
||
|
->where('parent_id=0')
|
||
|
->orderby('order_id ASC')
|
||
|
->all();
|
||
|
|
||
|
return $this->render('edit',[
|
||
|
'info' => $info,
|
||
|
'menu_items' => $menu_items
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
public function actionSave()
|
||
|
{
|
||
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
||
|
$request = Yii::$app->request;
|
||
|
$result = array();
|
||
|
$result['success'] = false;
|
||
|
$result['msg'] = '保存失败';
|
||
|
|
||
|
if($request->isPost) {
|
||
|
$id = $request->post('id',0);
|
||
|
$name = $request->post('name');
|
||
|
$parent_id = $request->post('parent_id');
|
||
|
$path = $request->post('path');
|
||
|
$order_id = $request->post('order_id');
|
||
|
|
||
|
if($name == '') {
|
||
|
$result['msg'] = '请输入菜单名称!';
|
||
|
return $result;
|
||
|
}
|
||
|
if($id > 0 && $parent_id == $id) {
|
||
|
$result['msg'] = '上级菜单不能选择自己!';
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
$tran = MenuT::getDb()->beginTransaction();
|
||
|
|
||
|
try {
|
||
|
$row = null;
|
||
|
if($id > 0) {
|
||
|
$row = MenuT::findOne(['id'=>$id]);
|
||
|
} else {
|
||
|
$row = new MenuT();
|
||
|
}
|
||
|
$row->name = $name;
|
||
|
$row->path = $path;
|
||
|
$row->parent_id = $parent_id;
|
||
|
$row->order_id = $order_id;
|
||
|
$row->save();
|
||
|
|
||
|
$tran->commit();
|
||
|
} catch(\Exception $e) {
|
||
|
$tran->rollBack();
|
||
|
throw $e;
|
||
|
}
|
||
|
|
||
|
$result['success'] = true;
|
||
|
$result['msg'] = '保存成功';
|
||
|
}
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function actionDelete()
|
||
|
{
|
||
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
||
|
$request = Yii::$app->request;
|
||
|
$result = array();
|
||
|
$result['success'] = false;
|
||
|
$result['msg'] = '删除失败';
|
||
|
|
||
|
if($request->isPost) {
|
||
|
$id = $request->post('id',0);
|
||
|
|
||
|
if($id > 0) {
|
||
|
$tran = MenuT::getDb()->beginTransaction();
|
||
|
try {
|
||
|
$row = MenuT::findOne(['id'=>$id]);
|
||
|
$children = $row->menus;
|
||
|
if(count($children) > 0) {
|
||
|
$result['msg'] = '请先删除子菜单';
|
||
|
return $result;
|
||
|
}
|
||
|
$row->unlinkAll('groups',true);
|
||
|
$row->delete();
|
||
|
|
||
|
$result['success'] = true;
|
||
|
$result['msg'] = '删除成功';
|
||
|
|
||
|
$tran->commit();
|
||
|
} catch(\Exception $e) {
|
||
|
$tran->rollBack();
|
||
|
throw $e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function actionOrderSave()
|
||
|
{
|
||
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
||
|
$request = Yii::$app->request;
|
||
|
$result = array();
|
||
|
$result['success'] = false;
|
||
|
$result['msg'] = '保存失败';
|
||
|
|
||
|
if($request->isPost) {
|
||
|
$order_ids = $request->post('order_ids',array());
|
||
|
|
||
|
$tran = MenuT::getDb()->beginTransaction();
|
||
|
|
||
|
try {
|
||
|
foreach($order_ids as $id=>$order_id) {
|
||
|
$row = MenuT::findOne(['id'=>$id]);
|
||
|
if($row) {
|
||
|
$row->order_id = $order_id;
|
||
|
$row->save();
|
||
|
}
|
||
|
}
|
||
|
$result['success'] = true;
|
||
|
$result['msg'] = '保存成功';
|
||
|
|
||
|
$tran->commit();
|
||
|
} catch(\Exception $e) {
|
||
|
$tran->rollBack();
|
||
|
throw $e;
|
||
|
}
|
||
|
}
|
||
|
return $result;
|
||
|
}
|
||
|
}
|