Goodsclass.php 6.1 KB

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