Service.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\cardservice\api\controller;
  11. use app\api\controller\BaseApi;
  12. use app\model\goods\Goods;
  13. use app\model\goods\GoodsService;
  14. use app\model\web\Config as ConfigModel;
  15. use addon\cardservice\model\ServiceGoods;
  16. use think\facade\Db;
  17. /**
  18. * 服务
  19. */
  20. class Service extends BaseApi
  21. {
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. $this->initStoreData();
  26. }
  27. public function detail()
  28. {
  29. $sku_id = isset($this->params[ 'sku_id' ]) ? $this->params[ 'sku_id' ] : 0;
  30. $goods_id = isset($this->params[ 'goods_id' ]) ? $this->params[ 'goods_id' ] : 0;
  31. $goods = new Goods();
  32. if (empty($sku_id) && !empty($goods_id)) {
  33. $sku_id = $goods->getGoodsInfo([ [ 'goods_id', '=', $goods_id ] ], 'sku_id')[ 'data' ][ 'sku_id' ] ?? 0;
  34. }
  35. if (empty($sku_id) && empty($goods_id)) {
  36. return $this->response($this->error('', 'REQUEST_ID'));
  37. }
  38. $condition = [
  39. [ 'gs.sku_id', '=', $sku_id ],
  40. [ 'gs.site_id', '=', $this->site_id ]
  41. ];
  42. $field = 'gs.goods_id,gs.sku_id,gs.qr_id,gs.goods_name,gs.sku_name,gs.sku_spec_format,gs.price,gs.market_price,gs.discount_price,gs.promotion_type,gs.start_time
  43. ,gs.end_time,gs.stock,gs.click_num,(g.sale_num + g.virtual_sale) as sale_num,gs.collect_num,gs.sku_image,gs.sku_images
  44. ,gs.goods_content,gs.goods_state,gs.is_free_shipping,gs.goods_spec_format,gs.goods_attr_format,gs.introduction,gs.unit,gs.video_url
  45. ,gs.is_virtual,gs.goods_service_ids,gs.max_buy,gs.min_buy,gs.is_limit,gs.limit_type,gs.support_trade_type,g.goods_image,g.keywords,g.stock_show,g.sale_show,g.market_price_show,g.barrage_show,g.evaluate,g.goods_class,g.sale_store,g.sale_channel';
  46. $join = [
  47. [ 'goods g', 'g.goods_id = gs.goods_id', 'inner' ]
  48. ];
  49. // 如果是连锁运营模式
  50. if ($this->store_data[ 'config' ][ 'store_business' ] == 'store') {
  51. $join[] = [ 'store_goods_sku sgs', 'gs.sku_id = sgs.sku_id and sgs.store_id=' . $this->store_id, 'left' ];
  52. $field .= ',sgs.status as store_goods_status';
  53. $field = str_replace('gs.price', 'IFNULL(IF(g.is_unify_price = 1,gs.price,sgs.price), gs.price) as price', $field);
  54. $field = str_replace('gs.discount_price', 'IFNULL(IF(g.is_unify_price = 1,gs.discount_price,sgs.price), gs.discount_price) as discount_price', $field);
  55. if ($this->store_data[ 'store_info' ][ 'stock_type' ] == 'store') {
  56. $field = str_replace('gs.stock', 'IFNULL(sgs.stock, 0) as stock', $field);
  57. }
  58. }
  59. $goods_sku_detail = $goods->getGoodsSkuInfo($condition, $field, 'gs', $join)[ 'data' ];
  60. if (empty($goods_sku_detail) || $goods_sku_detail[ 'goods_class' ] != ( new ServiceGoods() )->getGoodsClass()[ 'id' ]) return $this->response($this->error());
  61. $goods_sku_detail[ 'purchased_num' ] = 0; // 该商品已购数量
  62. $res[ 'goods_sku_detail' ] = $goods_sku_detail;
  63. // 商品服务
  64. $goods_service = new GoodsService();
  65. $goods_service_list = $goods_service->getServiceList([ [ 'site_id', '=', $this->site_id ], [ 'id', 'in', $res[ 'goods_sku_detail' ][ 'goods_service_ids' ] ] ], 'service_name,desc,icon');
  66. $res[ 'goods_sku_detail' ][ 'goods_service' ] = $goods_service_list[ 'data' ];
  67. return $this->response($this->success($res));
  68. }
  69. /**
  70. * 列表信息
  71. */
  72. public function page()
  73. {
  74. $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
  75. $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
  76. $goods_id_arr = isset($this->params[ 'goods_id_arr' ]) ? $this->params[ 'goods_id_arr' ] : '';//goods_id数组
  77. $keyword = isset($this->params[ 'keyword' ]) ? trim($this->params[ 'keyword' ]) : '';//关键词
  78. $service_category = isset($this->params[ 'service_category' ]) ? $this->params[ 'service_category' ] : 0;//分类
  79. $is_free_shipping = isset($this->params[ 'is_free_shipping' ]) ? $this->params[ 'is_free_shipping' ] : 0;//是否免邮
  80. $order = isset($this->params[ 'order' ]) ? $this->params[ 'order' ] : "";//排序(综合、销量、价格)
  81. $sort = isset($this->params[ 'sort' ]) ? $this->params[ 'sort' ] : "";//升序、降序
  82. $condition = [];
  83. $condition[] = [ 'gs.site_id', '=', $this->site_id ];
  84. $condition[] = [ 'g.goods_class', '=', ( new ServiceGoods() )->getGoodsClass()[ 'id' ] ];
  85. $condition[] = [ '', 'exp', Db::raw("(g.sale_channel = 'all' OR g.sale_channel = 'online')") ];
  86. if (!empty($goods_id_arr)) {
  87. $condition[] = [ 'gs.goods_id', 'in', $goods_id_arr ];
  88. }
  89. if (!empty($service_category)) {
  90. $condition[] = [ 'g.service_category', 'like', '%,' . $service_category . ',%' ];
  91. }
  92. if (!empty($keyword)) {
  93. $condition[] = [ 'g.goods_name|gs.sku_name|gs.keywords', 'like', '%' . $keyword . '%' ];
  94. }
  95. if (!empty($is_free_shipping)) {
  96. $condition[] = [ 'gs.is_free_shipping', '=', $is_free_shipping ];
  97. }
  98. // 非法参数进行过滤
  99. if ($sort != "desc" && $sort != "asc") {
  100. $sort = "";
  101. }
  102. // 非法参数进行过滤
  103. if ($order != '') {
  104. if ($order != "sale_num" && $order != "discount_price") {
  105. $order = 'gs.sort';
  106. } elseif ($order == "sale_num") {
  107. $order = 'sale_sort';
  108. } else {
  109. $order = 'gs.' . $order;
  110. }
  111. $order_by = $order . ' ' . $sort;
  112. } else {
  113. $config_model = new ConfigModel();
  114. $sort_config = $config_model->getGoodsSort($this->site_id);
  115. $sort_config = $sort_config[ 'data' ][ 'value' ];
  116. $order_by = 'g.sort ' . $sort_config[ 'type' ] . ',g.create_time desc';
  117. }
  118. $condition[] = [ 'g.goods_state', '=', 1 ];
  119. $condition[] = [ 'g.is_delete', '=', 0 ];
  120. $alias = 'gs';
  121. $field = 'gs.is_consume_discount,gs.discount_config,gs.discount_method,gs.member_price,gs.goods_id,gs.sort,gs.sku_id,gs.sku_name,gs.price,gs.market_price,gs.discount_price,gs.stock,(g.sale_num + g.virtual_sale) as sale_num,(gs.sale_num + gs.virtual_sale) as sale_sort,gs.sku_image,gs.goods_name,gs.site_id,gs.is_free_shipping,gs.introduction,gs.promotion_type,g.goods_image,g.promotion_addon,gs.is_virtual,g.goods_spec_format,g.recommend_way,gs.max_buy,gs.min_buy,gs.unit,gs.is_limit,gs.limit_type,g.label_name,g.stock_show,g.sale_show,g.market_price_show,g.barrage_show,g.sale_channel,g.sale_store';
  122. $join = [
  123. [ 'goods g', 'gs.sku_id = g.sku_id', 'inner' ]
  124. ];
  125. // 如果是连锁运营模式
  126. if ($this->store_data[ 'config' ][ 'store_business' ] == 'store') {
  127. $join[] = [ 'store_goods_sku sgs', 'g.sku_id = sgs.sku_id and sgs.store_id=' . $this->store_id, 'left' ];
  128. $condition[] = [ 'g.sale_store', 'like', [ '%all%', '%,' . $this->store_id . ',%' ], 'or' ];
  129. $condition[] = [ 'sgs.status', '=', 1 ];
  130. $field = str_replace('gs.price', 'IFNULL(IF(g.is_unify_price = 1,gs.price,sgs.price), gs.price) as price', $field);
  131. $field = str_replace('gs.discount_price', 'IFNULL(IF(g.is_unify_price = 1,gs.price,sgs.price), gs.price) as discount_price', $field);
  132. if ($this->store_data[ 'store_info' ][ 'stock_type' ] == 'store') {
  133. $field = str_replace('gs.stock', 'IFNULL(sgs.stock, 0) as stock', $field);
  134. }
  135. }
  136. $goods = new Goods();
  137. $list = $goods->getGoodsSkuPageList($condition, $page, $page_size, $order_by, $field, $alias, $join);
  138. return $this->response($list);
  139. }
  140. }