...
|
...
|
@@ -41,6 +41,89 @@ class CartController extends MemberbaseController { |
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改购物车商品数量
|
|
|
* @param id 商品id
|
|
|
* @param sort 类型(增加/减少)
|
|
|
*/
|
|
|
public function changeNum() {
|
|
|
if(IS_AJAX) {
|
|
|
$user_id = sp_get_current_userid();
|
|
|
$id = I('post.id',0,'intval');
|
|
|
$sort = I('post.sort');
|
|
|
$num = I('post.num',0,'intval');
|
|
|
if($sort == 'reduce') {
|
|
|
$num = -1;
|
|
|
}
|
|
|
// 判断传参是否正常
|
|
|
if(!$num) {
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'请输入正确的商品数量'));
|
|
|
}
|
|
|
if($sort != 'plus' && $sort != 'reduce' && $sort != 'change') {
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'参数错误'));
|
|
|
}
|
|
|
// 判断购物车及商品数据
|
|
|
$info = $this->cart_model->getOneByUser($user_id,$id);
|
|
|
if(!$info) {
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'购物车不存在该商品'));
|
|
|
}
|
|
|
if($sort == 'plus' || $sort == 'reduce') {
|
|
|
if(($info['num']+$num)>$info['goods_num']) {
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'商品库存不足'));
|
|
|
}
|
|
|
$info['num'] += $num;
|
|
|
}
|
|
|
if($sort == 'change') {
|
|
|
if(($num)>$info['goods_num']) {
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'商品库存不足'));
|
|
|
}
|
|
|
$info['num'] = $num;
|
|
|
}
|
|
|
// 更新购物车数据(价格及数量)
|
|
|
$this->cart_model->where(array('user_id'=>$user_id,'goods_id'=>$id))->save(array('num'=>$info['num'],'price'=>$info['goods_price']));
|
|
|
$info['amount'] = $info['num'] * $info['goods_price'];
|
|
|
$info['sort'] = $sort;
|
|
|
$this->ajaxReturn(array('status'=>true,'msg'=>'修改成功','data'=>$info));
|
|
|
} else {
|
|
|
$this->error('非法操作');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除(去付款)购物车商品
|
|
|
* @param ids 要删除(去付款)的商品
|
|
|
*/
|
|
|
public function del() {
|
|
|
if(IS_AJAX) {
|
|
|
$user_id = sp_get_current_userid();
|
|
|
$ids = I('post.ids');
|
|
|
$sort = I('post.sort');
|
|
|
$redirect_url = '';
|
|
|
if($sort != 'delete' && $sort != 'topay') {
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'参数错误'));
|
|
|
}
|
|
|
if($sort == 'delete') {
|
|
|
$name = '删除';
|
|
|
}
|
|
|
if($sort == 'topay') {
|
|
|
$redirect_url = U('User/Cart/confirm',array('ids'=>implode(',',$ids)));
|
|
|
$name = '购买';
|
|
|
}
|
|
|
if(!$ids) {
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'请选择要'.$name.'的商品'));
|
|
|
}
|
|
|
if($sort == 'delete') {
|
|
|
$result = $this->cart_model->where(array('goods_id'=>array('in',$ids),'user_id'=>$user_id))->delete();
|
|
|
if(!$result) {
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'删除失败'));
|
|
|
}
|
|
|
}
|
|
|
$this->ajaxReturn(array('status'=>true,'msg'=>$name.'成功','data'=>$redirect_url));
|
|
|
} else {
|
|
|
$this->error('非法操作');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 确认订单页面
|
|
|
*/
|
|
|
public function confirm() {
|
...
|
...
|
@@ -55,11 +138,73 @@ class CartController extends MemberbaseController { |
|
|
$cartList[$k]['amount'] = $v['goods_price']*$v['num'];
|
|
|
$count_amount += $cartList[$k]['amount'];
|
|
|
}
|
|
|
$this->assign('list',$cartList);
|
|
|
$this->assign('cartList',$cartList);
|
|
|
$this->assign('count_amount', $count_amount);
|
|
|
$this->display();
|
|
|
} else {
|
|
|
$this->error('非法操作');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 确认订单
|
|
|
* @param address_id 收货地址信息
|
|
|
* @param ids 购买的商品id
|
|
|
*/
|
|
|
public function createOrder() {
|
|
|
if(IS_AJAX) {
|
|
|
$user_id = $info['user_id'] = sp_get_current_userid();
|
|
|
if(!$user_id) {
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'未登录'));
|
|
|
}
|
|
|
$info['address_id'] = $address_id = I('post.address_id',0,'intval');
|
|
|
$ids = explode(',',I('post.ids'));
|
|
|
if(!$ids) {
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'参数错误'));
|
|
|
}
|
|
|
$list = $this->cart_model->where(array('goods_id'=>array('in',$ids),'user_id'=>$user_id))->select();
|
|
|
if(!$list) {
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'购物车出现变化,请重新购买'));
|
|
|
}
|
|
|
$count_amount = 0;
|
|
|
foreach($list as $k=>$v) {
|
|
|
$count_amount += $v['num'] * $v['goods_price'];
|
|
|
}
|
|
|
// 生成订单
|
|
|
M('Order')->startTrans();
|
|
|
$order_model = D('Common/Order');
|
|
|
$order_detail_model = D('Common/OrderDetail');
|
|
|
$info['order_sn'] = $detail['order_sn'] = sp_get_order_sn();
|
|
|
$info['status'] = 1;
|
|
|
$info['price_count'] = $count_amount;
|
|
|
$info['ctime'] = time();
|
|
|
if(!$order_model->create($info)) {
|
|
|
M('Order')->rollback();
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>$order_model->getError()));
|
|
|
}
|
|
|
$order_id = $order_model->add($info);
|
|
|
if(!$order_id) {
|
|
|
M('Order')->rollback();
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'添加订单失败'));
|
|
|
}
|
|
|
foreach($list as $k=>$v) {
|
|
|
$detail['goods_id'] = $v['goods_id'];
|
|
|
$detail['goods_price'] = $v['goods_price'];
|
|
|
$detail['num'] = $v['num'];
|
|
|
$detail['amount'] = $v['num'] * $v['goods_price'];
|
|
|
$detail['ctime'] = time();
|
|
|
if(!$order_detail_model->create($detail)) {
|
|
|
M('Order')->rollback();
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>$order_detail_model->getError()));
|
|
|
}
|
|
|
if(!$order_detail_model->create($detail)) {
|
|
|
M('Order')->rollback();
|
|
|
$this->ajaxReturn(array('status'=>false,'msg'=>'添加订单失败'));
|
|
|
}
|
|
|
}
|
|
|
$this->ajaxReturn(array('status'=>true,'msg'=>'添加订单成功','data'=>$order_id));
|
|
|
} else {
|
|
|
$this->error('非法操作');
|
|
|
}
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|