RolePath.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\Model;
  7. /**
  8. * @mixin \think\Model
  9. */
  10. class RolePath extends BaseModel
  11. {
  12. private $_list = [];
  13. /**
  14. * 获取分类数据
  15. * @return array
  16. * @throws \think\db\exception\DataNotFoundException
  17. * @throws \think\db\exception\DbException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. */
  20. public function getArMenu($show = 0)
  21. {
  22. $menus = $this
  23. ->field("rp.*,(select title from table_admin_menu where id = rp.menu_id) as menu_title")
  24. ->alias("rp")
  25. ->when(!empty($show), function ($query) {
  26. $query->where('is_show', 1);
  27. })
  28. ->order("seq", "desc")
  29. ->select();
  30. $data = [];
  31. foreach ($menus as $item) {
  32. $data[] = $item->getData();
  33. }
  34. $menuAr = Arr::getTree($data);
  35. return $menuAr;
  36. }
  37. /**
  38. * 删除栏目
  39. * @param $id
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function delMenu($id)
  45. {
  46. $mAr = $this->where('pid', $id)->select()->toArray();
  47. foreach ($mAr as $v) {
  48. $this->delMenu($v['id']);
  49. }
  50. $this->where('id', $id)->delete();
  51. }
  52. /**
  53. * 设置显示状态
  54. * @param $id 设置状态
  55. * @param $status
  56. * @return bool
  57. */
  58. public function setStatus($id, $status)
  59. {
  60. self::beginTrans();
  61. try {
  62. $this->where('id', $id)->save(['is_show' => $status]);
  63. $this->where('pid', $id)->save(['is_show' => $status]);
  64. self::commitTrans();
  65. return true;
  66. } catch (DbException $db) {
  67. self::rollbackTrans();
  68. return false;
  69. }
  70. }
  71. /**
  72. * 获取全部数据【select】
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public function getAll()
  78. {
  79. $this->_list = $this->order("seq","desc")->select()->toArray();
  80. return $this->_list;
  81. }
  82. /**
  83. * 根据id 获取数据
  84. * @param $id
  85. */
  86. public function findData($id){
  87. foreach ($this->_list as $v) {
  88. if($id == $v['id']) return $v;
  89. }
  90. return null;
  91. }
  92. /**
  93. * 获取数据
  94. */
  95. public function getRoleData()
  96. {
  97. return $this->getChild(0);
  98. }
  99. /**
  100. * 获取关联属性
  101. * @param $pid
  102. */
  103. public function getMenus($id) : int
  104. {
  105. foreach ($this->_list as $v) {
  106. if($v['id'] == $id) return $v['menu_id'];
  107. }
  108. return 0;
  109. }
  110. /**
  111. * 下一集合
  112. * @param $pid
  113. */
  114. public function getChild($pid)
  115. {
  116. $tAr = [];
  117. foreach ($this->_list as $v) {
  118. if ($v['pid'] == $pid) {
  119. $d = $v;
  120. $d['children'] = $this->getChild($v['id']);
  121. $tAr[] = $d;
  122. }
  123. }
  124. return $tAr;
  125. }
  126. /**
  127. * 获取model数据
  128. * @param $moule
  129. */
  130. public static function getMoule($str){
  131. $module = !empty($str) ? json_decode($str,true) : [];
  132. if(empty($module)) return [];
  133. $idAr = [];
  134. foreach ($module as $k => $v) {
  135. $idAr[] = str_replace('check_','',$k);
  136. }
  137. $model = new static();
  138. $model->getAll();
  139. $data = [];
  140. foreach ($idAr as $v) {
  141. $d = $model->findData($v);
  142. if(!empty($d)) $data[] = $d;
  143. }
  144. //获取关联栏目
  145. foreach ($data as &$v) {
  146. $v['path_menu_id'] = $model->getMenus($v['pid']);
  147. }
  148. return $data;
  149. }
  150. }