SystemMenu.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\system;
  4. use app\Request;
  5. use library\basic\BaseModel;
  6. use library\services\UtilService;
  7. use library\utils\Arr;
  8. use think\db\exception\DbException;
  9. use think\Model;
  10. /**
  11. * @mixin \think\Model
  12. */
  13. class SystemMenu extends BaseModel
  14. {
  15. private $_data;
  16. /**
  17. * 设置显示状态
  18. * @param $id
  19. * @param $status
  20. */
  21. public function setStatus($id,$status) {
  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. if(!empty($post['id'])) {
  40. $this->where('id',$post['id'])->save($post);
  41. return true;
  42. } else {
  43. unset($post['id']);
  44. $bool = $this->insert($post);
  45. return $bool;
  46. }
  47. }
  48. /**
  49. * 删除栏目
  50. * @param $id
  51. */
  52. public function delMenu($id){
  53. $this->where('pid',$id)->delete();
  54. $this->where('id',$id)->delete();
  55. return true;
  56. }
  57. /**
  58. * 获取admin端用户菜单
  59. * @param $roule_id
  60. * @return array
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public function getRoute($roule_id)
  66. {
  67. $rules = (new SystemRole())->getRoleId($roule_id);
  68. $menuAr = $this->getArMenu(1);
  69. //判断权限
  70. if($rules['is_system']) {
  71. $resultAr = Arr::toIviewUi($menuAr);
  72. }
  73. $resultAr = array_merge([[
  74. 'header' => '0',
  75. 'icon' => 'md-home',
  76. 'is_header' => 0,
  77. 'path' => '/system/index',
  78. 'title' => '主页'
  79. ]],$resultAr);
  80. return $resultAr;
  81. }
  82. /**
  83. * 获取分类数据
  84. * @return array
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public function getArMenu($show = 0){
  90. $menus = $this->when(!empty($show), function ($query){
  91. $query->where('is_show',1);
  92. })->order("seq","desc")->select();
  93. $data = [];
  94. foreach ($menus as $item) {
  95. $data[] = $item->getData();
  96. }
  97. $menuAr = Arr::getTree($data);
  98. return $menuAr;
  99. }
  100. }