Goodsclass.php 6.3 KB

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