Category.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. use cores\BaseModel;
  14. /**
  15. * 商品分类模型
  16. * Class Category
  17. * @package app\common\model
  18. */
  19. class Category extends BaseModel
  20. {
  21. // 定义表名
  22. protected $name = 'category';
  23. // 定义主键
  24. protected $pk = 'category_id';
  25. /**
  26. * 分类图片
  27. * @return \think\model\relation\HasOne
  28. */
  29. public function image()
  30. {
  31. return $this->hasOne('UploadFile', 'file_id', 'image_id');
  32. }
  33. /**
  34. * 分类详情
  35. * @param int|array $where
  36. * @param array $with
  37. * @return array|null|static
  38. */
  39. public static function detail($where, array $with = [])
  40. {
  41. return static::get($where, $with);
  42. }
  43. /**
  44. * 获取列表记录
  45. * @param array $param
  46. * @return array
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function getList(array $param = [])
  52. {
  53. $list = $this->getAll($param);
  54. return $this->getTreeData($list);
  55. }
  56. /**
  57. * 获取所有记录
  58. * @param array $param
  59. * @return \think\Collection
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. protected function getAll(array $param = [])
  65. {
  66. // 默认查询参数
  67. $params = $this->setQueryDefaultValue($param, [
  68. 'status' => -1 // 状态(1显示 0隐藏 -1全部)
  69. ]);
  70. // 设置检索条件
  71. $filter = [];
  72. $params['status'] > -1 && $filter[] = ['status', '=', $params['status']];
  73. // 查询列表数据
  74. return $this->with(['image'])
  75. ->where($filter)
  76. ->order(['sort', 'create_time'])
  77. ->select();
  78. }
  79. /**
  80. * 获取树状列表
  81. * @param $list
  82. * @param int $parentId
  83. * @return array
  84. */
  85. private function getTreeData($list, int $parentId = 0)
  86. {
  87. $data = [];
  88. foreach ($list as $key => $item) {
  89. if ($item['parent_id'] == $parentId) {
  90. $children = $this->getTreeData($list, $item['category_id']);
  91. !empty($children) && $item['children'] = $children;
  92. $data[] = $item;
  93. unset($list[$key]);
  94. }
  95. }
  96. return $data;
  97. }
  98. }