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