Cart.php 16 KB

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