ZjCartController.php 1.6 KB
<?php
/**
 * Created by PhpStorm.
 * User: wz
 * Date: 2018/9/28
 * Time: 18:05
 */

namespace app\cart\controller;



use cmf\controller\HomeBaseController;
use think\Db;

class ZjCartController extends HomeBaseController
{
    /**
     * 购物车
     */
    public function cart(){
//        $id=session('user.id');
        $id=8;
        $all=Db::name('zj_cart')->alias('c')->join('zj_goods g','c.gid=g.id')->join('zj_category ca','g.cid=ca.id')
            ->where('c.uid',$id)->field('c.id as cartid,c.gid,c.num,g.*,ca.cid as caid')->select();
        if (empty($all[0])){
            $you=1;
        }else{
            $you=2;
        }
        $this->assign('you',$you);
        $this->assign('all',$all);
        return $this->fetch();
    }
    /**
     * 商品数量更改
     */
    public function num(){
        if ($this->request->isAjax()){
            $param=input('param.');
            if ($param['state']==1){
                $jia=Db::name('zj_cart')->where('id',$param['id'])->setDec('num','1');
            }else{
                $jia=Db::name('zj_cart')->where('id',$param['id'])->setInc('num','1');
            }
            if (empty($jia)){
                $this->error('NO');
            }else{
                $this->success('OK');
            }
        }
    }
    /**
     * 删除选中商品
     */
    public function del(){
        if ($this->request->isAjax()){
            $param=input('param.');
            $del=Db::name('zj_cart')->where('id','in',$param['id'])->delete();
            if (empty($del)){
                $this->error('NO');
            }else{
                $this->success('OK');
            }
        }
    }

}