Goods.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\pointexchange\api\controller;
  11. use app\api\controller\BaseApi;
  12. use addon\pointexchange\model\Exchange as ExchangeModel;
  13. use app\model\goods\GoodsService;
  14. use app\model\web\Config as ConfigModel;
  15. use app\model\goods\GoodsCategory as GoodsCategoryModel;
  16. /**
  17. * 积分兑换
  18. */
  19. class Goods extends BaseApi
  20. {
  21. /**
  22. * 详情信息
  23. */
  24. public function detail()
  25. {
  26. $id = isset($this->params[ 'id' ]) ? $this->params[ 'id' ] : 0;
  27. if (empty($id)) {
  28. return $this->response($this->error('', 'REQUEST_ID'));
  29. }
  30. $exchange_model = new ExchangeModel();
  31. $exchange_info = $exchange_model->getExchangeInfo($id, 'type, type_id')[ 'data' ];
  32. $condition = [
  33. [ 'peg.id', '=', $id ],
  34. [ 'peg.site_id', '=', $this->site_id ],
  35. [ 'peg.state', '=', 1 ],
  36. ];
  37. if (empty($exchange_info)) return $this->response($this->error('', '商品未找到'));
  38. $info = $exchange_model->getExchangeDetail($condition, $exchange_info[ 'type' ])[ 'data' ];
  39. if (empty($info)) return $this->response($this->error($info));
  40. if ($exchange_info[ 'type' ] == 1) {
  41. //判断商品规格项
  42. $goods_spec_format = $exchange_model->getGoodsSpecFormat($info[ 'exchange_id' ], $this->site_id, $info[ 'goods_spec_format' ]);
  43. $info[ 'goods_spec_format' ] = json_encode($goods_spec_format);
  44. $goods_service = new GoodsService();
  45. $info[ 'goods_service' ] = $goods_service->getServiceList([ [ 'site_id', '=', $this->site_id ], [ 'id', 'in', $info[ 'goods_service_ids' ] ] ], 'service_name,desc,icon')[ 'data' ];
  46. }
  47. return $this->response($this->success($info));
  48. }
  49. /**
  50. * 查询商品SKU集合
  51. * @return false|string
  52. */
  53. public function goodsSku()
  54. {
  55. $goods_id = isset($this->params[ 'goods_id' ]) ? $this->params[ 'goods_id' ] : 0;
  56. $exchange_id = isset($this->params[ 'exchange_id' ]) ? $this->params[ 'exchange_id' ] : 0;
  57. $type = isset($this->params[ 'type' ]) ? $this->params[ 'type' ] : 0;
  58. if (empty($goods_id)) {
  59. return $this->response($this->error('', 'REQUEST_ID'));
  60. }
  61. if (empty($exchange_id)) {
  62. return $this->response($this->error('', 'REQUEST_ID'));
  63. }
  64. $exchange_model = new ExchangeModel();
  65. $condition = [
  66. [ 'peg.id', '=', $exchange_id ],
  67. [ 'peg.site_id', '=', $this->site_id ],
  68. [ 'peg.state', '=', 1 ],
  69. ];
  70. $list = $exchange_model->getExchangeSkuList($condition, $type);
  71. if (!empty($list[ 'data' ])) {
  72. foreach ($list[ 'data' ] as $k => $v) {
  73. if (!empty($v[ 'goods_spec_format' ])) {
  74. $goods_spec_format = $exchange_model->getGoodsSpecFormat($v[ 'exchange_id' ], $this->site_id, $v[ 'goods_spec_format' ]);
  75. $list[ 'data' ][ $k ][ 'goods_spec_format' ] = json_encode($goods_spec_format);
  76. }
  77. }
  78. }
  79. return $this->response($list);
  80. }
  81. /**
  82. * 【废弃】基础信息
  83. */
  84. public function info()
  85. {
  86. $sku_id = isset($this->params[ 'sku_id' ]) ? $this->params[ 'sku_id' ] : 0;
  87. $exchange_id = isset($this->params[ 'exchange_id' ]) ? $this->params[ 'exchange_id' ] : 0;
  88. if (empty($sku_id)) {
  89. return $this->response($this->error('', 'REQUEST_SKU_ID'));
  90. }
  91. if (empty($exchange_id)) {
  92. return $this->response($this->error('', 'REQUEST_ID'));
  93. }
  94. $exchange_model = new ExchangeModel();
  95. $condition = [
  96. [ 'pe.type_id', '=', $sku_id ],
  97. [ 'peg.id', '=', $exchange_id ],
  98. [ 'peg.site_id', '=', $this->site_id ],
  99. [ 'peg.state', '=', 1 ],
  100. ];
  101. $info = $exchange_model->getExchangeDetail($condition, 1);
  102. $info = $info[ 'data' ];
  103. if (!empty($info)) {
  104. //判断商品规格项
  105. $goods_spec_format = $exchange_model->getGoodsSpecFormat($info[ 'exchange_id' ], $this->site_id, $info[ 'goods_spec_format' ]);
  106. $info[ 'goods_spec_format' ] = json_encode($goods_spec_format);
  107. } else {
  108. $sku_id = $exchange_model->getGoodsSpecFormat($info[ 'exchange_id' ], $this->site_id, '', $sku_id);
  109. $info = $this->success([ 'type' => 'again', 'sku_id' => $sku_id ]);
  110. }
  111. return $this->response($this->success($info));
  112. }
  113. public function page()
  114. {
  115. $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
  116. $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
  117. $type = isset($this->params[ 'type' ]) ? $this->params[ 'type' ] : 1;//兑换类型,1:商品,2:优惠券,3:红包
  118. //商品类型条件查询
  119. $keyword = isset($this->params[ 'keyword' ]) ? $this->params[ 'keyword' ] : ''; //关键词
  120. $order = isset($this->params[ 'order' ]) ? $this->params[ 'order' ] : "";//排序(综合、上新时间、价格)
  121. $sort = isset($this->params[ 'sort' ]) ? $this->params[ 'sort' ] : "";//升序、降序
  122. $min_point = isset($this->params[ 'min_point' ]) ? $this->params[ 'min_point' ] : 0;//积分区间,小
  123. $max_point = isset($this->params[ 'max_point' ]) ? $this->params[ 'max_point' ] : 0;//积分区间,大
  124. $category_id = isset($this->params[ 'category_id' ]) ? $this->params[ 'category_id' ] : 0;//分类
  125. $condition = [
  126. [ 'peg.state', '=', 1 ],
  127. [ 'peg.type', '=', $type ],
  128. [ 'peg.site_id', '=', $this->site_id ]
  129. ];
  130. if (!empty($keyword)) {
  131. $condition[] = [ 'g.goods_name|peg.name', 'like', '%' . $keyword . '%' ];
  132. }
  133. // 非法参数进行过滤
  134. if ($sort != "desc" && $sort != "asc") {
  135. $sort = "";
  136. }
  137. // 非法参数进行过滤
  138. if ($order != '') {
  139. if ($order != "create_time" && $order != "point") {
  140. $order = 'peg.sort';
  141. } elseif ($order == "create_time") {
  142. $order = 'peg.create_time';
  143. } else {
  144. $order = 'peg.' . $order;
  145. }
  146. $order_by = $order . ' ' . $sort;
  147. } else {
  148. $config_model = new ConfigModel();
  149. $sort_config = $config_model->getGoodsSort($this->site_id);
  150. $sort_config = $sort_config[ 'data' ][ 'value' ];
  151. $order_by = 'peg.sort ' . $sort_config[ 'type' ] . ',peg.create_time desc';
  152. }
  153. if ($min_point != "" && $max_point != "") {
  154. $condition[] = [ 'peg.point', 'between', [ $min_point, $max_point ] ];
  155. } elseif ($min_point != "") {
  156. $condition[] = [ 'peg.point', '>=', $min_point ];
  157. } elseif ($max_point != "") {
  158. $condition[] = [ 'peg.point', '<=', $max_point ];
  159. }
  160. if (!empty($category_id)) {
  161. $goods_category_model = new GoodsCategoryModel();
  162. // 查询当前
  163. $category_list = $goods_category_model->getCategoryList([ [ 'category_id', '=', $category_id ], [ 'site_id', '=', $this->site_id ] ], 'category_id,pid,level')[ 'data' ];
  164. // 查询子级
  165. $category_child_list = $goods_category_model->getCategoryList([ [ 'pid', '=', $category_id ], [ 'site_id', '=', $this->site_id ] ], 'category_id,pid,level')[ 'data' ];
  166. $temp_category_list = [];
  167. if (!empty($category_list)) {
  168. $temp_category_list = $category_list;
  169. } elseif (!empty($category_child_list)) {
  170. $temp_category_list = $category_child_list;
  171. }
  172. if (!empty($temp_category_list)) {
  173. $category_id_arr = [];
  174. foreach ($temp_category_list as $k => $v) {
  175. // 三级分类,并且都能查询到
  176. if ($v[ 'level' ] == 3 && !empty($category_list) && !empty($category_child_list)) {
  177. array_push($category_id_arr, $v[ 'pid' ]);
  178. } else {
  179. array_push($category_id_arr, $v[ 'category_id' ]);
  180. }
  181. }
  182. $category_id_arr = array_unique($category_id_arr);
  183. $temp_condition = [];
  184. foreach ($category_id_arr as $ck => $cv) {
  185. $temp_condition[] = '%,' . $cv . ',%';
  186. }
  187. $category_condition = $temp_condition;
  188. $condition[] = [ 'g.category_id', 'like', $category_condition, 'or' ];
  189. }
  190. }
  191. $field = 'peg.*';
  192. $alias = 'peg';
  193. $join = [];
  194. if ($type == 1) {
  195. $condition[] = [ 'g.is_delete', '=', 0 ];
  196. $condition[] = [ 'g.goods_state', '=', 1 ];
  197. $join = [
  198. [ 'goods g', 'peg.type_id = g.goods_id', 'inner' ]
  199. ];
  200. $field .= ',g.goods_name as name,g.goods_image as image, g.goods_stock as stock, g.stock_show,g.sale_show,(g.sale_num + g.virtual_sale) as sale_num';
  201. } elseif ($type == 2) {
  202. $field .= ',pct.type as coupon_type,pct.goods_type,pct.at_least,pct.money,pct.discount';
  203. $join = [
  204. [ 'promotion_coupon_type pct', 'peg.type_id = pct.coupon_type_id', 'inner' ]
  205. ];
  206. }
  207. $exchange_model = new ExchangeModel();
  208. $list = $exchange_model->getExchangeGoodsPageList($condition, $page, $page_size, $order_by, $field, $alias, $join);
  209. return $this->response($list);
  210. }
  211. }