Column.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace addons\wwh\model;
  3. use think\Db;
  4. use think\Model;
  5. class Column extends Model
  6. {
  7. // 表名
  8. protected $name = 'wwh_column';
  9. /**
  10. * 获取顶级栏目
  11. */
  12. public static function getTopColumn($navCur)
  13. {
  14. if ($navCur['parent_id'] == 0) {
  15. return $navCur;
  16. }
  17. $top = self::where('id', $navCur['parent_id'])
  18. ->where('parent_id', 0)
  19. ->find();
  20. if (!$top) {
  21. // 如果父级不是顶级,继续向上查找
  22. $parentId = $navCur['parent_id'];
  23. while ($parentId != 0) {
  24. $parent = self::where('id', $parentId)->find();
  25. if ($parent['parent_id'] == 0) {
  26. $top = $parent;
  27. break;
  28. }
  29. $parentId = $parent['parent_id'];
  30. }
  31. }
  32. return $top ?: $navCur;
  33. }
  34. /**
  35. * 获取第二级栏目
  36. */
  37. public static function getSecondId($currentId)
  38. {
  39. $current = self::where('id', $currentId)
  40. ->field('parent_ids')
  41. ->find();
  42. if (!$current || empty($current['parent_ids'])) {
  43. return 0;
  44. }
  45. $parents = array_values(array_filter(
  46. explode(',', $current['parent_ids']),
  47. function($v) {
  48. return trim($v) !== '' && $v !== '0';
  49. }
  50. ));
  51. return $parents[1] ?? 0;
  52. }
  53. }