作者 v_bairong06

购物车部分制作

@@ -26,7 +26,9 @@ class AppframeController extends Controller { @@ -26,7 +26,9 @@ class AppframeController extends Controller {
26 26
27 // 一级分类列表 27 // 一级分类列表
28 public function product() { 28 public function product() {
  29 + $where['parent'] = 0;
29 $product = M('Product') 30 $product = M('Product')
  31 + ->where($where)
30 ->select(); 32 ->select();
31 $this->assign('product', $product); 33 $this->assign('product', $product);
32 } 34 }
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: 29925
  5 + * Date: 2018/5/1
  6 + * Time: 17:59
  7 + */
  8 +
  9 +namespace Common\Model;
  10 +
  11 +use Common\Controller\AdminbaseController;
  12 +
  13 +class CartModel extends AdminbaseController {
  14 +
  15 + // 获取用户购物车列表
  16 + public function getListByUser($user_id) {
  17 + $where['user_id'] = $user_id;
  18 + return $this->where($where)->getField('goods_id',true);
  19 + }
  20 +}
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: 29925
  5 + * Date: 2018/5/1
  6 + * Time: 17:59
  7 + */
  8 +
  9 +namespace Portal\Controller;
  10 +
  11 +use Common\Controller\HomebaseController;
  12 +
  13 +class CartController extends HomebaseController {
  14 +
  15 + protected $cart_model;
  16 + protected $goods_model;
  17 + protected $goods_brand_model;
  18 + protected $product_model;
  19 +
  20 + function _initialize() {
  21 + parent::_initialize();
  22 + $this->cart_model = D("Common/Cart");
  23 + $this->goods_model = D("Common/Goods");
  24 + $this->goods_brand_model = D("Common/GoodsBrand");
  25 + $this->product_model = D("Common/Product");
  26 + }
  27 +
  28 + /**
  29 + * 用户购物车列表
  30 + */
  31 + public function index() {
  32 + $user_id = sp_get_current_userid();
  33 + if(!$user_id) {
  34 +
  35 + }
  36 +
  37 + }
  38 +}
@@ -12,12 +12,14 @@ use Common\Controller\HomebaseController; @@ -12,12 +12,14 @@ use Common\Controller\HomebaseController;
12 12
13 class GoodsController extends HomebaseController { 13 class GoodsController extends HomebaseController {
14 14
  15 + protected $cart_model;
15 protected $goods_model; 16 protected $goods_model;
16 protected $goods_brand_model; 17 protected $goods_brand_model;
17 protected $product_model; 18 protected $product_model;
18 19
19 function _initialize() { 20 function _initialize() {
20 parent::_initialize(); 21 parent::_initialize();
  22 + $this->cart_model = D("Common/Cart");
21 $this->goods_model = D("Common/Goods"); 23 $this->goods_model = D("Common/Goods");
22 $this->goods_brand_model = D("Common/GoodsBrand"); 24 $this->goods_brand_model = D("Common/GoodsBrand");
23 $this->product_model = D("Common/Product"); 25 $this->product_model = D("Common/Product");
@@ -51,4 +53,42 @@ class GoodsController extends HomebaseController { @@ -51,4 +53,42 @@ class GoodsController extends HomebaseController {
51 $this->assign($info); 53 $this->assign($info);
52 $this->display(); 54 $this->display();
53 } 55 }
  56 +
  57 + /**
  58 + * 加入购物车
  59 + */
  60 + public function addCart() {
  61 + if(IS_AJAX) {
  62 + $user_id = sp_get_current_userid();
  63 + if(!$user_id) {
  64 +
  65 + }
  66 + // 判断商品是否存在
  67 + $goods_id = I('post.id',0,'intval');
  68 + $info = $this->goods_model->getInfo($goods_id);
  69 + if(!$info) {
  70 + $this->ajaxReturn(array('status'=>false,'msg'=>'该商品不存在'));
  71 + }
  72 + $cartGoodsList = $this->cart_model->getListByUser($user_id);
  73 + if(in_array($goods_id,$cartGoodsList)) {
  74 + $result = $this->cart_model->where(array('user_id'=>$user_id,'goods_id'=>$goods_id))->setInc('num');
  75 + } else {
  76 + $cart['user_id'] = $user_id;
  77 + $cart['goods_id'] = $goods_id;
  78 + $cart['price'] = $info['goods_price'];
  79 + $cart['num'] = 1;
  80 + $cart['ctime'] = $cart['utime'] = time();
  81 + if(!$this->cart_model->create($cart)) {
  82 + $this->ajaxReturn(array('status'=>false,'msg'=>$this->cart_model->getError()));
  83 + }
  84 + $result = $this->cart_model->add($cart);
  85 + }
  86 + if(!$result) {
  87 + $this->ajaxReturn(array('status'=>false,'msg'=>'添加失败'));
  88 + }
  89 + $this->ajaxReturn(array('status'=>true,'msg'=>'添加成功'));
  90 + } else {
  91 + $this->error('非法操作');
  92 + }
  93 + }
54 } 94 }
@@ -35,7 +35,7 @@ @@ -35,7 +35,7 @@
35 </div> 35 </div>
36 </div> 36 </div>
37 </div> 37 </div>
38 - <notempty> 38 + <notempty name="product_son">
39 <div class="filter-row filter-bg-row"> 39 <div class="filter-row filter-bg-row">
40 <div class="filter-key">详细分类</div> 40 <div class="filter-key">详细分类</div>
41 <div class="filter-body"> 41 <div class="filter-body">