Memberfavorites.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. use think\facade\Db;
  5. /**
  6. * ============================================================================
  7. *
  8. * ============================================================================
  9. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  10. * 网站地址: https://www.valimart.net/
  11. * ----------------------------------------------------------------------------
  12. *
  13. * ============================================================================
  14. * 收藏商品控制器
  15. */
  16. class Memberfavorites extends MobileMember {
  17. public function initialize() {
  18. parent::initialize(); // TODO: Change the autogenerated stub
  19. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberfavorites.lang.php');
  20. }
  21. /**
  22. * @api {POST} api/memberfavorites/favorites_list 商品收藏列表
  23. * @apiVersion 1.0.0
  24. * @apiGroup MemberFavorites
  25. *
  26. * @apiHeader {String} X-DS-KEY 用户授权token
  27. *
  28. * @apiParam {String} per_page 每页显示数量
  29. * @apiParam {String} page 当前页数
  30. *
  31. * @apiSuccess {String} code 返回码,10000为成功
  32. * @apiSuccess {String} message 返回消息
  33. * @apiSuccess {Object} result 返回数据
  34. * @apiSuccess {Object[]} result.favorites_list 收藏列表
  35. * @apiSuccess {Int} result.favorites_list.fav_id 收藏ID
  36. * @apiSuccess {Int} result.favorites_list.goods_id 商品ID
  37. * @apiSuccess {String} result.favorites_list.goods_image 商品图片名称
  38. * @apiSuccess {String} result.favorites_list.goods_image_url 商品图片完整路径
  39. * @apiSuccess {String} result.favorites_list.goods_name 商品名称
  40. * @apiSuccess {Float} result.favorites_list.goods_price 商品价格
  41. * @apiSuccess {Int} result.favorites_list.store_id 店铺ID
  42. * @apiSuccess {Int} result.page_total 总页数
  43. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  44. */
  45. public function favorites_list() {
  46. $favorites_model = model('favorites');
  47. $favorites_list = $favorites_model->getGoodsFavoritesList(array(array('member_id' ,'=', $this->member_info['member_id'])), '*', $this->pagesize);
  48. $favorites_id = '';
  49. foreach ($favorites_list as $value) {
  50. $favorites_id .= $value['fav_id'] . ',';
  51. }
  52. $favorites_id = rtrim($favorites_id, ',');
  53. $goods_model = model('goods');
  54. $field = 'goods_id,goods_name,goods_price,goods_image,store_id';
  55. // 默认不显示预订商品
  56. $goods_list = $goods_model->getGoodsList(array(array('goods_id', 'in', $favorites_id)), $field);
  57. foreach ($goods_list as $key => $value) {
  58. $goods_list[$key]['fav_id'] = $value['goods_id'];
  59. $goods_list[$key]['goods_image_url'] = goods_cthumb($value['goods_image'], 240, $value['store_id']);
  60. }
  61. $result = array_merge(array('favorites_list' => $goods_list), mobile_page($favorites_model->page_info));
  62. ds_json_encode(10000, '', $result);
  63. }
  64. /**
  65. * @api {POST} api/memberfavorites/address_list 添加商品收藏
  66. * @apiVersion 1.0.0
  67. * @apiGroup MemberFavorites
  68. *
  69. * @apiHeader {String} X-DS-KEY 用户授权token
  70. *
  71. * @apiParam {String} goods_id 商品ID
  72. *
  73. * @apiSuccess {String} code 返回码,10000为成功
  74. * @apiSuccess {String} message 返回消息
  75. */
  76. public function favorites_add() {
  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. $fav_id = intval(input('post.fav_id'));
  127. if ($fav_id <= 0) {
  128. ds_json_encode(10001, lang('param_error'));
  129. }
  130. $favorites_model = model('favorites');
  131. $goods_model = model('goods');
  132. $condition = array();
  133. $condition[] = array('fav_type','=','goods');
  134. $condition[] = array('fav_id','=',$fav_id);
  135. $condition[] = array('member_id','=',$this->member_info['member_id']);
  136. //判断收藏是否存在
  137. $favorites_info = $favorites_model->getOneFavorites($condition);
  138. if (empty($favorites_info)) {
  139. ds_json_encode(10001, lang('favorite_goods_not_exist'));
  140. }
  141. $favorites_model->delFavorites($condition);
  142. $goods_model->editGoodsById(array('goods_collect' => Db::raw('goods_collect-1')), $fav_id);
  143. ds_json_encode(10000, '', 1);
  144. }
  145. }