GoodsController.php 4.1 KB
<?php
namespace app\goods\controller;
use app\goods\model\GoodsModel;
use cmf\controller\HomeBaseController;
use think\Db;

class GoodsController extends HomeBaseController
{

    public function goodsList()
    {
        $request = request();
        $cid = $request->param('cid');
        $category = [];
        if (!$request->isAjax()) {
            $category = $this->getCategory($cid);
            $cid = $category[0]['id'];
        }
        $goods = new GoodsModel;
        $goodsList = $goods->getGoods(null, $cid);
        if ($request->isAjax()) {
            $html = '';
            if (!empty($goodsList)) {
                foreach ($goodsList as $item) {
                    if ($item['caid'] == 2) {
                        $integral = '购买可获得' . $item['price'] * 1.5 . '积分';
                    } else {
                        $integral = '该商品无积分奖励';
                    }
                    $html .= '
                    <a href="' . url('goods/Goods/detail', ['id' => $item['id']]) . '">
                    <div class="container_list">
                        <div class="text_img">
                            <img src="' . $item['thumb'] . '" alt="">
                        </div>
                        <div class="container_info">
                            <p class="info_name">
                                ' . $item['name'] . '
                            </p>
                            <p class="info_num">
                                ' . $item['intro'] . '
                            </p>
                            <div class="people">
                                <p class="people_weight">
                                    ' . $integral . '
                                </p>
                                <p class="people_money">¥<span class="money">' . $item['price'] . '</span></p>
                            </div>
                        </div>
                    </div>
                    </a>
                ';
                }
            } else {
                $html = '<div style="width: 100%; text-align: center; font-size: small">暂无商品</div>';
            }
            $this->success('', '', $html);
        } else {
            return $this->fetch(':list', [
                'category' => $category,
                'goodsList' => $goodsList
            ]);
        }
    }

    public function detail()
    {
        $id = request()->param('id');
        $data = Db::name('zj_goods')->alias('g')
            ->field('thumb,g.name,g.intro,g.price,g.content,g.more,c.cid as caid')
            ->join('zj_category c', 'c.id=g.cid')
            ->where(['g.id' => $id])->find();
        $data['more'] = json_decode($data['more'], true);
        foreach ($data['more'] as $k => $v) {
            $data['photo'][$k]['photo'] = cmf_get_image_url($v['url']);
        }
        if ($data['caid'] == 2) {
            $data['integral'] = '购买可获得' . $data['price'] * 1.5 . '积分';
        } else {
            $data['integral'] = '该商品无积分奖励';
        }
        $data['content'] = html_entity_decode($data['content']);
        $total = Db::name('zj_cart')->alias('c')
            ->field('sum(num) as total')
            ->join('zj_goods g', 'c.gid=g.id')
            ->where(['uid' => session('user.id'), 'is_sta' => 1])
            ->select();
        if ($total[0]['total'] == '') {
            $total = 0;
        } else {
            $total = $total[0]['total'];
        }
        return $this->fetch(':detail', [
            'total' => $total,
            'data' => $data,
            'id' => $id,
            'comment' => $this->getGoodsComment($id)
        ]);
    }

    private function getCategory($cid)
    {
        return Db::name('zj_category')->field('id,name')->where(['cid' => $cid, 'delete_time' => ['eq', 0]])->select()->toArray();
    }

    public function getGoodsComment($gid)
    {
        return Db::name('zj_evaluate')->alias('e')
            ->field('u.user_nickname,avatar,e.content,e.create_time')
            ->join('user u', 'u.id=e.uid')
            ->where(['gid' => $gid])
            ->order('create_time DESC')
            ->select()
            ->toArray();
    }

}