Menus.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/5
  6. * Time: 下午5:24
  7. */
  8. namespace backend\server;
  9. use backend\base\Help;
  10. use Yii;
  11. use common\models\Sysmenu;
  12. class Menus
  13. {
  14. public $status = [1 => '正常', 2 => '禁用'];
  15. public function Create()
  16. {
  17. $models = new Sysmenu();
  18. $models->scenario = 'add';
  19. $model = $models->Authenticator(Yii::$app->request->post('data'));
  20. if (!empty($model->pid)) {
  21. $model->id = $model->pid;
  22. $row = $model->FindById();
  23. $model->path = $row->path . $row->id . ',';
  24. unset($model['id']);
  25. }
  26. if (is_object($model)) {
  27. if ($model->insert() == true) return true;
  28. }
  29. return $model;
  30. }
  31. public function Edit()
  32. {
  33. $model = new Sysmenu();
  34. $model = $model->Authenticator(Yii::$app->request->post('data'));
  35. if (is_object($model)) {
  36. $model->id = Yii::$app->request->post('data')['id'];
  37. $row = $model->FindById();
  38. $row->name = $model->name;
  39. $row->url = $model->url;
  40. $row->sort = $model->sort;
  41. $row->status = $model->status;
  42. $row->icon = $model->icon;
  43. if ($row->update() == true) return true;
  44. }
  45. return $model;
  46. }
  47. public function getchildren()
  48. {
  49. $model = new Sysmenu();
  50. $modela = new Checkauthority();
  51. $arrId = $modela->MenuAccess();
  52. if (Yii::$app->session['user_info']['user'] != Yii::$app->params['UserSuperadministrator']) {
  53. $model->id = $arrId;
  54. }
  55. $model->pid = Yii::$app->request->post('pid');
  56. $model->status = 1;
  57. $row = $model->MultipleConditionQuery('id,name as title,icon, url as href,pid'); //2级目录
  58. if ($row == null) return false;
  59. $model->pid = array_column($row, 'id');;
  60. $list = $model->MultipleConditionQuery('id,name as title,icon, url as href,pid'); //三级目录
  61. //重组三级菜单
  62. if ($list != null) {
  63. foreach ($list as $key => $val) {
  64. $arr[$val['pid']][] = $val;
  65. }
  66. }
  67. foreach ($row as $k => $v) {
  68. if (!empty($arr[$v['id']])) {
  69. $row[$k]['children'] = $arr[$v['id']];
  70. }
  71. }
  72. //重组返回数据 [父菜单ID]=>子菜单
  73. $brr = [];
  74. $brr[Yii::$app->request->post('pid')] = $row;
  75. return $brr;
  76. }
  77. public function MenuListSorting()
  78. {
  79. $model = new Sysmenu();
  80. // $model->status = 1;
  81. $rows = $model->getList(Yii::$app->request->post());
  82. foreach ($rows['data'] as $key => $val) {
  83. $num = substr_count(trim($val['paths'], ','), ',');
  84. $rows['data'][$key]['name'] = '|' . str_repeat('___', $num) . $val['name'];
  85. // $rows['data'][$key]['status'] = $this->status[$val['status']];
  86. $rows['data'][$key]['level'] = substr_count(trim($val['paths'], ','), ',');
  87. }
  88. return $rows;
  89. }
  90. public function TopMenu()
  91. {
  92. $Sysmenu = new Sysmenu();
  93. $Sysmenu->pid = 0;
  94. if (Yii::$app->session['user_info']['user'] != Yii::$app->params['UserSuperadministrator']) {
  95. $model = new Checkauthority();
  96. $Sysmenu->id = $model->MenuAccess();
  97. }
  98. $Sysmenu->status = 1;
  99. $row = $Sysmenu->MultipleConditionQuery(['id', 'name', 'pid', 'path', 'url', 'sort', 'status']);
  100. return $row;
  101. }
  102. public function Del()
  103. {
  104. $model = new Sysmenu();
  105. $model->pid = Yii::$app->request->post('id');
  106. $row = $model->OneFind();
  107. if ($row != null) return '请按层级关系逐个删除';
  108. $model->pid = '';
  109. $model->id = Yii::$app->request->post('id');
  110. $one = $model->OneFind();
  111. if ($one != null) {
  112. if ($one->delete() == true) return true;
  113. }
  114. return false;
  115. }
  116. /**
  117. *修改菜单栏状态
  118. **/
  119. public function EditMenuStatus()
  120. {
  121. $Menu_id = intval(Yii::$app->request->post('menuid'));
  122. $res = intval(Yii::$app->request->post('res'));
  123. $model = new Sysmenu();
  124. $model->id = $Menu_id;
  125. $row = $model->FindById();
  126. $row->status = $res;
  127. if ($row->update() == true) return true;
  128. return false;
  129. }
  130. /*更改排序*/
  131. public function SetSortH()
  132. {
  133. $input = Yii::$app->request->post();
  134. $rows = Sysmenu::findOne($input['id']);
  135. $rows->sort = $input['sort'];
  136. if($rows->update()) return true;
  137. return false;
  138. }
  139. }