CartController.php 2.5 KB
<?php
/**
 * Created by PhpStorm.
 * User: yhbr
 * Date: 2018/9/28
 * Time: 16:12
 */
namespace app\cart\controller;
use app\cart\model\CartModel;
use cmf\controller\HomeBaseController;
use think\Db;

class CartController extends HomeBaseController
{

    public function addCart()
    {
        $cart = new CartModel;
        $request = request();
        if ($request->isAjax()) {
            $gid = $request->param('gid');
            $num = $request->param('num');
            if ($cart->addToCart(session('user.id'), $gid, $num)) {
                $this->success('', '', true);
            }
        }
    }

    //直接购买
    public function purchase() {
        $request = request();
        if ($request->isAjax()) {
            session('cart.id', null);
            session('goods.id', $request->param('gid'));
            $this->success('', url('cart/zj_cart/orderConfirm'), true);
        }
    }

    public function editCart()
    {
        $request = request();
        if ($request->isAjax()) {
            $cart = [
                'id' => $request->param('id'),
                'num' => $request->param('num')
            ];
            Db::name('cart')->update($cart);
        }
    }

    public function delCart()
    {
        $request = request();
        if ($request->isAjax()) {
            if (Db::name('cart')->delete($request->param('id'))) {
                $this->success('', '', true);
            }
        }
    }

    //检查购物车中勾选的商品一级大分类是否相同
    public function checkCartGoods()
    {
        $request = request();
        if ($request->isAjax()) {
            $str = $request->param('id');
            $cid = Db::name('zj_cart')->alias('c')
                ->join('zj_goods g', 'c.gid=g.id')
                ->join('zj_category t', 't.id=g.cid')
                ->field('t.cid')
                ->where(['c.id' => ['in', $str]])
                ->select()
                ->toArray();
            $temp = $cid[0]['cid'];
            $status = true;
            foreach ($cid as $k => $v) {
                if ($k != 0) {
                    if ($temp != $v['cid']) {
                        $status = false;
                        break;
                    }
                }
            }
            if ($status === true) {
                session('cart.id', $str);
                $this->success('成功', url('cart/Cart/confirm'), $status);
            } else {
                $this->success('请购买同分类的商品', null, $status);
            }
        }
    }

}