123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace app\api\controller;
- use app\api\model\Article as ArticleModel;
- use app\api\model\Goods as GoodsModel;
- use think\response\Redirect;
- /**
- * 默认控制器
- * Class Index
- * @package app\api\controller
- */
- class Index extends Controller
- {
- public function index()
- {
- //banner位
- //商品区。new,video,bestseller
- $model = new GoodsModel;
- $newArrival = $model->getList(['listType'=>'on_sale'],4)->toArray()['data'];
- $bestseller = $model->getList(['sortType'=>'sales'],4)->toArray()['data'];
- //return $this->renderSuccess(compact('bestseller'));
- $newGoodsId = array_column($newArrival,'goods_id');
- $bestGoodsId = array_column($bestseller,'goods_id');
- //获取评价数量
- $goodsIds = array_unique(array_merge($newGoodsId,$bestGoodsId));
- $commentModel = new \app\api\model\Comment();
- $rows = $commentModel->rowsTotalBatch($goodsIds)->toArray();
- $rowsByGoodsId = array_column($rows,'cnt','goods_id');
- //可考虑缓存
- foreach ($newArrival as &$item){
- $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
- }
- foreach ($bestseller as &$item){
- $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
- }
- $model = new ArticleModel;
- $article = $model->getList(0,5)->toArray()['data'];
- foreach ($article as &$datum){
- $datum['year'] = substr($datum['create_time'],0,4);
- $datum['month_day'] = substr($datum['create_time'],5,5);
- $datum['date'] = substr($datum['create_time'],0,10);
- }
- //dd($article);
- return view('index', [
- 'newGoods' => $newArrival,
- 'bestseller' => $bestseller,
- 'article' => $article,
- ]);
- echo '当前访问的index.php,请将index.html设为默认站点入口';
- }
- public function productDetail()
- {
- // $goodsId = $this->request->param('goodsId',0);
- // if (empty($goodsId)){
- // return \redirect('index');
- // }
- $goodsId = 10001;
- $goodsModel = new GoodsModel();
- $goods = $goodsModel->getDetails($goodsId)->toArray();
- //dd(json_encode($goods['goods_images']));
- $goods['content'] = html_entity_decode($goods['content']);
- //dd($goods['content']);
- return view('productDetails',['goods'=>$goods]);
- }
- }
|