Goodscollect.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\api\controller;
  3. use app\model\goods\GoodsCollect as GoodsCollectModel;
  4. use app\model\goods\Goods as GoodsModel;
  5. /**
  6. * 商品收藏
  7. * @author Administrator
  8. *
  9. */
  10. class Goodscollect extends BaseApi
  11. {
  12. /**
  13. * 添加信息
  14. */
  15. public function add()
  16. {
  17. $token = $this->checkToken();
  18. if ($token[ 'code' ] < 0) return $this->response($token);
  19. $goods_id = isset($this->params[ 'goods_id' ]) ? $this->params[ 'goods_id' ] : 0;
  20. $sku_id = isset($this->params[ 'sku_id' ]) ? $this->params[ 'sku_id' ] : 0;
  21. $sku_name = isset($this->params[ 'sku_name' ]) ? $this->params[ 'sku_name' ] : '';
  22. $sku_price = isset($this->params[ 'sku_price' ]) ? $this->params[ 'sku_price' ] : '';
  23. $sku_image = isset($this->params[ 'sku_image' ]) ? $this->params[ 'sku_image' ] : '';
  24. if (empty($goods_id)) {
  25. return $this->response($this->error('', 'REQUEST_GOODS_ID'));
  26. }
  27. if (empty($sku_id)) {
  28. return $this->response($this->error('', 'REQUEST_SKU_ID'));
  29. }
  30. $goods_collect_model = new GoodsCollectModel();
  31. $data = [
  32. 'member_id' => $token[ 'data' ][ 'member_id' ],
  33. 'goods_id' => $goods_id,
  34. 'sku_id' => $sku_id,
  35. 'sku_name' => $sku_name,
  36. 'sku_price' => $sku_price,
  37. 'sku_image' => $sku_image,
  38. 'site_id' => $this->site_id
  39. ];
  40. $res = $goods_collect_model->addCollect($data);
  41. return $this->response($res);
  42. }
  43. /**
  44. * 删除信息
  45. */
  46. public function delete()
  47. {
  48. $token = $this->checkToken();
  49. if ($token[ 'code' ] < 0) return $this->response($token);
  50. $goods_id = isset($this->params[ 'goods_id' ]) ? $this->params[ 'goods_id' ] : 0;
  51. if (empty($goods_id)) {
  52. return $this->response($this->error('', 'REQUEST_GOODS_ID'));
  53. }
  54. $goods_collect_model = new GoodsCollectModel();
  55. $res = $goods_collect_model->deleteCollect($token[ 'data' ][ 'member_id' ], $goods_id);
  56. return $this->response($res);
  57. }
  58. /**
  59. * 分页列表信息
  60. */
  61. public function page()
  62. {
  63. $token = $this->checkToken();
  64. if ($token[ 'code' ] < 0) return $this->response($token);
  65. $this->initStoreData();
  66. $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
  67. $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
  68. $goods_collect_model = new GoodsModel();
  69. $condition = [
  70. [ 'gc.member_id', '=', $token[ 'data' ][ 'member_id' ] ],
  71. [ ' g.is_delete', '=', 0 ]
  72. ];
  73. $join = [
  74. [ 'goods_collect gc', 'gc.goods_id = g.goods_id', 'inner' ],
  75. [ 'goods_sku sku', 'g.sku_id = sku.sku_id', 'inner' ]
  76. ];
  77. $field = 'gc.collect_id, gc.member_id, gc.goods_id, gc.sku_id,sku.sku_name, gc.sku_price, gc.sku_image,g.goods_name,g.is_free_shipping,sku.promotion_type,sku.member_price,sku.discount_price,g.sale_num,g.price,g.market_price,g.is_virtual, g.goods_image';
  78. if ($this->store_data[ 'config' ][ 'store_business' ] == 'store') {
  79. $join[] = [ 'store_goods_sku sgs', 'g.sku_id = sgs.sku_id and sgs.store_id=' . $this->store_id, 'left' ];
  80. $field = str_replace('sku.price', 'IFNULL(IF(sku.is_unify_price = 1,sku.price,sgs.price), sku.price) as price', $field);
  81. $field = str_replace('sku.discount_price', 'IFNULL(IF(sku.is_unify_price = 1,sku.discount_price,sgs.price), sku.discount_price) as discount_price', $field);
  82. }
  83. $list = $goods_collect_model->getGoodsPageList($condition, $page, $page_size, 'gc.create_time desc', $field, 'g', $join);
  84. return $this->response($list);
  85. }
  86. /**
  87. * 检测用户是否收藏商品
  88. * @param int $id
  89. * @return false|string
  90. */
  91. public function iscollect($id = 0)
  92. {
  93. $token = $this->checkToken();
  94. if ($token[ 'code' ] < 0) return $this->response($token);
  95. $goods_id = isset($this->params[ 'goods_id' ]) ? $this->params[ 'goods_id' ] : 0;
  96. if (!empty($id)) {
  97. $goods_id = $id;
  98. }
  99. if (empty($goods_id)) {
  100. return $this->response($this->error('', 'REQUEST_GOODS_ID'));
  101. }
  102. $goods_collect_model = new GoodsCollectModel();
  103. $res = $goods_collect_model->getIsCollect($goods_id, $token[ 'data' ][ 'member_id' ]);
  104. return $this->response($res);
  105. }
  106. }