Cart.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. *
  8. * ============================================================================
  9. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  10. * 网站地址: https://www.valimart.net/
  11. * ----------------------------------------------------------------------------
  12. *
  13. * ============================================================================
  14. * 控制器
  15. */
  16. class Cart extends BaseMember {
  17. public function initialize() {
  18. parent::initialize();
  19. Lang::load(base_path() . 'home/lang/'.config('lang.default_lang').'/cart.lang.php');
  20. }
  21. function index()
  22. {
  23. $cart_model = model('cart');
  24. $logic_buy_1 = model('buy_1','logic');
  25. //购物车列表
  26. $cart_list = $cart_model->getCartList('db', array('buyer_id' => session('member_id')));
  27. //购物车列表 [得到最新商品属性及促销信息]
  28. $cart_list = $logic_buy_1->getGoodsCartList($cart_list);
  29. //购物车商品以店铺ID分组显示,并计算商品小计,店铺小计与总价由JS计算得出
  30. $store_cart_list = array();
  31. foreach ($cart_list as $cart) {
  32. $cart['goods_total'] = ds_price_format($cart['goods_price'] * $cart['goods_num']);
  33. $store_cart_list[$cart['store_id']][] = $cart;
  34. }
  35. View::assign('store_cart_list', $store_cart_list);
  36. //店铺信息
  37. $store_list = model('store')->getStoreMemberIDList(array_keys($store_cart_list));
  38. View::assign('store_list', $store_list);
  39. //取得店铺级活动 - 可用的满即送活动
  40. $mansong_rule_list = $logic_buy_1->getMansongruleList(array_keys($store_cart_list));
  41. View::assign('mansong_rule_list', $mansong_rule_list);
  42. //取得哪些店铺有满免运费活动
  43. $free_freight_list = $logic_buy_1->getFreeFreightActiveList(array_keys($store_cart_list));
  44. View::assign('free_freight_list', $free_freight_list);
  45. //标识 购买流程执行第几步
  46. View::assign('buy_step', 'step1');
  47. return View::fetch(empty($cart_list) ? $this->template_dir .'cart_empty' : $this->template_dir .'cart');
  48. }
  49. /**
  50. * 异步查询购物车
  51. */
  52. public function ajax_load() {
  53. $cart_map = array(
  54. 'buyer_id' => session('member_id'),
  55. );
  56. $cart_mod=model('cart');
  57. $cart_list = $cart_mod->getCartList('db',$cart_map);
  58. $cart_array = array();
  59. $cart_all_price = 0;
  60. $cart_goods_num = 0;
  61. if (!empty($cart_list)) {
  62. foreach ($cart_list as $k => $cart) {
  63. $cart_array['list'][$k]['cart_id'] = isset($cart['cart_id'])?$cart['cart_id']:$cart['goods_id'];
  64. $cart_array['list'][$k]['goods_id'] = $cart['goods_id'];
  65. $cart_array['list'][$k]['goods_name'] = $cart['goods_name'];
  66. $cart_array['list'][$k]['goods_price'] = $cart['goods_price'];
  67. $cart_array['list'][$k]['goods_image'] = goods_thumb($cart, 240);
  68. $cart_array['list'][$k]['goods_num'] = $cart['goods_num'];
  69. $cart_array['list'][$k]['goods_url'] = (string)url('Goods/index', ['goods_id' => $cart['goods_id']]);
  70. $cart_all_price += $cart['goods_price'] * $cart['goods_num'];
  71. $cart_goods_num ++;
  72. }
  73. }
  74. $cart_array['cart_all_price'] = number_format($cart_all_price,'2');
  75. $cart_array['cart_goods_num'] = $cart_goods_num;
  76. if (input('param.type') == 'html') {
  77. View::assign('cart_list',$cart_array);
  78. echo View::fetch($this->template_dir.'cart_mini');
  79. }else{
  80. $json_data = json_encode($cart_array);
  81. exit($json_data);
  82. }
  83. }
  84. /**
  85. * 加入购物车,登录后存入购物车表
  86. * 存入COOKIE,由于COOKIE长度限制,最多保存5个商品
  87. * 未登录不能将优惠套装商品加入购物车,登录前保存的信息以goods_id为下标
  88. *
  89. */
  90. function add() {
  91. $goods_model = model('goods');
  92. $logic_buy_1 = model('buy_1','logic');
  93. $goods_id = intval(input('param.goods_id'));
  94. $quantity = intval(input('param.quantity'));
  95. $bl_id = intval(input('param.bl_id'));
  96. if (is_numeric($goods_id) && $goods_id>0) {
  97. //商品加入购物车(默认)
  98. if ($goods_id <= 0)
  99. return;
  100. $goods_info = $goods_model->getGoodsOnlineInfoAndPromotionById($goods_id);
  101. //抢购
  102. $logic_buy_1->getGroupbuyInfo($goods_info, $quantity);
  103. //批发
  104. $logic_buy_1->getWholesaleInfo($goods_info, $quantity);
  105. //秒杀
  106. $logic_buy_1->getXianshiInfo($goods_info, $quantity);
  107. //会员等级折扣
  108. $logic_buy_1->getMgdiscountInfo($goods_info);
  109. $this->_check_goods($goods_info, $quantity);
  110. } elseif (is_numeric($bl_id)&& $bl_id>0 ) {
  111. //优惠套装加入购物车(单套)
  112. if (!session('member_id')) {
  113. exit(json_encode(array('msg' => lang('please_login_first'))));
  114. }
  115. if ($bl_id <= 0)
  116. return;
  117. $pbundling_model = model('pbundling');
  118. $bl_info = $pbundling_model->getBundlingInfo(array('bl_id' => $bl_id));
  119. if (empty($bl_info) || $bl_info['bl_state'] == '0') {
  120. exit(json_encode(array('msg' => lang('recommendations_buy_separately'))));
  121. }
  122. //检查每个商品是否符合条件,并重新计算套装总价
  123. $bl_goods_list = $pbundling_model->getBundlingGoodsList(array('bl_id' => $bl_id));
  124. $goods_id_array = array();
  125. $bl_amount = 0;
  126. foreach ($bl_goods_list as $goods) {
  127. $goods_id_array[] = $goods['goods_id'];
  128. $bl_amount += $goods['blgoods_price'];
  129. }
  130. $goods_model = model('goods');
  131. $goods_list = $goods_model->getGoodsOnlineListAndPromotionByIdArray($goods_id_array);
  132. foreach ($goods_list as $goods) {
  133. $this->_check_goods($goods, 1);
  134. }
  135. //优惠套装作为一条记录插入购物车,图片取套装内的第一个商品图
  136. $goods_info = array();
  137. $goods_info['store_id'] = $bl_info['store_id'];
  138. $goods_info['goods_id'] = $goods_list[0]['goods_id'];
  139. $goods_info['goods_name'] = $bl_info['bl_name'];
  140. $goods_info['goods_price'] = $bl_amount;
  141. $goods_info['goods_num'] = 1;
  142. $goods_info['goods_image'] = $goods_list[0]['goods_image'];
  143. $goods_info['store_name'] = $bl_info['store_name'];
  144. $goods_info['bl_id'] = $bl_id;
  145. $quantity = 1;
  146. }
  147. $save_type = 'db';
  148. $goods_info['buyer_id'] = session('member_id');
  149. $cart_model = model('cart');
  150. $insert = $cart_model->addCart($goods_info, $save_type, $quantity);
  151. if ($insert) {
  152. $data = array('state' => 'true', 'num' => $cart_model->cart_goods_num, 'amount' => ds_price_format($cart_model->cart_all_price));
  153. } else {
  154. $data = array('state' => 'false','message'=>$cart_model->error_message);
  155. }
  156. exit(json_encode($data));
  157. }
  158. /**
  159. * 推荐组合加入购物车
  160. */
  161. public function add_comb() {
  162. if (!preg_match('/^[\d|]+$/', input('get.goods_ids'))) {
  163. exit(json_encode(array('state' => 'false')));
  164. }
  165. $logic_buy_1 = model('buy_1','logic');
  166. if (!session('member_id')) {
  167. exit(json_encode(array('msg' => lang('please_login_first'))));
  168. }
  169. $goods_id_array = explode('|', input('get.goods_ids'));
  170. $goods_model = model('goods');
  171. $goods_list = $goods_model->getGoodsOnlineListAndPromotionByIdArray($goods_id_array);
  172. if(empty($goods_list)){
  173. exit(json_encode(array('state' => 'false')));
  174. }
  175. foreach ($goods_list as $goods) {
  176. $this->_check_goods($goods, 1);
  177. }
  178. //抢购
  179. $logic_buy_1->getGroupbuyCartList($goods_list);
  180. //秒杀
  181. $logic_buy_1->getXianshiCartList($goods_list);
  182. $cart_model = model('cart');
  183. foreach ($goods_list as $goods_info) {
  184. $cart_info = array();
  185. $cart_info['store_id'] = $goods_info['store_id'];
  186. $cart_info['goods_id'] = $goods_info['goods_id'];
  187. $cart_info['goods_name'] = $goods_info['goods_name'];
  188. $cart_info['goods_price'] = $goods_info['goods_price'];
  189. $cart_info['goods_num'] = 1;
  190. $cart_info['goods_image'] = $goods_info['goods_image'];
  191. $cart_info['store_name'] = $goods_info['store_name'];
  192. $quantity = 1;
  193. $save_type = 'db';
  194. $cart_info['buyer_id'] = session('member_id');
  195. $insert = $cart_model->addCart($cart_info, $save_type, $quantity);
  196. if ($insert) {
  197. //购物车商品种数记入cookie
  198. cookie('cart_goods_num', $cart_model->cart_goods_num, 2 * 3600);
  199. $data = array('state' => 'true', 'num' => $cart_model->cart_goods_num, 'amount' => ds_price_format($cart_model->cart_all_price));
  200. } else {
  201. $data = array('state' => 'false');
  202. exit(json_encode($data));
  203. }
  204. }
  205. exit(json_encode($data));
  206. }
  207. /**
  208. * 检查商品是否符合加入购物车条件
  209. * @param unknown $goods
  210. * @param number $quantity
  211. */
  212. private function _check_goods($goods_info, $quantity) {
  213. if (empty($quantity)) {
  214. exit(json_encode(array('msg' => lang('param_error'))));
  215. }
  216. if (empty($goods_info)) {
  217. exit(json_encode(array('msg' => lang('cart_add_goods_not_exists'))));
  218. }
  219. if ($goods_info['store_id'] == session('store_id')) {
  220. exit(json_encode(array('msg' => lang('cart_add_cannot_buy'))));
  221. }
  222. if (intval($goods_info['goods_storage']) < 1) {
  223. exit(json_encode(array('msg' => lang('cart_add_stock_shortage'))));
  224. }
  225. if (intval($goods_info['goods_storage']) < $quantity) {
  226. exit(json_encode(array('msg' => lang('cart_add_too_much'))));
  227. }
  228. if ($goods_info['is_virtual'] || $goods_info['is_goodsfcode']) {
  229. exit(json_encode(array('msg' => lang('please_purchase_directly'))));
  230. }
  231. }
  232. /**
  233. * 购物车更新商品数量
  234. */
  235. public function update() {
  236. $cart_id = intval(abs(input('get.cart_id')));
  237. $quantity = intval(abs(input('get.quantity')));
  238. if (empty($cart_id) || empty($quantity)) {
  239. exit(json_encode(array('msg' => lang('cart_update_buy_fail'))));
  240. }
  241. $cart_model = model('cart');
  242. $goods_model = model('goods');
  243. $logic_buy_1 = model('buy_1','logic');
  244. //存放返回信息
  245. $return = array();
  246. $cart_info = $cart_model->getCartInfo(array('cart_id' => $cart_id, 'buyer_id' => session('member_id')));
  247. if ($cart_info['bl_id'] == '0') {
  248. //普通商品
  249. $goods_id = intval($cart_info['goods_id']);
  250. $goods_info = $logic_buy_1->getGoodsOnlineInfo($goods_id, $quantity);
  251. if (empty($goods_info)) {
  252. $return['state'] = 'invalid';
  253. $return['msg'] = lang('merchandise_off_shelves');
  254. $return['subtotal'] = 0;
  255. $condition = array();
  256. $condition[] = array('buyer_id','=',session('member_id'));
  257. $condition[] = array('cart_id','in',array($cart_id));
  258. model('cart')->delCart('db', $condition,session('member_id'));
  259. exit(json_encode($return));
  260. }
  261. // //抢购
  262. // $logic_buy_1->getGroupbuyInfo($goods_info, $quantity);
  263. // //秒杀
  264. // $logic_buy_1->getXianshiInfo($goods_info, $quantity);
  265. $quantity = $goods_info['goods_num'];
  266. if (intval($goods_info['goods_storage']) < $quantity) {
  267. $return['state'] = 'shortage';
  268. $return['msg'] = lang('cart_add_too_much');
  269. $return['goods_num'] = $goods_info['goods_num'];
  270. $return['goods_price'] = $goods_info['goods_price'];
  271. $return['subtotal'] = $goods_info['goods_price'] * $quantity;
  272. $cart_model->editCart(array('goods_num' => $goods_info['goods_storage']), array('cart_id' => $cart_id, 'buyer_id' => session('member_id')),session('member_id'));
  273. exit(json_encode($return));
  274. }
  275. } else {
  276. //优惠套装商品
  277. $pbundling_model = model('pbundling');
  278. $bl_goods_list = $pbundling_model->getBundlingGoodsList(array('bl_id' => $cart_info['bl_id']));
  279. $goods_id_array = array();
  280. foreach ($bl_goods_list as $goods) {
  281. $goods_id_array[] = $goods['goods_id'];
  282. }
  283. $goods_list = $goods_model->getGoodsOnlineListAndPromotionByIdArray($goods_id_array);
  284. //如果其中有商品下架,删除
  285. if (count($goods_list) != count($goods_id_array)) {
  286. $return['state'] = 'invalid';
  287. $return['msg'] = lang('wheatsuit_no_longer_valid');
  288. $return['subtotal'] = 0;
  289. $condition = array();
  290. $condition[] = array('buyer_id','=',session('member_id'));
  291. $condition[] = array('cart_id','in',array($cart_id));
  292. model('cart')->delCart('db', $condition,session('member_id'));
  293. exit(json_encode($return));
  294. }
  295. //如果有商品库存不足,更新购买数量到目前最大库存
  296. foreach ($goods_list as $goods_info) {
  297. if ($quantity > $goods_info['goods_storage']) {
  298. $return['state'] = 'shortage';
  299. $return['msg'] = lang('preferential_suit_understock');
  300. $return['goods_num'] = $goods_info['goods_storage'];
  301. $return['goods_price'] = $cart_info['goods_price'];
  302. $return['subtotal'] = $cart_info['goods_price'] * $quantity;
  303. $cart_model->editCart(array('goods_num' => $goods_info['goods_storage']), array('cart_id' => $cart_id, 'buyer_id' => session('member_id')),session('member_id'));
  304. exit(json_encode($return));
  305. break;
  306. }
  307. }
  308. $goods_info['goods_price'] = $cart_info['goods_price'];
  309. }
  310. $data = array();
  311. $data['goods_num'] = $quantity;
  312. $data['goods_price'] = $goods_info['goods_price'];
  313. $update = $cart_model->editCart($data, array('cart_id' => $cart_id, 'buyer_id' => session('member_id')),session('member_id'));
  314. if ($update) {
  315. $return = array();
  316. $return['state'] = 'true';
  317. $return['subtotal'] = $goods_info['goods_price'] * $quantity;
  318. $return['goods_price'] = $goods_info['goods_price'];
  319. $return['goods_num'] = $quantity;
  320. } else {
  321. $return = array('msg' => lang('cart_update_buy_fail'));
  322. }
  323. exit(json_encode($return));
  324. }
  325. /**
  326. * 购物车删除单个商品,未登录前使用cart_id即为goods_id
  327. */
  328. public function del() {
  329. $cart_id = intval(input('get.cart_id'));
  330. if ($cart_id < 0)
  331. return;
  332. $cart_model = model('cart');
  333. $data = array();
  334. //登录状态下删除数据库内容
  335. $delete = $cart_model->delCart('db', array('cart_id' => $cart_id, 'buyer_id' => session('member_id')),session('member_id'));
  336. if ($delete) {
  337. $data['state'] = 'true';
  338. $data['quantity'] = $cart_model->cart_goods_num;
  339. $data['amount'] = $cart_model->cart_all_price;
  340. } else {
  341. $data['msg'] = lang('cart_drop_del_fail');
  342. }
  343. cookie('cart_goods_num', $cart_model->cart_goods_num, 2 * 3600);
  344. $json_data = json_encode($data);
  345. // if (isset($_GET['callback'])) {
  346. // $json_data = $_GET['callback'] == '?' ? '(' . $json_data . ')' : $_GET['callback'] . "($json_data);";
  347. // }
  348. exit($json_data);
  349. }
  350. }