Frontendmenu.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace backend\server;
  3. use backend\base\Help;
  4. use Yii;
  5. use common\models\Frontend;
  6. use backend\server\UploadFile;
  7. class Frontendmenu
  8. {
  9. public $status = [1=>'正常',2=>'禁用'];
  10. public function Create()
  11. {
  12. $models = new Frontend();
  13. //$models->scenario = 'add';
  14. $model = $models->Authenticator(Yii::$app->request->post());
  15. if(!empty($model->pid))
  16. {
  17. $model->id = $model->pid;
  18. $row = $model->FindById();
  19. $model->path = $row->path.$row->id.',';
  20. unset($model['id']);
  21. }else{
  22. $model->id = $model->pid;
  23. $model->path = '0'.',';
  24. }
  25. if(is_object($model))
  26. {
  27. $url = Yii::$app->params['img_url']['city'];
  28. $icon = UploadFile::InstanceImgName('img',$url);
  29. if (is_string($icon))
  30. {
  31. $model->icon = $icon;
  32. }
  33. if($model->insert() == true) return true;
  34. }
  35. return $model;
  36. }
  37. public function Edit()
  38. {
  39. /*$model = new Frontend();
  40. $model = $model->Authenticator(Yii::$app->request->post());
  41. if(is_object($model))
  42. {
  43. $model->id = Yii::$app->request->post('data')['id'];
  44. $row = $model->FindById();
  45. if(!empty($row))
  46. {
  47. $row = Help::SetAttr(Yii::$app->request->post(),$model,$row);
  48. $url = Yii::$app->params['img_url']['city'];
  49. $img = UploadFile::InstanceImgName('img',$url);
  50. if(is_string($img))
  51. {
  52. UploadFile::delImg($url,$row->icon);
  53. $row->icon = $img;
  54. $row->name = $model->name;
  55. $row->url = $model->url;
  56. $row->sort = $model->sort;
  57. $row->status = $model->status;
  58. }
  59. }
  60. if($row->save(false)) return true;
  61. }
  62. return $model;*/
  63. $model = new Frontend();
  64. $input = Yii::$app->request->post();
  65. $setAttribute = $model->Authenticator($input);
  66. if(is_object($setAttribute))
  67. {
  68. $model->id = $input['id'];
  69. $row = $model->FindById();
  70. if($row != null)
  71. {
  72. $row = Help::SetAttr($input,$model,$row);
  73. $url = Yii::$app->params['img_url']['city'];
  74. $img = UploadFile::InstanceImgName('img',$url);
  75. if(is_string($img))
  76. {
  77. UploadFile::delImg($url,$row->icon);
  78. $row->icon = $img;
  79. }
  80. if($row->save(false)) return true;
  81. }
  82. // return true;
  83. }
  84. return false;
  85. }
  86. public function getchildren()
  87. {
  88. $model = new Frontend();
  89. $modela = new Checkauthority();
  90. $arrId = $modela->MenuAccess();
  91. if(Yii::$app->session['user_info']['user'] != Yii::$app->params['UserSuperadministrator'])
  92. {
  93. $model->id = $arrId;
  94. }
  95. $model->pid = Yii::$app->request->post('pid');
  96. $model->status = 1;
  97. $row = $model->MultipleConditionQuery('id,name as title,icon, url as href,pid'); //2级目录
  98. if($row == null) return false;
  99. $model->pid = array_column($row,'id');;
  100. $list = $model->MultipleConditionQuery('id,name as title,icon, url as href,pid'); //三级目录
  101. //重组三级菜单
  102. if($list != null)
  103. {
  104. foreach ($list as $key=>$val)
  105. {
  106. $arr[$val['pid']][] = $val;
  107. }
  108. }
  109. foreach ($row as $k=>$v)
  110. {
  111. if(!empty($arr[$v['id']]))
  112. {
  113. $row[$k]['children'] = $arr[$v['id']];
  114. }
  115. }
  116. //重组返回数据 [父菜单ID]=>子菜单
  117. $brr = [];
  118. $brr[Yii::$app->request->post('pid')] = $row;
  119. return $brr;
  120. }
  121. public function MenuListSorting()
  122. {
  123. $model = new Frontend();
  124. $model->status = 1;
  125. $rows = $model->getList(Yii::$app->request->post());
  126. foreach ($rows['data'] as $key=>$val)
  127. {
  128. $num = substr_count(trim($val['paths'],','),',');
  129. $rows['data'][$key]['name'] = '|'.str_repeat('___',$num).$val['name'];
  130. $rows['data'][$key]['status'] = $this->status[$val['status']];
  131. $rows['data'][$key]['position'] = $val['position'] ==1 ? '首页' : '内页';
  132. $rows['data'][$key]['level'] = substr_count(trim($val['paths'],','),',');
  133. }
  134. return $rows;
  135. }
  136. public function TopMenu()
  137. {
  138. $Sysmenu = new Frontend();
  139. $Sysmenu->pid = 0;
  140. if(Yii::$app->session['user_info']['user'] != Yii::$app->params['UserSuperadministrator'])
  141. {
  142. $model = new Checkauthority();
  143. $Sysmenu->id = $model->MenuAccess();
  144. }
  145. $Sysmenu->status = 1;
  146. $row = $Sysmenu->MultipleConditionQuery(['id','name','pid','path','url','sort','status']);
  147. return $row;
  148. }
  149. public function Del()
  150. {
  151. $model = new Frontend();
  152. $model->pid = Yii::$app->request->post('id');
  153. $row = $model->OneFind();
  154. if($row != null) return '请按层级关系逐个删除';
  155. $model->pid = '';
  156. $model->id = Yii::$app->request->post('id');
  157. $one = $model->OneFind();
  158. if($one != null)
  159. {
  160. if($one->delete() == true) return true;
  161. }
  162. return false;
  163. }
  164. }