Memberfavorites.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. use think\facade\Db;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 收藏商品控制器
  11. */
  12. class Memberfavorites extends MobileMember
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize(); // TODO: Change the autogenerated stub
  17. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberfavorites.lang.php');
  18. }
  19. /**
  20. * @api {POST} api/memberfavorites/favorites_list 商品收藏列表
  21. * @apiVersion 1.0.0
  22. * @apiGroup MemberFavorites
  23. *
  24. * @apiHeader {String} X-DS-KEY 用户授权token
  25. *
  26. * @apiParam {String} per_page 每页显示数量
  27. * @apiParam {String} page 当前页数
  28. *
  29. * @apiSuccess {String} code 返回码,10000为成功
  30. * @apiSuccess {String} message 返回消息
  31. * @apiSuccess {Object} result 返回数据
  32. * @apiSuccess {Object[]} result.favorites_list 收藏列表
  33. * @apiSuccess {Int} result.favorites_list.fav_id 收藏ID
  34. * @apiSuccess {Int} result.favorites_list.goods_id 商品ID
  35. * @apiSuccess {String} result.favorites_list.goods_image 商品图片名称
  36. * @apiSuccess {String} result.favorites_list.goods_image_url 商品图片完整路径
  37. * @apiSuccess {String} result.favorites_list.goods_name 商品名称
  38. * @apiSuccess {Float} result.favorites_list.goods_price 商品价格
  39. * @apiSuccess {Int} result.favorites_list.store_id 店铺ID
  40. * @apiSuccess {Int} result.page_total 总页数
  41. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  42. */
  43. public function favorites_list()
  44. {
  45. $favorites_model = model('favorites');
  46. $favorites_list = $favorites_model->getGoodsFavoritesList(array(array('member_id', '=', $this->member_info['member_id'])), '*', $this->pagesize);
  47. $favorites_id = '';
  48. foreach ($favorites_list as $value) {
  49. $favorites_id .= $value['fav_id'] . ',';
  50. }
  51. $favorites_id = rtrim($favorites_id, ',');
  52. $goods_model = model('goods');
  53. $field = 'goods_id,goods_name,goods_price,goods_image,store_id';
  54. // 默认不显示预订商品
  55. $goods_list = $goods_model->getGoodsList(array(array('goods_id', 'in', $favorites_id)), $field);
  56. foreach ($goods_list as $key => $value) {
  57. $goods_list[$key]['fav_id'] = $value['goods_id'];
  58. $goods_list[$key]['goods_image_url'] = goods_cthumb($value['goods_image'], 240, $value['store_id']);
  59. }
  60. $result = array_merge(array('favorites_list' => $goods_list), mobile_page($favorites_model->page_info));
  61. ds_json_encode(10000, '', $result);
  62. }
  63. /**
  64. * @api {POST} api/memberfavorites/address_list 添加商品收藏
  65. * @apiVersion 1.0.0
  66. * @apiGroup MemberFavorites
  67. *
  68. * @apiHeader {String} X-DS-KEY 用户授权token
  69. *
  70. * @apiParam {String} goods_id 商品ID
  71. *
  72. * @apiSuccess {String} code 返回码,10000为成功
  73. * @apiSuccess {String} message 返回消息
  74. */
  75. public function favorites_add()
  76. {
  77. $goods_id = intval(input('post.goods_id'));
  78. if ($goods_id <= 0) {
  79. ds_json_encode(10001, lang('param_error'));
  80. }
  81. $favorites_model = model('favorites');
  82. //判断是否已经收藏
  83. $favorites_info = $favorites_model->getOneFavorites(array(
  84. 'fav_id' => $goods_id, 'fav_type' => 'goods',
  85. 'member_id' => $this->member_info['member_id']
  86. ));
  87. if (!empty($favorites_info)) {
  88. ds_json_encode(10001, lang('favorite_already_favorite_goods'));
  89. }
  90. //判断商品是否为当前会员所有
  91. $goods_model = model('goods');
  92. $goods_info = $goods_model->getGoodsInfoByID($goods_id);
  93. $seller_info = model('seller')->getSellerInfo(array('member_id' => $this->member_info['member_id']));
  94. if ($goods_info['store_id'] == $seller_info['store_id']) {
  95. ds_json_encode(10001, lang('favorite_no_my_product'));
  96. }
  97. //添加收藏
  98. $insert_arr = array();
  99. $insert_arr['member_id'] = $this->member_info['member_id'];
  100. $insert_arr['member_name'] = $this->member_info['member_name'];
  101. $insert_arr['fav_id'] = $goods_id;
  102. $insert_arr['fav_type'] = 'goods';
  103. $insert_arr['fav_time'] = TIMESTAMP;
  104. $result = $favorites_model->addFavorites($insert_arr);
  105. if ($result) {
  106. //增加收藏数量
  107. $goods_model->editGoodsById(array('goods_collect' => Db::raw('goods_collect+1')), $goods_id);
  108. ds_json_encode(10000, '', 1);
  109. } else {
  110. ds_json_encode(10001, lang('ds_common_op_fail'));
  111. }
  112. }
  113. /**
  114. * @api {POST} api/memberfavorites/favorites_del 删除商品收藏
  115. * @apiVersion 1.0.0
  116. * @apiGroup MemberFavorites
  117. *
  118. * @apiHeader {String} X-DS-KEY 用户授权token
  119. *
  120. * @apiParam {String} fav_id 主键ID
  121. *
  122. * @apiSuccess {String} code 返回码,10000为成功
  123. * @apiSuccess {String} message 返回消息
  124. */
  125. public function favorites_del()
  126. {
  127. $fav_id = intval(input('post.fav_id'));
  128. if ($fav_id <= 0) {
  129. ds_json_encode(10001, lang('param_error'));
  130. }
  131. $favorites_model = model('favorites');
  132. $goods_model = model('goods');
  133. $condition = array();
  134. $condition[] = array('fav_type', '=', 'goods');
  135. $condition[] = array('fav_id', '=', $fav_id);
  136. $condition[] = array('member_id', '=', $this->member_info['member_id']);
  137. //判断收藏是否存在
  138. $favorites_info = $favorites_model->getOneFavorites($condition);
  139. if (empty($favorites_info)) {
  140. ds_json_encode(10001, lang('favorite_goods_not_exist'));
  141. }
  142. $favorites_model->delFavorites($condition);
  143. $goods_model->editGoodsById(array('goods_collect' => Db::raw('goods_collect-1')), $fav_id);
  144. ds_json_encode(10000, '', 1);
  145. }
  146. }