AdverPage.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\api;
  4. use think\Model;
  5. /**
  6. * @mixin \think\Model
  7. */
  8. class AdverPage extends Model
  9. {
  10. /**
  11. * 保存分类
  12. * @param $post
  13. * @return bool
  14. */
  15. public function savePage($post){
  16. if(!empty($post['id'])) {
  17. $this->where('id',$post['id'])->save($post);
  18. return true;
  19. } else {
  20. unset($post['id']);
  21. $post['add_time'] = time();
  22. $bool = $this->insert($post);
  23. return $bool;
  24. }
  25. }
  26. public function delAdver($id){
  27. $this->where('pid',$id)->delete();
  28. $this->where('id',$id)->delete();
  29. return true;
  30. }
  31. /**
  32. * 获取分类数据
  33. * @return array
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function getArMenu($cate_name = '',$pid = '',$show = 0){
  39. $menus = $this
  40. ->when(!empty($cate_name),function ($query) use($cate_name){
  41. $query->whereLike('cate_name',"%{$cate_name}%");
  42. })
  43. ->when(!empty($pid),function ($query) use($pid){
  44. $query->where('pid',$pid);
  45. })
  46. ->order("sort","desc")
  47. ->select();
  48. $data = [];
  49. foreach ($menus as $item) {
  50. $data[] = $item->getData();
  51. }
  52. $menuAr = self::getTree($data);
  53. return $menuAr;
  54. }
  55. /**
  56. * 获取树型菜单
  57. * @param $data
  58. * @param int $pid
  59. * @param int $level
  60. * @return array
  61. */
  62. public static function getTree($data, $pid = 0, $level = 0)
  63. {
  64. $childs = self::getChild($data, $pid, $level);
  65. array_multisort(array_column($childs, 'sort'), SORT_DESC, $childs);
  66. foreach ($childs as $key => $navItem) {
  67. $resChild = self::getTree($data, $navItem['id']);
  68. if (null != $resChild) {
  69. $childs[$key]['children'] = $resChild;
  70. }
  71. }
  72. return $childs;
  73. }
  74. /**
  75. * 获取子菜单
  76. * @param $arr
  77. * @param $id
  78. * @param $lev
  79. * @return array
  80. */
  81. private static function getChild(&$arr, $id, $lev)
  82. {
  83. $child = [];
  84. foreach ($arr as $k => $value) {
  85. if ($value['pid'] == $id) {
  86. $value['level'] = $lev;
  87. $child[] = $value;
  88. }
  89. }
  90. return $child;
  91. }
  92. }