Goodsclass.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\api\controller;
  3. /**
  4. * 商品分类控制器
  5. */
  6. class Goodsclass extends MobileMall
  7. {
  8. public function initialize()
  9. {
  10. parent::initialize();
  11. }
  12. /**
  13. * @api {POST} api/Goodsclass/index 商品分类列表
  14. * @apiVersion 1.0.0
  15. * @apiGroup GoodsClass
  16. *
  17. * @apiParam {String} shop 商品数据
  18. * @apiParam {String} goodsclass 商品分类
  19. * @apiParam {Int} page 当前第几页
  20. * @apiParam {Int} perpage 每页多少
  21. *
  22. * @apiSuccess {String} code 返回码,10000为成功
  23. * @apiSuccess {String} message 返回消息
  24. * @apiSuccess {Object} result 返回数据
  25. * @apiSuccess {Object[]} result.class_list 商品分类列表
  26. * @apiSuccess {Int} result.class_list.id 分类ID
  27. * @apiSuccess {String} result.class_list.value 分类名称
  28. * @apiSuccess {Float} result.class_list.commis_rate 佣金
  29. * @apiSuccess {Object[]} result.class_list.children 子分类列表
  30. */
  31. public function index()
  32. {
  33. $goodsclass_model = model('goodsclass');
  34. $goods_class = $goodsclass_model->get_all_category();
  35. $goods_class = array_merge($goods_class);
  36. foreach ($goods_class as $key => $value) {
  37. $goods_class[$key]['id'] = $value['gc_id'];
  38. $goods_class[$key]['value'] = $value['gc_name'];
  39. if (isset($goods_class[$key]['class2'])) {
  40. $goods_class[$key]['children'] = array_merge($goods_class[$key]['class2']);
  41. } else {
  42. $goods_class[$key]['children'] = [];
  43. }
  44. foreach ($goods_class[$key]['children'] as $kk => $vv) {
  45. $goods_class[$key]['children'][$kk]['id'] = $vv['gc_id'];
  46. $goods_class[$key]['children'][$kk]['value'] = $vv['gc_name'];
  47. if (isset($goods_class[$key]['children'][$kk]['class3'])) {
  48. $goods_class[$key]['children'][$kk]['children'] = array_merge($goods_class[$key]['children'][$kk]['class3']);
  49. } else {
  50. $goods_class[$key]['children'][$kk]['children'] = [];
  51. }
  52. foreach ($goods_class[$key]['children'][$kk]['children'] as $k => $v) {
  53. $goods_class[$key]['children'][$kk]['children'][$k]['id'] = $v['gc_id'];
  54. $goods_class[$key]['children'][$kk]['children'][$k]['value'] = $v['gc_name'];
  55. $goods_class[$key]['children'][$kk]['children'][$k]['image'] = goodsclass_image($v['gc_image']);
  56. }
  57. }
  58. }
  59. $result['class_list'] = $goods_class;
  60. ds_json_encode(10000, '', $result);
  61. }
  62. /**
  63. * 返回一级分类列表
  64. */
  65. private function _get_root_class()
  66. {
  67. $goodsclass_model = model('goodsclass');
  68. $goods_class_array = model('goodsclass')->getGoodsclassForCacheModel();
  69. $class_list = $goodsclass_model->getGoodsclassListByParentId(0);
  70. foreach ($class_list as $key => $value) {
  71. $class_list[$key]['text'] = '';
  72. if (isset($goods_class_array[$value['gc_id']]['child'])) {
  73. $child_class_string = $goods_class_array[$value['gc_id']]['child'];
  74. $child_class_array = explode(',', $child_class_string);
  75. foreach ($child_class_array as $child_class) {
  76. $class_list[$key]['text'] .= $goods_class_array[$child_class]['gc_name'] . '/';
  77. }
  78. }
  79. $class_list[$key]['text'] = rtrim($class_list[$key]['text'], '/');
  80. }
  81. ds_json_encode(10000, '', array('class_list' => $class_list));
  82. }
  83. /**
  84. * 根据分类编号返回下级分类列表
  85. */
  86. private function _get_class_list($gc_id)
  87. {
  88. $goods_class_array = model('goodsclass')->getGoodsclassForCacheModel();
  89. $goods_class = $goods_class_array[$gc_id];
  90. if (empty($goods_class['child'])) {
  91. //无下级分类返回0
  92. return array('class_list' => array());
  93. } else {
  94. //返回下级分类列表
  95. $class_list = array();
  96. $child_class_string = $goods_class_array[$gc_id]['child'];
  97. $child_class_array = explode(',', $child_class_string);
  98. foreach ($child_class_array as $child_class) {
  99. $class_item = array();
  100. $class_item['gc_id'] = '';
  101. $class_item['gc_name'] = '';
  102. $class_item['gc_id'] .= $goods_class_array[$child_class]['gc_id'];
  103. $class_item['gc_name'] .= $goods_class_array[$child_class]['gc_name'];
  104. $class_item['image'] = ds_get_pic(ATTACH_COMMON, $goods_class_array[$child_class]['gc_image']);
  105. $class_list[] = $class_item;
  106. }
  107. return array('class_list' => $class_list);
  108. }
  109. }
  110. /**
  111. * 获取全部子集分类
  112. */
  113. public function get_child_all()
  114. {
  115. $gc_id = intval(input('param.gc_id'));
  116. $data = array();
  117. if ($gc_id > 0) {
  118. $prefix = 'api-goodsclass-all-';
  119. $data = rcache($gc_id, $prefix);
  120. if (empty($data)) {
  121. $data = $this->_get_class_list($gc_id);
  122. if (!empty($data['class_list'])) {
  123. foreach ($data['class_list'] as $key => $val) {
  124. $d = $this->_get_class_list($val['gc_id']);
  125. $data['class_list'][$key]['child'] = $d['class_list'];
  126. }
  127. }
  128. wcache($gc_id, $data, $prefix, 3600);
  129. }
  130. }
  131. ds_json_encode(10000, '', $data);
  132. }
  133. }