Goodscategory.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\api\controller;
  3. use app\model\goods\GoodsAttribute as GoodsAttributeModel;
  4. use app\model\goods\GoodsCategory as GoodsCategoryModel;
  5. use app\model\goods\Goods as GoodsModel;
  6. /**
  7. * 商品分类
  8. * Class Goodscategory
  9. * @package app\api\controller
  10. */
  11. class Goodscategory extends BaseApi
  12. {
  13. /**
  14. * 树状结构信息
  15. */
  16. public function tree()
  17. {
  18. $level = isset($this->params['level']) ? $this->params['level'] : 3;// 分类等级 1 2 3
  19. $template = isset($this->params['template']) ? $this->params['template'] : 2;// 模板 1:无图,2:有图,3:有商品
  20. $goods = new GoodsModel();
  21. $goods_category_model = new GoodsCategoryModel();
  22. $condition = [
  23. [ 'is_show', '=', 1 ],
  24. [ 'level', '<=', $level ]
  25. ];
  26. $field = "category_id,category_name,short_name,pid,level,image,category_id_1,category_id_2,category_id_3,image_adv";
  27. $order = "sort desc,category_id desc";
  28. $list = $goods_category_model->getCategoryTree($condition, $field, $order);
  29. // 查询商品
  30. if ($level == 3 && $template == 3) {
  31. $goods_field = 'gs.sku_id,gs.discount_price,gs.sale_num,gs.sku_image,gs.goods_name,gs.site_id,gs.website_id,gs.is_free_shipping,gs.introduction,gs.promotion_type';
  32. $alias = 'gs';
  33. $join = [
  34. [ 'goods g', 'gs.sku_id = g.sku_id', 'inner' ]
  35. ];
  36. foreach ($list['data'] as $k => $v) {
  37. if (!empty($v['child_list'])) {
  38. foreach ($v['child_list'] as $second_k => $second_v) {
  39. if (!empty($second_v['child_list'])) {
  40. foreach ($second_v['child_list'] as $third_k => $third_v) {
  41. $goods_condition = [
  42. [ 'gs.goods_state', '=', 1 ],
  43. [ 'gs.verify_state', '=', 1 ],
  44. [ 'gs.is_delete', '=', 0 ],
  45. [ 'gs.is_delete', '=', 0 ],
  46. [ 'gs.category_id_3', '=', $third_v['category_id'] ]
  47. ];
  48. $goods_list = $goods->getGoodsSkuPageList($goods_condition, 1, 3, 'gs.sort desc,gs.create_time desc', $goods_field, $alias, $join);
  49. $goods_list = $goods_list['data']['list'];
  50. $list['data'][ $k ]['child_list'][ $second_k ]['child_list'][ $third_k ]['goods_list'] = $goods_list;
  51. }
  52. } else {
  53. $list['data'][ $k ]['child_list'][ $second_k ]['child_list'] = [];
  54. }
  55. }
  56. } else {
  57. $list['data'][ $k ]['child_list'] = [];
  58. }
  59. }
  60. }
  61. return $this->response($list);
  62. }
  63. /**
  64. * 根据商品分类查询关联商品类型,查询关联品牌、属性
  65. * @return string
  66. */
  67. public function relevanceinfo()
  68. {
  69. $category_id = isset($this->params['category_id']) ? $this->params['category_id'] : 0;//分类id
  70. if (empty($category_id)) {
  71. return $this->response($this->error('', 'REQUEST_CATEGORY_ID'));
  72. }
  73. $goods_category_model = new GoodsCategoryModel();
  74. $category_info = $goods_category_model->getCategoryInfo([ [ 'category_id', '=', $category_id ] ], 'attr_class_id');
  75. $category_info = $category_info['data'];
  76. $goods_attribute_model = new GoodsAttributeModel();
  77. //商品类型关联品牌
  78. $brand_list = $goods_attribute_model->getAttrClassBrandList([ [ 'ngacb.attr_class_id', '=', $category_info['attr_class_id'] ] ]);
  79. $brand_list = $brand_list['data'];
  80. //商品类型关联属性
  81. $attribute_list = $goods_attribute_model->getAttributeList([ [ 'attr_class_id', '=', $category_info['attr_class_id'] ], [ 'is_query', '=', 1 ] ], 'attr_id,attr_name,attr_class_id,sort,is_query,is_spec,attr_value_list,attr_value_list,attr_type,site_id,attr_value_format');
  82. $attribute_list = $attribute_list['data'];
  83. if (!empty($attribute_list)) {
  84. foreach ($attribute_list as $k => $v) {
  85. $attribute_list[ $k ]['child'] = json_decode($attribute_list[ $k ]['attr_value_format'], true);
  86. }
  87. }
  88. $res = [
  89. 'brand_list' => $brand_list,
  90. 'attribute_list' => $attribute_list
  91. ];
  92. return $this->response($this->success($res));
  93. }
  94. }