AdminMenu.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\model\system;
  4. use library\basic\BaseModel;
  5. use library\utils\Arr;
  6. use think\db\exception\DbException;
  7. use think\Model;
  8. /**
  9. * @mixin \think\Model
  10. */
  11. class AdminMenu extends BaseModel
  12. {
  13. private $_data;
  14. private $rolePath;
  15. /**
  16. * 设置显示状态
  17. * @param $id
  18. * @param $status
  19. */
  20. public function setStatus($id, $status)
  21. {
  22. self::beginTrans();
  23. try {
  24. $this->where('id', $id)->save(['is_show' => $status]);
  25. $this->where('pid', $id)->save(['is_show' => $status]);
  26. self::commitTrans();
  27. return true;
  28. } catch (DbException $db) {
  29. self::rollbackTrans();
  30. return false;
  31. }
  32. }
  33. /**
  34. * 保存分类
  35. * @param $post
  36. * @return bool
  37. */
  38. public function saveMenu($post)
  39. {
  40. if (!empty($post['id'])) {
  41. $this->where('id', $post['id'])->save($post);
  42. return true;
  43. } else {
  44. unset($post['id']);
  45. $bool = $this->insert($post);
  46. return $bool;
  47. }
  48. }
  49. /**
  50. * 删除栏目
  51. * @param $id
  52. */
  53. public function delMenu($id)
  54. {
  55. $this->where('pid', $id)->delete();
  56. $this->where('id', $id)->delete();
  57. return true;
  58. }
  59. /**
  60. * 获取admin端用户菜单
  61. * @param $roule_id
  62. * @return array
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function getRoute($roule_id)
  68. {
  69. $rules = (new AdminRole())->getRoleId($roule_id);
  70. $menuAr = $this->getArMenu(1);
  71. $paths = [];
  72. //判断权限
  73. if ($rules['is_system']) {
  74. $resultAr = Arr::toIviewUi($menuAr);
  75. } else {
  76. //获取权限
  77. $rolePathData = RolePath::getMoule($rules['module']);
  78. //权限对比
  79. $resultAr = $this->permissions($rolePathData, $menuAr);
  80. foreach ($rolePathData as $v) {
  81. if(strpos($v['role_path'],',') !== false) {
  82. $aAr = explode(',',$v['role_path']);
  83. $aAr = array_filter($aAr,function ($item){ return empty(trim($item)) ? false :true; });
  84. foreach ($aAr as $v2) {
  85. $paths[] = trim($v2);
  86. }
  87. } else {
  88. if(!empty($v['role_path'])) $paths[] = trim($v['role_path']);
  89. }
  90. }
  91. }
  92. $resultAr = array_merge([[
  93. 'header' => '0',
  94. 'icon' => 'md-home',
  95. 'is_header' => 0,
  96. 'path' => '/system/index',
  97. 'title' => '主页',
  98. ]], $resultAr);
  99. return [$resultAr, ['is_system' => $rules['is_system'], 'data' => $paths]];
  100. }
  101. /**
  102. * 获取分类数据
  103. * @return array
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\DbException
  106. * @throws \think\db\exception\ModelNotFoundException
  107. */
  108. public function getArMenu($show = 0)
  109. {
  110. $menus = $this->when(!empty($show), function ($query) {
  111. $query->where('is_show', 1);
  112. })->order("seq", "desc")->select();
  113. $data = [];
  114. foreach ($menus as $item) {
  115. $data[] = $item->getData();
  116. }
  117. $menuAr = Arr::getTree($data);
  118. return $menuAr;
  119. }
  120. /**
  121. * 权限分析
  122. * @param $rolePath 权利组
  123. * @param $menuAr 后台菜单组
  124. */
  125. public function permissions($rolePath, $menuAr)
  126. {
  127. $mPath = array_column($rolePath, 'path_menu_id');
  128. $nMenuAr = [];
  129. foreach ($menuAr as $v) {
  130. $d = [];
  131. if(!empty($v['children'])) {
  132. foreach ($v['children'] as $v2) {
  133. if (in_array($v2['id'], $mPath)) {
  134. $d[] = $v2;
  135. }
  136. }
  137. }
  138. if (!empty($d)) {
  139. $v['children'] = $d;
  140. $nMenuAr[] = $v;
  141. }
  142. }
  143. return $nMenuAr;
  144. }
  145. }