Menus.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. *------------------------------------------------------
  4. * Menus.php
  5. *------------------------------------------------------
  6. *
  7. * @author qqiu@qq.com
  8. * @date 2016/12/19 11:31
  9. * @version V1.0
  10. *
  11. */
  12. namespace App\Services\Admin;
  13. use App\Services\Base\BaseProcess;
  14. use App\Models\AdminMenusModel;
  15. use App\Services\Base\Tree;
  16. class Menus extends BaseProcess
  17. {
  18. /**
  19. * 模型
  20. */
  21. private $_model;
  22. /**
  23. * 初始化
  24. */
  25. public function __construct()
  26. {
  27. if (!$this->_model) $this->_model = new AdminMenusModel();
  28. }
  29. public function model()
  30. {
  31. $this->setSucessCode();
  32. return $this->_model;
  33. }
  34. public function find($id)
  35. {
  36. return $this->_model->find($id);
  37. }
  38. /**
  39. * 添加
  40. * @param unknown $data
  41. */
  42. public function create($data)
  43. {
  44. return $this->_model->create($data);
  45. }
  46. /**
  47. * 更新
  48. * @param unknown $id
  49. * @param unknown $data
  50. */
  51. public function update($id, $data)
  52. {
  53. $obj = $this->_model->find($id);
  54. if (!$obj) {
  55. return false;
  56. }
  57. $ok = $obj->update($data);
  58. return $ok;
  59. }
  60. /**
  61. * 删除
  62. * @param unknown $id
  63. */
  64. public function destroy($id)
  65. {
  66. return $this->_model->destroy($id);
  67. }
  68. /**
  69. * 获取当前菜单层级
  70. * @param $pid
  71. * @return level
  72. */
  73. public function getLevel($id)
  74. {
  75. $tree = new Tree();
  76. $MenusArr = $this->_model->orderBy('sort', 'DESC')->get()->toArray();
  77. $tree->init($MenusArr);
  78. $allParents = $tree->getPos($id);
  79. return count($allParents);
  80. }
  81. /**
  82. * 获取菜单树状结构
  83. */
  84. public function getMenusTree($search = array())
  85. {
  86. $currentQuery = $this->_model;
  87. if (isset($search['keyword']) && !empty($search['keyword'])) {
  88. $keywords = '%' . $search['keyword'] . '%';
  89. $currentQuery = $currentQuery->where(function ($query) use ($keywords) {
  90. $query->where('name', 'like', $keywords)
  91. // ->orwhere('module', 'like', $keywords)
  92. ->orwhere('path', 'like', $keywords)
  93. ->orwhere('mark', 'like', $keywords);
  94. });
  95. }
  96. $MenusArr = $currentQuery->orderBy('sort', 'DESC')->get()->toArray();
  97. $tree = new Tree();
  98. $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
  99. $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
  100. $tree->init($MenusArr);
  101. return $tree->getTree();
  102. }
  103. /**
  104. * 搜索
  105. * @param $search
  106. * @param $pagesize
  107. */
  108. public function search($search, $orderby = array('sort' => 'desc'), $pagesize = PAGE_NUMS)
  109. {
  110. $currentQuery = $this->_model;
  111. if (isset($search['keyword']) && !empty($search['keyword'])) {
  112. $keywords = '%' . $search['keyword'] . '%';
  113. $currentQuery = $currentQuery->where(function ($query) use ($keywords) {
  114. $query->where('name', 'like', $keywords)
  115. ->orwhere('module', 'like', $keywords)
  116. ->orwhere('contrl', 'like', $keywords);
  117. });
  118. }
  119. if (isset($search['min_level']) && !empty($search['min_level'])) {
  120. $currentQuery = $currentQuery->where('level', '<=', $search['min_level']);
  121. }
  122. if (isset($search['display']) && !empty($search['display'])) {
  123. $currentQuery = $currentQuery->where('display', $search['display']);
  124. }
  125. if ($orderby) {
  126. foreach ($orderby as $key => $sort) {
  127. $currentQuery = $currentQuery->orderBy($key, $sort);
  128. }
  129. }
  130. if ($pagesize) {
  131. $currentQuery = $currentQuery->paginate($pagesize);
  132. } else {
  133. $currentQuery = $currentQuery->get();
  134. }
  135. return $currentQuery;
  136. }
  137. }