ShoppingController.php 2.1 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\ShoppingModel;
use cmf\controller\HomeBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;
use think\Session;

//购物车
class ShoppingController extends CommentController
{
//    加入购物车
    public function add(){
        $data = $this->request->param();
        $model = new ShoppingModel();
//        判断是否已添加至购物车
        $where_shopping['user_id'] = Session::get('uid');
        $where_shopping['vehicle_id'] = $data['vehicle_id'];
        $where_shopping['status'] = array('neq',9);
        $is_set = $model->where($where_shopping)->find();
        if($is_set){
            $add['id'] = $is_set['id'];
            $add['num'] = $is_set['num'] + 1;
            $res = $model->isUpdate(true)->save($add);
        }else{
            $add = $data;
            $add['user_id'] = Session::get('uid');
            $add['num'] = 1;
            $res = $model->save($add);
        }
        if($res){
            $where_count['user_id'] = Session::get('uid');
            $where_count['status'] = array('neq',9);
            $cart = $model->where($where_count)->count();
            $this->apiResponse('1','成功',$cart);
        }else{
            $this->apiResponse('0','加入购物车失败');
        }
    }

//    购物车列表
    public function index(){
        $model = new ShoppingModel();
        $where_shopping['user_id'] = Session::get('uid');
        $where_shopping['status'] = array('neq',9);
        $cart = $model->where($where_shopping)->select()->toArray();
        $this->assign('cart',$cart);
        return $this->fetch();
    }

}