Brand.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 品牌控制器
  10. */
  11. class Brand extends MobileMall
  12. {
  13. public function initialize()
  14. {
  15. parent::initialize();
  16. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/brand.lang.php');
  17. }
  18. /**
  19. * @api {POST} api/Brand/get_list 品牌列表
  20. * @apiVersion 1.0.0
  21. * @apiGroup Brand
  22. *
  23. * @apiSuccess {String} code 返回码,10000为成功
  24. * @apiSuccess {String} message 返回消息
  25. * @apiSuccess {Object} result 返回数据
  26. * @apiSuccess {Object} result.brand_class 品牌分类列表,键为品牌分类ID
  27. * @apiSuccess {Object} result.brand_class.brand_class 品牌分类名称
  28. * @apiSuccess {Object[]} result.brand_r 推荐品牌列表
  29. * @apiSuccess {Int} result.brand_r.brand_apply 品牌申请,0为申请中,1为通过
  30. * @apiSuccess {String} result.brand_r.brand_class 品牌分类名称
  31. * @apiSuccess {Int} result.brand_r.brand_id 品牌ID
  32. * @apiSuccess {String} result.brand_r.brand_initial 品牌首字母
  33. * @apiSuccess {String} result.brand_r.brand_name 品牌名称
  34. * @apiSuccess {String} result.brand_r.brand_pic 品牌图标
  35. * @apiSuccess {Int} result.brand_r.brand_recommend 品牌推荐,0为否,1为是
  36. * @apiSuccess {Int} result.brand_r.brand_showtype 品牌展示类型 0表示图片 1表示文字
  37. * @apiSuccess {Int} result.brand_r.brand_sort 品牌排序
  38. * @apiSuccess {Int} result.brand_r.gc_id 品牌分类ID
  39. * @apiSuccess {Int} result.brand_r.store_id 品牌申请店铺ID
  40. * @apiSuccess {Object} result.brand_c 品牌列表,按品牌分类ID分组,键为品牌分类ID
  41. * @apiSuccess {Int} result.brand_c.brand_apply 品牌申请,0为申请中,1为通过
  42. * @apiSuccess {String} result.brand_c.brand_class 品牌分类名称
  43. * @apiSuccess {Int} result.brand_c.brand_id 品牌ID
  44. * @apiSuccess {String} result.brand_c.brand_initial 品牌首字母
  45. * @apiSuccess {String} result.brand_c.brand_name 品牌名称
  46. * @apiSuccess {String} result.brand_c.brand_pic 品牌图标
  47. * @apiSuccess {Int} result.brand_c.brand_recommend 品牌推荐,0为否,1为是
  48. * @apiSuccess {Int} result.brand_c.brand_showtype 品牌展示类型 0表示图片 1表示文字
  49. * @apiSuccess {Int} result.brand_c.brand_sort 品牌排序
  50. * @apiSuccess {Int} result.brand_c.gc_id 品牌分类ID
  51. * @apiSuccess {Int} result.brand_c.store_id 品牌申请店铺ID
  52. */
  53. public function get_list()
  54. {
  55. $brand_mod = model('brand');
  56. $brand_c_list = $brand_mod->getBrandList(array('brand_apply' => '1'));
  57. $brands = $this->_tidyBrand($brand_c_list);
  58. extract($brands);
  59. ds_json_encode(10000, '', array('brand_l' => $brand_listchar, 'brand_c' => $brand_listnew, 'brand_class' => $brand_class, 'brand_r' => $brand_r_list));
  60. }
  61. /**
  62. * 所有品牌全部显示在一级类目下,不显示二三级类目
  63. * @param type $brand_c_list
  64. * @return type
  65. */
  66. private function _tidyBrand($brand_c_list)
  67. {
  68. $brand_listnew = array(); #品牌分类下对应的品牌
  69. $brand_listchar = array(); #品牌首字母下对应的品牌
  70. $brand_class = array(); #品牌分类
  71. $brand_r_list = array(); #推荐品牌
  72. if (!empty($brand_c_list) && is_array($brand_c_list)) {
  73. $goods_class = model('goodsclass')->getGoodsclassForCacheModel();
  74. foreach ($brand_c_list as $key => $brand_c) {
  75. $brand_c['brand_pic'] = brand_image($brand_c['brand_pic']);
  76. $gc_array = $this->_getTopClass($goods_class, $brand_c['gc_id']);
  77. if (empty($gc_array)) {
  78. $brand_listnew[0][] = $brand_c;
  79. $brand_class[0]['brand_class'] = lang('brand_index_other');
  80. } else {
  81. $brand_listnew[$gc_array['gc_id']][] = $brand_c;
  82. $brand_class[$gc_array['gc_id']]['brand_class'] = $gc_array['gc_name'];
  83. }
  84. $brand_listchar[$brand_c['brand_initial']][] = $brand_c;
  85. //推荐品牌
  86. if ($brand_c['brand_recommend'] == 1) {
  87. $brand_r_list[] = $brand_c;
  88. }
  89. }
  90. }
  91. krsort($brand_class);
  92. krsort($brand_listnew);
  93. ksort($brand_listchar);
  94. return array('brand_listchar' => $brand_listchar, 'brand_listnew' => $brand_listnew, 'brand_class' => $brand_class, 'brand_r_list' => $brand_r_list);
  95. }
  96. /**
  97. * 获取顶级商品分类\递归调用
  98. * @param type $goods_class
  99. * @param type $gc_id
  100. * @return type
  101. */
  102. private function _getTopClass($goods_class, $gc_id)
  103. {
  104. if (!isset($goods_class[$gc_id])) {
  105. return null;
  106. }
  107. if ($goods_class[$gc_id]['gc_parent_id'] == $gc_id) { //自身ID等于父ID
  108. return null;
  109. }
  110. if (isset($goods_class[$goods_class[$gc_id]['gc_parent_id']]['gc_parent_id']) && $goods_class[$goods_class[$gc_id]['gc_parent_id']]['gc_parent_id'] == $gc_id) { //父分类的父ID等于自身ID
  111. return null;
  112. }
  113. return $goods_class[$gc_id]['gc_parent_id'] == 0 ? $goods_class[$gc_id] : $this->_getTopClass($goods_class, $goods_class[$gc_id]['gc_parent_id']);
  114. }
  115. }