FrontendController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace backend\controllers;
  3. use backend\base\CommonController;
  4. use backend\base\Help;
  5. use common\models\Frontend;
  6. use backend\server\UploadFile;
  7. use Yii;
  8. use yii\helpers\Html;
  9. class FrontendController extends CommonController
  10. {
  11. public $enableCsrfValidation = false;
  12. /*
  13. * 菜单数据显示
  14. * */
  15. public function actionIndex()
  16. {
  17. return $this->render('index');
  18. }
  19. /*
  20. * 获取菜单数据
  21. * */
  22. public function actionIndexfrom()
  23. {
  24. $input = Yii::$app->request->post();
  25. $model = new \common\models\Frontend();
  26. $rows = $model->getList($input,['id','name','icon','url','pid','status','function','sort']);
  27. if($rows)
  28. {
  29. $imgUrl = Yii::$app->params['httpImg']['host'].Yii::$app->params['httpImg']['city'];
  30. foreach ($rows as &$val)
  31. {
  32. $val['icon'] = $imgUrl.$val['icon'];
  33. }
  34. return Help::JsonData(0,'成功',$model->getListTotal(),$rows);
  35. }
  36. return Help::JsonCode(Help::ERROR,Yii::t('app','get_error'));
  37. }
  38. /*
  39. * 父类添加
  40. */
  41. public function actionParentadd()
  42. {
  43. return $this->render('parentadd');
  44. }
  45. /*
  46. * 添加子菜单页面
  47. * */
  48. public function actionCreate()
  49. {
  50. return $this->render('create',['model'=>Yii::$app->request->get()]);
  51. }
  52. /*
  53. *添加菜单
  54. * */
  55. public function actionCreatefrom()
  56. {
  57. $model = new \common\models\Frontend();
  58. $input = Yii::$app->request->post();
  59. $url = Yii::$app->params['img_url']['city'];
  60. $img = UploadFile::InstanceImgName('img',$url);
  61. if(is_string($img))
  62. {
  63. $input['icon'] = $img;
  64. }
  65. if($model->load($input,'') && $model->save())
  66. {
  67. return Help::JsonCode(Help::SUCCESS,'菜单添加成功');
  68. }
  69. return Help::JsonCode(Help::ERROR,'菜单添加失败',$model->errors);
  70. }
  71. /*
  72. * 修改菜单
  73. * */
  74. public function actionEditfrom()
  75. {
  76. $input = Yii::$app->request->post();
  77. $model = new \common\models\Frontend();
  78. $row = $model->FindById($input['id']);
  79. if($row)
  80. {
  81. $url = Yii::$app->params['img_url']['city'];
  82. $img = UploadFile::InstanceImgName('img',$url);
  83. if(is_string($img))
  84. {
  85. UploadFile::delImg($url,$row->icon);
  86. $input['icon'] = $img;
  87. }
  88. if($row->load($input,'') && $row->save())
  89. {
  90. return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
  91. }
  92. }
  93. return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'),$row->errors);
  94. }
  95. /*
  96. * 修改菜单页面
  97. * */
  98. public function actionEdit()
  99. {
  100. $model = new \common\models\Frontend();
  101. $result = $model->FindById(Yii::$app->request->get('id'));
  102. if($result)
  103. {
  104. if($result['pid'] == 0)
  105. {
  106. return $this->render('parentedit',['model'=>$result]);
  107. }
  108. $result['function'] = Html::encode($result['function']);
  109. return $this->render('edit',['model'=>$result]);
  110. }
  111. return $this->goBack();
  112. }
  113. /*
  114. * 修改排序
  115. * */
  116. public function actionFrontendmenusortedit()
  117. {
  118. $model = new Frontend();
  119. $input = Yii::$app->request->post();
  120. $row = $model->GetFindById($input['id']);
  121. if (!empty($row))
  122. {
  123. switch ($input['type'])
  124. {
  125. case 'status':
  126. if($row->status == 1)
  127. {
  128. $row->status = 2;
  129. }else if($row->status == 2)
  130. {
  131. $row->status = 1;
  132. }
  133. break;
  134. case 'del':
  135. $row->del = 2;
  136. break;
  137. case 'sort':
  138. $row->sort = $input['sort'];
  139. break;
  140. }
  141. if($row->save(false)) return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
  142. }
  143. return Help::JsonCode(Help::ERROR,'操作失败');
  144. }
  145. //生成代码 用于显示和隐藏或个HTML模块
  146. public function actionProduce()
  147. {
  148. $input = Yii::$app->request->get();
  149. if(!empty($input['id']))
  150. {
  151. $find = \common\models\Frontend::findOne($input['id']);
  152. if($find)
  153. {
  154. $str = '<?php modules_show('.$find['id'].') ?>';
  155. }
  156. }
  157. return $this->render('produce',['str'=>$str]);
  158. }
  159. }