Category.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\model;
  10. use think\Cache;
  11. /**
  12. * 拼团商品分类模型
  13. * Class Category
  14. * @package app\common\model
  15. */
  16. class Category extends BaseModel
  17. {
  18. protected $name = 'category';
  19. /**
  20. * 分类图片
  21. * @return \think\model\relation\HasOne
  22. */
  23. public function image()
  24. {
  25. return $this->hasOne('uploadFile', 'file_id', 'image_id');
  26. }
  27. /**
  28. * 所有分类
  29. * @return mixed
  30. */
  31. public static function getALL()
  32. {
  33. $model = new static;
  34. if (!Cache::get('category_' . $model::$wxapp_id)) {
  35. $data = $model->with(['image'])->order(['sort' => 'asc', 'create_time' => 'asc'])->select();
  36. $all = !empty($data) ? $data->toArray() : [];
  37. $tree = [];
  38. foreach ($all as $first) {
  39. if ($first['parent_id'] != 0) continue;
  40. $twoTree = [];
  41. foreach ($all as $two) {
  42. if ($two['parent_id'] != $first['category_id']) continue;
  43. $threeTree = [];
  44. foreach ($all as $three)
  45. $three['parent_id'] == $two['category_id']
  46. && $threeTree[$three['category_id']] = $three;
  47. !empty($threeTree) && $two['child'] = $threeTree;
  48. $twoTree[$two['category_id']] = $two;
  49. }
  50. if (!empty($twoTree)) {
  51. array_multisort(array_column($twoTree, 'sort'), SORT_ASC, $twoTree);
  52. $first['child'] = $twoTree;
  53. }
  54. $tree[$first['category_id']] = $first;
  55. }
  56. Cache::tag('cache')->set('category_' . $model::$wxapp_id, compact('all', 'tree'));
  57. }
  58. return Cache::get('category_' . $model::$wxapp_id);
  59. }
  60. /**
  61. * 获取所有分类
  62. * @return mixed
  63. */
  64. public static function getCacheAll()
  65. {
  66. return self::getALL()['all'];
  67. }
  68. /**
  69. * 获取所有分类(树状结构)
  70. * @return mixed
  71. */
  72. public static function getCacheTree()
  73. {
  74. return self::getALL()['tree'];
  75. }
  76. /**
  77. * 获取所有分类(树状结构)
  78. * @return string
  79. */
  80. public static function getCacheTreeJson()
  81. {
  82. return json_encode(static::getCacheTree());
  83. }
  84. /**
  85. * 获取指定分类下的所有子分类id
  86. * @param $parent_id
  87. * @param array $all
  88. * @return array
  89. */
  90. public static function getSubCategoryId($parent_id, $all = [])
  91. {
  92. $arrIds = [$parent_id];
  93. empty($all) && $all = self::getCacheAll();
  94. foreach ($all as $key => $item) {
  95. if ($item['parent_id'] == $parent_id) {
  96. unset($all[$key]);
  97. $subIds = self::getSubCategoryId($item['category_id'], $all);
  98. !empty($subIds) && $arrIds = array_merge($arrIds, $subIds);
  99. }
  100. }
  101. return $arrIds;
  102. }
  103. /**
  104. * 指定的分类下是否存在子分类
  105. * @param $parentId
  106. * @return bool
  107. */
  108. protected static function hasSubCategory($parentId)
  109. {
  110. $all = self::getCacheAll();
  111. foreach ($all as $item) {
  112. if ($item['parent_id'] == $parentId) {
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. }