Memberfavoritesstore.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * ============================================================================
  13. * 收藏店铺控制器
  14. */
  15. class Memberfavoritesstore extends MobileMember
  16. {
  17. public function initialize()
  18. {
  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/memberfavoritesstore/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.fav_time 收藏时间
  38. * @apiSuccess {String} result.favorites_list.fav_time_text 收藏时间描述
  39. * @apiSuccess {Int} result.favorites_list.goods_count 商品数量
  40. * @apiSuccess {String} result.favorites_list.store_avatar 店铺头像名称
  41. * @apiSuccess {String} result.favorites_list.store_avatar_url 店铺头像完整路径
  42. * @apiSuccess {Int} result.favorites_list.store_collect 收藏数量
  43. * @apiSuccess {Int} result.favorites_list.store_id 店铺ID
  44. * @apiSuccess {String} result.favorites_list.store_name 店铺名称
  45. * @apiSuccess {Int} result.page_total 总页数
  46. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  47. */
  48. public function favorites_list()
  49. {
  50. $favorites_model = model('favorites');
  51. $store_model = model('store');
  52. $favorites_list = $favorites_model->getStoreFavoritesList(array(array(
  53. 'member_id', '=', $this->member_info['member_id'],
  54. )), '*', $this->pagesize);
  55. $store_list = array();
  56. $favorites_list_indexed = array();
  57. foreach ($favorites_list as $v) {
  58. $item = array();
  59. $item['fav_id'] = $v['fav_id'];
  60. $item['store_id'] = $v['store_id'];
  61. $item['store_name'] = $v['store_name'];
  62. $item['fav_time'] = $v['fav_time'];
  63. $item['fav_time_text'] = date('Y-m-d H:i', $v['fav_time']);
  64. $store = $store_model->getStoreInfoByID($v['store_id']);
  65. $item['goods_count'] = $store['goods_count'];
  66. $item['store_collect'] = $store['store_collect'];
  67. $item['store_avatar'] = $store['store_avatar'];
  68. $item['store_avatar_url'] = get_store_logo($store['store_avatar']);
  69. $store_list[] = $item;
  70. }
  71. $result = array_merge(array('favorites_list' => $store_list), mobile_page($favorites_model->page_info));
  72. ds_json_encode(10000, '', $result);
  73. }
  74. /**
  75. * @api {POST} api/memberfavoritesstore/favorites_add 添加店铺收藏
  76. * @apiVersion 1.0.0
  77. * @apiGroup MemberFavorites
  78. *
  79. * @apiHeader {String} X-DS-KEY 用户授权token
  80. *
  81. * @apiParam {String} store_id 店铺ID
  82. *
  83. * @apiSuccess {String} code 返回码,10000为成功
  84. * @apiSuccess {String} message 返回消息
  85. */
  86. public function favorites_add()
  87. {
  88. $fav_id = intval(input('post.store_id'));
  89. if ($fav_id <= 0) {
  90. ds_json_encode(10001, lang('param_error'));
  91. }
  92. $favorites_model = model('favorites');
  93. //判断是否已经收藏
  94. $favorites_info = $favorites_model->getOneFavorites(array(
  95. 'fav_id' => $fav_id, 'fav_type' => 'store',
  96. 'member_id' => $this->member_info['member_id'],
  97. ));
  98. if (!empty($favorites_info)) {
  99. ds_json_encode(10001, lang('favorite_already_favorite_store'));
  100. }
  101. //判断店铺是否为当前会员所有
  102. $seller_info = model('seller')->getSellerInfo(array('member_id' => $this->member_info['member_id']));
  103. if ($fav_id == $seller_info['store_id']) {
  104. ds_json_encode(10001, lang('favorite_no_my_store'));
  105. }
  106. //添加收藏
  107. $insert_arr = array();
  108. $insert_arr['member_id'] = $this->member_info['member_id'];
  109. $insert_arr['member_name'] = $this->member_info['member_name'];
  110. $insert_arr['fav_id'] = $fav_id;
  111. $insert_arr['fav_type'] = 'store';
  112. $insert_arr['fav_time'] = TIMESTAMP;
  113. $result = $favorites_model->addFavorites($insert_arr);
  114. if ($result) {
  115. //增加收藏数量
  116. $store_model = model('store');
  117. $store_model->editStore(array('store_collect' => Db::raw('store_collect+1')), array('store_id' => $fav_id));
  118. ds_json_encode(10000, lang('favorite_collect_success'), array('success' => '1'));
  119. } else {
  120. ds_json_encode(10001, lang('favorite_collect_fail'));
  121. }
  122. }
  123. /**
  124. * @api {POST} api/memberfavoritesstore/favorites_del 删除店铺收藏
  125. * @apiVersion 1.0.0
  126. * @apiGroup MemberFavorites
  127. *
  128. * @apiHeader {String} X-DS-KEY 用户授权token
  129. *
  130. * @apiParam {String} fav_id 主键ID
  131. *
  132. * @apiSuccess {String} code 返回码,10000为成功
  133. * @apiSuccess {String} message 返回消息
  134. */
  135. public function favorites_del()
  136. {
  137. $fav_id = intval(input('post.fav_id'));
  138. if ($fav_id <= 0) {
  139. ds_json_encode(10001, lang('param_error'));
  140. }
  141. $favorites_model = model('favorites');
  142. $store_model = model('store');
  143. $condition = array();
  144. $condition[] = array('fav_type', '=', 'store');
  145. $condition[] = array('fav_id', '=', $fav_id);
  146. $condition[] = array('member_id', '=', $this->member_info['member_id']);
  147. //判断是否已经收藏
  148. $favorites_info = $favorites_model->getOneFavorites($condition);
  149. if (empty($favorites_info)) {
  150. ds_json_encode(10001, lang('favorite_del_fail'));
  151. }
  152. $favorites_model->delFavorites($condition);
  153. $store_model->editStore(array('store_collect' => Db::raw('store_collect-1'),), array(array('store_id', '=', $fav_id), array('store_collect', '>', 0),));
  154. ds_json_encode(10000, lang('favorite_del_success'), array('success' => '1'));
  155. }
  156. }