GoodsController.php 2.5 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) {
                    $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">
                                    购买可获得40积分
                                </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')->field('thumb,name,intro,price,content')->where(['id' => $id])->find();
        return $this->fetch(':detail', [
            'data' => $data
        ]);
    }

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

}