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