Memberfavorites.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. use think\facade\Db;
  5. /**
  6. * ============================================================================
  7. * DSMall多用户商城
  8. * ============================================================================
  9. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  10. * 网站地址: http://www.csdeshang.com
  11. * ----------------------------------------------------------------------------
  12. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  13. * 不允许对程序代码以任何形式任何目的的再发布。
  14. * ============================================================================
  15. * 收藏商品控制器
  16. */
  17. class Memberfavorites extends MobileMember {
  18. public function initialize() {
  19. parent::initialize(); // TODO: Change the autogenerated stub
  20. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberfavorites.lang.php');
  21. }
  22. /**
  23. * @api {POST} api/memberfavorites/favorites_list 商品收藏列表
  24. * @apiVersion 1.0.0
  25. * @apiGroup MemberFavorites
  26. *
  27. * @apiHeader {String} X-DS-KEY 用户授权token
  28. *
  29. * @apiParam {String} per_page 每页显示数量
  30. * @apiParam {String} page 当前页数
  31. *
  32. * @apiSuccess {String} code 返回码,10000为成功
  33. * @apiSuccess {String} message 返回消息
  34. * @apiSuccess {Object} result 返回数据
  35. * @apiSuccess {Object[]} result.favorites_list 收藏列表
  36. * @apiSuccess {Int} result.favorites_list.fav_id 收藏ID
  37. * @apiSuccess {Int} result.favorites_list.goods_id 商品ID
  38. * @apiSuccess {String} result.favorites_list.goods_image 商品图片名称
  39. * @apiSuccess {String} result.favorites_list.goods_image_url 商品图片完整路径
  40. * @apiSuccess {String} result.favorites_list.goods_name 商品名称
  41. * @apiSuccess {Float} result.favorites_list.goods_price 商品价格
  42. * @apiSuccess {Int} result.favorites_list.store_id 店铺ID
  43. * @apiSuccess {Int} result.page_total 总页数
  44. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  45. */
  46. public function favorites_list() {
  47. $favorites_model = model('favorites');
  48. $favorites_list = $favorites_model->getGoodsFavoritesList(array(array('member_id' ,'=', $this->member_info['member_id'])), '*', $this->pagesize);
  49. $favorites_id = '';
  50. foreach ($favorites_list as $value) {
  51. $favorites_id .= $value['fav_id'] . ',';
  52. }
  53. $favorites_id = rtrim($favorites_id, ',');
  54. $goods_model = model('goods');
  55. $field = 'goods_id,goods_name,goods_price,goods_image,store_id';
  56. // 默认不显示预订商品
  57. $goods_list = $goods_model->getGoodsList(array(array('goods_id', 'in', $favorites_id)), $field);
  58. foreach ($goods_list as $key => $value) {
  59. $goods_list[$key]['fav_id'] = $value['goods_id'];
  60. $goods_list[$key]['goods_image_url'] = goods_cthumb($value['goods_image'], 240, $value['store_id']);
  61. }
  62. $result = array_merge(array('favorites_list' => $goods_list), mobile_page($favorites_model->page_info));
  63. ds_json_encode(10000, '', $result);
  64. }
  65. /**
  66. * @api {POST} api/memberfavorites/address_list 添加商品收藏
  67. * @apiVersion 1.0.0
  68. * @apiGroup MemberFavorites
  69. *
  70. * @apiHeader {String} X-DS-KEY 用户授权token
  71. *
  72. * @apiParam {String} goods_id 商品ID
  73. *
  74. * @apiSuccess {String} code 返回码,10000为成功
  75. * @apiSuccess {String} message 返回消息
  76. */
  77. public function favorites_add() {
  78. $goods_id = intval(input('post.goods_id'));
  79. if ($goods_id <= 0) {
  80. ds_json_encode(10001, lang('param_error'));
  81. }
  82. $favorites_model = model('favorites');
  83. //判断是否已经收藏
  84. $favorites_info = $favorites_model->getOneFavorites(array(
  85. 'fav_id' => $goods_id, 'fav_type' => 'goods',
  86. 'member_id' => $this->member_info['member_id']
  87. ));
  88. if (!empty($favorites_info)) {
  89. ds_json_encode(10001, lang('favorite_already_favorite_goods'));
  90. }
  91. //判断商品是否为当前会员所有
  92. $goods_model = model('goods');
  93. $goods_info = $goods_model->getGoodsInfoByID($goods_id);
  94. $seller_info = model('seller')->getSellerInfo(array('member_id' => $this->member_info['member_id']));
  95. if ($goods_info['store_id'] == $seller_info['store_id']) {
  96. ds_json_encode(10001, lang('favorite_no_my_product'));
  97. }
  98. //添加收藏
  99. $insert_arr = array();
  100. $insert_arr['member_id'] = $this->member_info['member_id'];
  101. $insert_arr['member_name'] = $this->member_info['member_name'];
  102. $insert_arr['fav_id'] = $goods_id;
  103. $insert_arr['fav_type'] = 'goods';
  104. $insert_arr['fav_time'] = TIMESTAMP;
  105. $result = $favorites_model->addFavorites($insert_arr);
  106. if ($result) {
  107. //增加收藏数量
  108. $goods_model->editGoodsById(array('goods_collect' => Db::raw('goods_collect+1')), $goods_id);
  109. ds_json_encode(10000, '', 1);
  110. } else {
  111. ds_json_encode(10001, lang('ds_common_op_fail'));
  112. }
  113. }
  114. /**
  115. * @api {POST} api/memberfavorites/favorites_del 删除商品收藏
  116. * @apiVersion 1.0.0
  117. * @apiGroup MemberFavorites
  118. *
  119. * @apiHeader {String} X-DS-KEY 用户授权token
  120. *
  121. * @apiParam {String} fav_id 主键ID
  122. *
  123. * @apiSuccess {String} code 返回码,10000为成功
  124. * @apiSuccess {String} message 返回消息
  125. */
  126. public function favorites_del() {
  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. }