Category.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\common\model;
  13. /**
  14. * 商品分类模型
  15. * Class Category
  16. * @package app\common\model
  17. */
  18. class Category extends BaseModel
  19. {
  20. // 定义表名
  21. protected $name = 'category';
  22. // 定义主键
  23. protected $pk = 'category_id';
  24. /**
  25. * 分类图片
  26. * @return \think\model\relation\HasOne
  27. */
  28. public function image()
  29. {
  30. return $this->hasOne('UploadFile', 'file_id', 'image_id');
  31. }
  32. /**
  33. * 分类详情
  34. * @param int|array $where
  35. * @param array $with
  36. * @return array|null|static
  37. */
  38. public static function detail($where, array $with = [])
  39. {
  40. return static::get($where, $with);
  41. }
  42. /**
  43. * 获取列表记录
  44. * @param array $param
  45. * @return array
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function getList(array $param = [])
  51. {
  52. $list = $this->getAll($param);
  53. return $this->getTreeData($list);
  54. }
  55. /**
  56. * 获取所有记录
  57. * @param array $param
  58. * @return \think\Collection
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. protected function getAll(array $param = [])
  64. {
  65. // 默认查询参数
  66. $params = $this->setQueryDefaultValue($param, [
  67. 'status' => -1 // 状态(1显示 0隐藏 -1全部)
  68. ]);
  69. // 设置检索条件
  70. $filter = [];
  71. $params['status'] > -1 && $filter[] = ['status', '=', $params['status']];
  72. // 查询列表数据
  73. return $this->with(['image'])
  74. ->where($filter)
  75. ->order(['sort', 'create_time'])
  76. ->select();
  77. }
  78. /**
  79. * 获取树状列表
  80. * @param $list
  81. * @param int $parentId
  82. * @return array
  83. */
  84. private function getTreeData($list, int $parentId = 0)
  85. {
  86. $data = [];
  87. foreach ($list as $key => $item) {
  88. if ($item['parent_id'] == $parentId) {
  89. $children = $this->getTreeData($list, $item['category_id']);
  90. !empty($children) && $item['children'] = $children;
  91. $data[] = $item;
  92. unset($list[$key]);
  93. }
  94. }
  95. return $data;
  96. }
  97. }