Memberfavorites.php 13 KB

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