Memberfavorites.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. use think\facade\Db;
  6. /**
  7. * ============================================================================
  8. *
  9. * ============================================================================
  10. *
  11. * ----------------------------------------------------------------------------
  12. *
  13. * ============================================================================
  14. * 控制器
  15. */
  16. class Memberfavorites extends BaseMember
  17. {
  18. public function initialize()
  19. {
  20. parent::initialize();
  21. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberfavorites.lang.php');
  22. }
  23. /**
  24. * 增加商品收藏
  25. */
  26. public function favoritesgoods()
  27. {
  28. $fav_id = intval(input('param.fid'));
  29. if ($fav_id <= 0) {
  30. echo json_encode(array('done' => false, 'msg' => lang('favorite_collect_fail')));
  31. die;
  32. }
  33. $favorites_model = model('favorites');
  34. //判断是否已经收藏
  35. $favorites_info = $favorites_model->getOneFavorites(array(
  36. 'fav_id' => "$fav_id", 'fav_type' => 'goods',
  37. 'member_id' => session('member_id')
  38. ));
  39. if (!empty($favorites_info)) {
  40. echo json_encode(array(
  41. 'done' => false, 'msg' => lang('favorite_already_favorite_goods')
  42. ));
  43. die;
  44. }
  45. //判断商品是否为当前会员所有
  46. $goods_model = model('goods');
  47. $goods_info = $goods_model->getGoodsInfoByID($fav_id);
  48. if ($goods_info['store_id'] == session('store_id')) {
  49. echo json_encode(array('done' => false, 'msg' => lang('favorite_no_my_product')));
  50. die;
  51. }
  52. //添加收藏
  53. $insert_arr = array();
  54. $insert_arr['member_id'] = session('member_id');
  55. $insert_arr['member_name'] = session('member_name');
  56. $insert_arr['fav_id'] = $fav_id;
  57. $insert_arr['fav_type'] = 'goods';
  58. $insert_arr['fav_time'] = TIMESTAMP;
  59. $result = $favorites_model->addFavorites($insert_arr);
  60. if ($result) {
  61. //增加收藏数量
  62. $goods_model->editGoodsById(array('goods_collect' => Db::raw('goods_collect+1')), $fav_id);
  63. echo json_encode(array('done' => true, 'msg' => lang('favorite_collect_success')));
  64. die;
  65. } else {
  66. echo json_encode(array('done' => false, 'msg' => lang('favorite_collect_fail')));
  67. die;
  68. }
  69. }
  70. /**
  71. * 增加店铺收藏
  72. */
  73. public function favoritesstore()
  74. {
  75. $fav_id = intval(input('param.fid'));
  76. if ($fav_id <= 0) {
  77. echo json_encode(array('done' => false, 'msg' => lang('favorite_collect_fail')));
  78. die;
  79. }
  80. $favorites_model = model('favorites');
  81. //判断是否已经收藏
  82. $favorites_info = $favorites_model->getOneFavorites(array(
  83. 'fav_id' => "$fav_id", 'fav_type' => 'store',
  84. 'member_id' => session('member_id')
  85. ));
  86. if (!empty($favorites_info)) {
  87. echo json_encode(array(
  88. 'done' => false, 'msg' => lang('favorite_already_favorite_store')
  89. ));
  90. die;
  91. }
  92. //判断店铺是否为当前会员所有
  93. if ($fav_id == session('store_id')) {
  94. echo json_encode(array('done' => false, 'msg' => lang('favorite_no_my_store')));
  95. die;
  96. }
  97. //添加收藏
  98. $insert_arr = array();
  99. $insert_arr['member_id'] = session('member_id');
  100. $insert_arr['member_name'] = session('member_name');
  101. $insert_arr['fav_id'] = $fav_id;
  102. $insert_arr['fav_type'] = 'store';
  103. $insert_arr['fav_time'] = TIMESTAMP;
  104. $result = $favorites_model->addFavorites($insert_arr);
  105. if ($result) {
  106. //增加收藏数量
  107. $store_model = model('store');
  108. $store_model->editStore(array('store_collect' => Db::raw('store_collect+1')), array('store_id' => $fav_id));
  109. echo json_encode(array('done' => true, 'msg' => lang('favorite_collect_success')));
  110. die;
  111. } else {
  112. echo json_encode(array('done' => false, 'msg' => lang('favorite_collect_fail')));
  113. die;
  114. }
  115. }
  116. /**
  117. * 商品收藏列表
  118. *
  119. * @param
  120. * @return
  121. */
  122. public function fglist()
  123. {
  124. $favorites_model = model('favorites');
  125. $show_type = 'favorites_goods_picshowlist'; //默认为图片横向显示
  126. $show = input('param.show');
  127. $store_array = array(
  128. 'list' => 'favorites_goods_index', 'pic' => 'favorites_goods_picshowlist',
  129. 'store' => 'favorites_goods_shoplist'
  130. );
  131. if (array_key_exists($show, $store_array))
  132. $show_type = $store_array[$show];
  133. $favorites_list = $favorites_model->getGoodsFavoritesList(array(array('member_id', '=', session('member_id'))), '*', 20);
  134. View::assign('show_page', $favorites_model->page_info->render());
  135. $store_favorites = array(); //店铺收藏信息
  136. $store_goods_list = array(); //店铺为分组的商品
  137. if (!empty($favorites_list) && is_array($favorites_list)) {
  138. $favorites_id = array(); //收藏的商品编号
  139. foreach ($favorites_list as $key => $favorites) {
  140. $fav_id = $favorites['fav_id'];
  141. $favorites_id[] = $favorites['fav_id'];
  142. $favorites_key[$fav_id] = $key;
  143. }
  144. $goods_model = model('goods');
  145. $field = 'goods.goods_id,goods.goods_name,goods.store_id,goods.goods_image,goods.goods_price,goods.evaluation_count,goods.goods_salenum,goods.goods_collect,store.store_name,store.member_id,store.member_name,store.store_qq,store.store_ww';
  146. $goods_list = $goods_model->getGoodsStoreList(array(array('goods_id', 'in', $favorites_id)), $field);
  147. $store_array = array(); //店铺编号
  148. if (!empty($goods_list) && is_array($goods_list)) {
  149. foreach ($goods_list as $key => $fav) {
  150. $fav_id = $fav['goods_id'];
  151. $fav['goods_member_id'] = $fav['member_id'];
  152. $key = $favorites_key[$fav_id];
  153. $favorites_list[$key]['goods'] = $fav;
  154. $store_id = $fav['store_id'];
  155. if (!in_array($store_id, $store_array))
  156. $store_array[] = $store_id;
  157. $store_goods_list[$store_id][] = $favorites_list[$key];
  158. }
  159. }
  160. if (!empty($store_array) && is_array($store_array)) {
  161. $store_list = $favorites_model->getStoreFavoritesList(array(
  162. array('member_id', '=', session('member_id')),
  163. array('fav_id', 'in', $store_array)
  164. ));
  165. if (!empty($store_list) && is_array($store_list)) {
  166. foreach ($store_list as $key => $val) {
  167. $store_id = $val['fav_id'];
  168. $store_favorites[] = $store_id;
  169. }
  170. }
  171. }
  172. }
  173. $this->setMemberCurMenu('member_favorites');
  174. $this->setMemberCurItem('fav_goods');
  175. View::assign('favorites_list', $favorites_list);
  176. View::assign('store_favorites', $store_favorites);
  177. View::assign('store_goods_list', $store_goods_list);
  178. return View::fetch($this->template_dir . $show_type);
  179. }
  180. /**
  181. * 店铺收藏列表
  182. *
  183. * @param
  184. * @return
  185. */
  186. public function fslist()
  187. {
  188. $favorites_model = model('favorites');
  189. $favorites_list = $favorites_model->getStoreFavoritesList(array(array('member_id', '=', session('member_id'))), '*', 10);
  190. if (!empty($favorites_list) && is_array($favorites_list)) {
  191. $favorites_id = array(); //收藏的店铺编号
  192. foreach ($favorites_list as $key => $favorites) {
  193. $fav_id = $favorites['fav_id'];
  194. $favorites_id[] = $favorites['fav_id'];
  195. $favorites_key[$fav_id] = $key;
  196. }
  197. $store_model = model('store');
  198. $store_list = $store_model->getStoreList(array(array('store_id', 'in', $favorites_id)));
  199. if (!empty($store_list) && is_array($store_list)) {
  200. foreach ($store_list as $key => $fav) {
  201. $fav_id = $fav['store_id'];
  202. $key = $favorites_key[$fav_id];
  203. $favorites_list[$key]['store'] = $fav;
  204. }
  205. }
  206. }
  207. $this->setMemberCurMenu('membergavorites');
  208. $this->setMemberCurItem('fav_store');
  209. View::assign('favorites_list', $favorites_list);
  210. View::assign('show_page', $favorites_model->page_info->render());
  211. return View::fetch($this->template_dir . "favorites_store_index");
  212. }
  213. /**
  214. * 删除收藏
  215. *
  216. * @param
  217. * @return
  218. */
  219. public function delfavorites()
  220. {
  221. if (!input('param.fav_id') || !input('param.type')) {
  222. ds_json_encode(10001, lang('favorite_del_fail'));
  223. }
  224. if (!preg_match_all('/^[0-9,]+$/', input('param.fav_id'), $matches)) {
  225. ds_json_encode(10001, lang('param_error'));
  226. }
  227. $fav_id = trim(input('param.fav_id'), ',');
  228. if (!in_array(input('param.type'), array('goods', 'store'))) {
  229. ds_json_encode(10001, lang('param_error'));
  230. }
  231. $type = input('param.type');
  232. $favorites_model = model('favorites');
  233. $fav_arr = explode(',', $fav_id);
  234. if (!empty($fav_arr) && is_array($fav_arr)) {
  235. $favorites_list = $favorites_model->getFavoritesList(array(array('fav_id', 'in', $fav_arr), array('fav_type', '=', "$type"), array('member_id', '=', session('member_id'))));
  236. if (!empty($favorites_list) && is_array($favorites_list)) {
  237. $fav_arr = array();
  238. foreach ($favorites_list as $k => $v) {
  239. $fav_arr[] = $v['fav_id'];
  240. }
  241. $result = $favorites_model->delFavorites(array(
  242. array('fav_id', 'in', $fav_arr), array('fav_type', '=', "$type"),
  243. array('member_id', '=', session('member_id'))
  244. ));
  245. if (!empty($fav_arr) && $result) {
  246. //更新商品收藏数量
  247. if ($type == 'store') {
  248. $store_model = model('store');
  249. $store_model->editStore(array('store_collect' => Db::raw('store_collect-1'),), array(array('store_id', '=', $fav_id), array('store_collect', '>', 0),));
  250. } else {
  251. $goods_model = model('goods');
  252. $goods_model->editGoodsById(array('goods_collect' => Db::raw('goods_collect-1')), $fav_arr);
  253. }
  254. ds_json_encode(10000, lang('favorite_del_success'));
  255. }
  256. } else {
  257. ds_json_encode(10001, lang('favorite_del_fail'));
  258. }
  259. } else {
  260. ds_json_encode(10001, lang('favorite_del_fail'));
  261. }
  262. }
  263. /**
  264. * 店铺新上架的商品列表
  265. *
  266. * @param
  267. * @return
  268. */
  269. public function store_goods()
  270. {
  271. $store_id = intval(input('param.store_id'));
  272. if ($store_id > 0) {
  273. $condition = array();
  274. $add_time_from = TIMESTAMP - 60 * 60 * 24 * 30; //30天
  275. $condition[] = array('store_id', '=', $store_id);
  276. $condition[] = array('goods_addtime', 'between', $add_time_from . ',' . TIMESTAMP);
  277. $goods_model = model('goods');
  278. $goods_list = $goods_model->getGoodsOnlineList($condition, 'goods_id,goods_name,store_id,goods_image,goods_price', 0, 'goods_id desc', 50);
  279. View::assign('goods_list', $goods_list);
  280. echo View::fetch($this->template_dir . 'favorites_store_goods');
  281. }
  282. }
  283. /**
  284. * 用户中心右边,小导航
  285. *
  286. * @param string $menu_type 导航类型
  287. * @param string $menu_key 当前导航的menu_key
  288. * @return
  289. */
  290. protected function getMemberItemList()
  291. {
  292. $menu_array = array(
  293. array(
  294. 'name' => 'fav_goods', 'text' => lang('ds_member_path_collect_list'),
  295. 'url' => (string)url('Memberfavorites/fglist')
  296. ), array(
  297. 'name' => 'fav_store', 'text' => lang('ds_member_path_collect_store'),
  298. 'url' => (string)url('Memberfavorites/fslist')
  299. )
  300. );
  301. if (config('ds_config.flea_isuse')) {
  302. $menu_array[] = array(
  303. 'name' => 'fav_flea', 'text' => lang('collection_idle'), 'url' => (string)url('Memberflea/favorites')
  304. );
  305. }
  306. return $menu_array;
  307. }
  308. }