Cart.php 17 KB

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