Memberfavorites.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. * DSMall多用户商城
  9. * ============================================================================
  10. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  11. * 网站地址: http://www.csdeshang.com
  12. * ----------------------------------------------------------------------------
  13. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  14. * 不允许对程序代码以任何形式任何目的的再发布。
  15. * ============================================================================
  16. * 控制器
  17. */
  18. class Memberfavorites extends BaseMember {
  19. public function initialize() {
  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. $fav_id = intval(input('param.fid'));
  28. if ($fav_id <= 0) {
  29. echo json_encode(array('done' => false, 'msg' => lang('favorite_collect_fail')));
  30. die;
  31. }
  32. $favorites_model = model('favorites');
  33. //判断是否已经收藏
  34. $favorites_info = $favorites_model->getOneFavorites(array(
  35. 'fav_id' => "$fav_id", 'fav_type' => 'goods',
  36. 'member_id' => session('member_id')
  37. ));
  38. if (!empty($favorites_info)) {
  39. echo json_encode(array(
  40. 'done' => false, 'msg' => lang('favorite_already_favorite_goods')
  41. ));
  42. die;
  43. }
  44. //判断商品是否为当前会员所有
  45. $goods_model = model('goods');
  46. $goods_info = $goods_model->getGoodsInfoByID($fav_id);
  47. if ($goods_info['store_id'] == session('store_id')) {
  48. echo json_encode(array('done' => false, 'msg' => lang('favorite_no_my_product')));
  49. die;
  50. }
  51. //添加收藏
  52. $insert_arr = array();
  53. $insert_arr['member_id'] = session('member_id');
  54. $insert_arr['member_name'] = session('member_name');
  55. $insert_arr['fav_id'] = $fav_id;
  56. $insert_arr['fav_type'] = 'goods';
  57. $insert_arr['fav_time'] = TIMESTAMP;
  58. $result = $favorites_model->addFavorites($insert_arr);
  59. if ($result) {
  60. //增加收藏数量
  61. $goods_model->editGoodsById(array('goods_collect' => Db::raw('goods_collect+1')), $fav_id);
  62. echo json_encode(array('done' => true, 'msg' => lang('favorite_collect_success')));
  63. die;
  64. } else {
  65. echo json_encode(array('done' => false, 'msg' => lang('favorite_collect_fail')));
  66. die;
  67. }
  68. }
  69. /**
  70. * 增加店铺收藏
  71. */
  72. public function favoritesstore() {
  73. $fav_id = intval(input('param.fid'));
  74. if ($fav_id <= 0) {
  75. echo json_encode(array('done' => false, 'msg' => lang('favorite_collect_fail')));
  76. die;
  77. }
  78. $favorites_model = model('favorites');
  79. //判断是否已经收藏
  80. $favorites_info = $favorites_model->getOneFavorites(array(
  81. 'fav_id' => "$fav_id", 'fav_type' => 'store',
  82. 'member_id' => session('member_id')
  83. ));
  84. if (!empty($favorites_info)) {
  85. echo json_encode(array(
  86. 'done' => false, 'msg' => lang('favorite_already_favorite_store')
  87. ));
  88. die;
  89. }
  90. //判断店铺是否为当前会员所有
  91. if ($fav_id == session('store_id')) {
  92. echo json_encode(array('done' => false, 'msg' => lang('favorite_no_my_store')));
  93. die;
  94. }
  95. //添加收藏
  96. $insert_arr = array();
  97. $insert_arr['member_id'] = session('member_id');
  98. $insert_arr['member_name'] = session('member_name');
  99. $insert_arr['fav_id'] = $fav_id;
  100. $insert_arr['fav_type'] = 'store';
  101. $insert_arr['fav_time'] = TIMESTAMP;
  102. $result = $favorites_model->addFavorites($insert_arr);
  103. if ($result) {
  104. //增加收藏数量
  105. $store_model = model('store');
  106. $store_model->editStore(array('store_collect' => Db::raw('store_collect+1')), array('store_id' => $fav_id));
  107. echo json_encode(array('done' => true, 'msg' => lang('favorite_collect_success')));
  108. die;
  109. } else {
  110. echo json_encode(array('done' => false, 'msg' => lang('favorite_collect_fail')));
  111. die;
  112. }
  113. }
  114. /**
  115. * 商品收藏列表
  116. *
  117. * @param
  118. * @return
  119. */
  120. public function fglist() {
  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. $favorites_model = model('favorites');
  185. $favorites_list = $favorites_model->getStoreFavoritesList(array(array('member_id' ,'=', session('member_id'))), '*', 10);
  186. if (!empty($favorites_list) && is_array($favorites_list)) {
  187. $favorites_id = array(); //收藏的店铺编号
  188. foreach ($favorites_list as $key => $favorites) {
  189. $fav_id = $favorites['fav_id'];
  190. $favorites_id[] = $favorites['fav_id'];
  191. $favorites_key[$fav_id] = $key;
  192. }
  193. $store_model = model('store');
  194. $store_list = $store_model->getStoreList(array(array('store_id','in', $favorites_id)));
  195. if (!empty($store_list) && is_array($store_list)) {
  196. foreach ($store_list as $key => $fav) {
  197. $fav_id = $fav['store_id'];
  198. $key = $favorites_key[$fav_id];
  199. $favorites_list[$key]['store'] = $fav;
  200. }
  201. }
  202. }
  203. $this->setMemberCurMenu('membergavorites');
  204. $this->setMemberCurItem('fav_store');
  205. View::assign('favorites_list', $favorites_list);
  206. View::assign('show_page', $favorites_model->page_info->render());
  207. return View::fetch($this->template_dir . "favorites_store_index");
  208. }
  209. /**
  210. * 删除收藏
  211. *
  212. * @param
  213. * @return
  214. */
  215. public function delfavorites() {
  216. if (!input('param.fav_id') || !input('param.type')) {
  217. ds_json_encode(10001,lang('favorite_del_fail'));
  218. }
  219. if (!preg_match_all('/^[0-9,]+$/', input('param.fav_id'), $matches)) {
  220. ds_json_encode(10001,lang('param_error'));
  221. }
  222. $fav_id = trim(input('param.fav_id'), ',');
  223. if (!in_array(input('param.type'), array('goods', 'store'))) {
  224. ds_json_encode(10001,lang('param_error'));
  225. }
  226. $type = input('param.type');
  227. $favorites_model = model('favorites');
  228. $fav_arr = explode(',', $fav_id);
  229. if (!empty($fav_arr) && is_array($fav_arr)) {
  230. $favorites_list = $favorites_model->getFavoritesList(array(array('fav_id','in', $fav_arr),array('fav_type' ,'=', "$type"),array('member_id' ,'=', session('member_id'))));
  231. if (!empty($favorites_list) && is_array($favorites_list)) {
  232. $fav_arr = array();
  233. foreach ($favorites_list as $k => $v) {
  234. $fav_arr[] = $v['fav_id'];
  235. }
  236. $result = $favorites_model->delFavorites(array(
  237. array('fav_id','in', $fav_arr), array('fav_type' ,'=', "$type"),
  238. array('member_id' ,'=', session('member_id'))
  239. ));
  240. if (!empty($fav_arr) && $result) {
  241. //更新商品收藏数量
  242. if ($type == 'store'){
  243. $store_model = model('store');
  244. $store_model->editStore(array('store_collect' => Db::raw('store_collect-1'),), array(array('store_id' ,'=', $fav_id), array('store_collect','>', 0),));
  245. }else{
  246. $goods_model = model('goods');
  247. $goods_model->editGoodsById(array('goods_collect' => Db::raw('goods_collect-1')), $fav_arr);
  248. }
  249. ds_json_encode(10000,lang('favorite_del_success'));
  250. }
  251. } else {
  252. ds_json_encode(10001,lang('favorite_del_fail'));
  253. }
  254. } else {
  255. ds_json_encode(10001,lang('favorite_del_fail'));
  256. }
  257. }
  258. /**
  259. * 店铺新上架的商品列表
  260. *
  261. * @param
  262. * @return
  263. */
  264. public function store_goods() {
  265. $store_id = intval(input('param.store_id'));
  266. if ($store_id > 0) {
  267. $condition = array();
  268. $add_time_from = TIMESTAMP - 60 * 60 * 24 * 30; //30天
  269. $condition[] = array('store_id','=',$store_id);
  270. $condition[] = array('goods_addtime','between',$add_time_from . ',' . TIMESTAMP);
  271. $goods_model = model('goods');
  272. $goods_list = $goods_model->getGoodsOnlineList($condition, 'goods_id,goods_name,store_id,goods_image,goods_price', 0, 'goods_id desc', 50);
  273. View::assign('goods_list', $goods_list);
  274. echo View::fetch($this->template_dir . 'favorites_store_goods');
  275. }
  276. }
  277. /**
  278. * 用户中心右边,小导航
  279. *
  280. * @param string $menu_type 导航类型
  281. * @param string $menu_key 当前导航的menu_key
  282. * @return
  283. */
  284. protected function getMemberItemList() {
  285. $menu_array = array(
  286. array(
  287. 'name' => 'fav_goods', 'text' => lang('ds_member_path_collect_list'),
  288. 'url' => (string)url('Memberfavorites/fglist')
  289. ), array(
  290. 'name' => 'fav_store', 'text' => lang('ds_member_path_collect_store'),
  291. 'url' => (string)url('Memberfavorites/fslist')
  292. )
  293. );
  294. if(config('ds_config.flea_isuse')){
  295. $menu_array[]=array(
  296. 'name' => 'fav_flea', 'text' => lang('collection_idle'), 'url' => (string)url('Memberflea/favorites')
  297. );
  298. }
  299. return $menu_array;
  300. }
  301. }