ZjCartController.php 7.3 KB
<?php

namespace app\cart\controller;

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

class ZjCartController extends HomeBaseController
{
    /**
     * 购物车
     */
    public function cart()
    {
        $id = session('user.id');
        $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)
            ->where(['g.is_sta' => '1', 'g.delete_time' => '0'])
            ->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');
            }
        }
    }

    //检查购物车中勾选的商品一级大分类是否相同
    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.uid' => session('user.id'), '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('goods.id', null);
                session('cart.id', $str);
                $this->success('成功', url('cart/ZjCart/orderConfirm'), $status);
            } else {
                $this->success('请购买同分类的商品', null, $status);
            }
        }
    }

    //订单确认页
    public function orderConfirm()
    {
        if (session('cart.id') == null && session('goods.id') == null) {
            $this->redirect(url('index/Index/index'));
        } else {
            $total_num = 0;
            $whole = 0;
            if (session('cart.id') != null && session('goods.id') == null) {
                $data = Db::name('zj_cart')->alias('c')
                    ->join('zj_goods g', 'c.gid=g.id')
                    ->join('zj_category t', 't.id=g.cid')
                    ->field('c.num,g.name,g.price,g.price_num,g.price_cash,g.thumb,g.is_type,g.intro,t.cid as caid,g.is_type')
                    ->where(['c.uid' => session('user.id'), 'c.id' => ['in', session('cart.id')]])
                    ->select();
            } elseif (session('cart.id') == null && session('goods.id') != null) {
                $data = Db::name('zj_goods')->alias('g')
                    ->join('zj_category t', 't.id=g.cid')
                    ->field('g.name,g.price,g.price_num,g.price_cash,g.thumb,g.is_type,g.intro,t.cid as caid,g.is_type')
                    ->where(['g.id' => session('goods.id')])
                    ->select();
            } else {
                return false;
            }
            foreach ($data as $k => $item) {
                if (session('cart.id') == null && session('goods.id') != null) {
                    $item['num'] = 1;
                }
                if ($item['caid'] == 2) {
                    $item['integral'] = '购买可获得' . $item['price'] * $item['num'] * 2 . '积分';
                } else {
                    $item['integral'] = '该商品无积分奖励';
                }
                $total_num += $item['num'];
                $item['sum_price'] = $item['price'] * $item['num'];
                $whole += $item['sum_price'];
                $data[$k] = $item;
            }
            $address = Db::name('zj_user_place')
                ->field('id,name,province,city,county,mobile,place,is_sta')
                ->where(['uid' => session('user.id')])
                ->select()
                ->toArray();
            $default_address_id = '';
            $default_name = '请选择地址';
            $default_mobile = '';
            $default_address = '';
            if (!empty($address)) {
                foreach ($address as $item) {
                    if ($item['is_sta'] == 2) {
                        $default_address_id = $item['id'];
                        $default_name = $item['name'];
                        $default_mobile = $item['mobile'];
                        $default_address = $item['province'] . $item['city'] . $item['county'] . $item['place'];
                        break;
                    }
                }
            }

            require_once EXTEND_PATH . '/WeChatCommon.php';
            $wx = new \WeChatCommon();
            //attach额外附加金额积分关系字段
            if ($data[0]['caid'] == 2) {
                $min_cash = 0;      //该笔订单所需的最低金额
                $min_num = 0;       //该笔订单所需的最低积分
                $total_price = 0;   //该笔订单所需的总价格
                foreach ($data as $k => $v) {
                    $min_cash += $v['price_cash'] * $v['num'];
                    $min_num += $v['price_num'] * $v['num'];
                    $total_price += $v['price'] * $v['num'];
                }
                $attach = [
                    'min_cash' => $min_cash,
                    'min_num' => $min_num,
                    'total_price' => $total_price
                ];
            } else {
                $attach = [
                    'min_cash' => '',
                    'min_num' => '',
                    'total_price' => ''
                ];
            }
            return $this->fetch('order_confirm', [
                'data' => $data,
                'address' => $address,
                'default_address_id' => $default_address_id,
                'default_name' => $default_name,
                'default_mobile' => $default_mobile,
                'default_address' => $default_address,
                'total_num' => $total_num,
                'total_price' => $whole,
                'caid' => $data[0]['caid'],
                'js_sdk' => $wx->js_sdk(),
                'uid' => session('user.id'),
                'attach' => $attach,
                'balance' => Db::name('user')->where(['id'=>session('user.id')])->value('balance')
            ]);
        }
    }

}