...
|
...
|
@@ -12,12 +12,14 @@ use Common\Controller\HomebaseController; |
|
|
|
|
|
class GoodsController extends HomebaseController {
|
|
|
|
|
|
protected $cart_model;
|
|
|
protected $goods_model;
|
|
|
protected $goods_brand_model;
|
|
|
protected $product_model;
|
|
|
|
|
|
function _initialize() {
|
|
|
parent::_initialize();
|
|
|
$this->cart_model = D("Common/Cart");
|
|
|
$this->goods_model = D("Common/Goods");
|
|
|
$this->goods_brand_model = D("Common/GoodsBrand");
|
|
|
$this->product_model = D("Common/Product");
|
...
|
...
|
@@ -51,4 +53,42 @@ class GoodsController extends HomebaseController { |
|
|
$this->assign($info);
|
|
|
$this->display();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加入购物车
|
|
|
*/
|
|
|
public function addCart() {
|
|
|
if(IS_AJAX) {
|
|
|
$user_id = sp_get_current_userid();
|
|
|
if(!$user_id) {
|
|
|
|
|
|
}
|
|
|
// 判断商品是否存在
|
|
|
$goods_id = I('post.id',0,'intval');
|
|
|
$info = $this->goods_model->getInfo($goods_id);
|
|
|
if(!$info) {
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'该商品不存在'));
|
|
|
}
|
|
|
$cartGoodsList = $this->cart_model->getListByUser($user_id);
|
|
|
if(in_array($goods_id,$cartGoodsList)) {
|
|
|
$result = $this->cart_model->where(array('user_id'=>$user_id,'goods_id'=>$goods_id))->setInc('num');
|
|
|
} else {
|
|
|
$cart['user_id'] = $user_id;
|
|
|
$cart['goods_id'] = $goods_id;
|
|
|
$cart['price'] = $info['goods_price'];
|
|
|
$cart['num'] = 1;
|
|
|
$cart['ctime'] = $cart['utime'] = time();
|
|
|
if(!$this->cart_model->create($cart)) {
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>$this->cart_model->getError()));
|
|
|
}
|
|
|
$result = $this->cart_model->add($cart);
|
|
|
}
|
|
|
if(!$result) {
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'添加失败'));
|
|
|
}
|
|
|
$this->ajaxReturn(array('status'=>true,'msg'=>'添加成功'));
|
|
|
} else {
|
|
|
$this->error('非法操作');
|
|
|
}
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|