Index.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\Article as ArticleModel;
  4. use app\api\model\Goods as GoodsModel;
  5. use think\helper\Str;
  6. use think\response\Redirect;
  7. /**
  8. * 默认控制器
  9. * Class Index
  10. * @package app\api\controller
  11. */
  12. class Index extends Controller
  13. {
  14. public function index()
  15. {
  16. //banner位
  17. //商品区。new,video,bestseller
  18. $model = new GoodsModel;
  19. $newArrival = $model->getList(['listType'=>'on_sale'],4)->toArray()['data'];
  20. $bestseller = $model->getList(['sortType'=>'sales'],4)->toArray()['data'];
  21. //return $this->renderSuccess(compact('bestseller'));
  22. $newGoodsId = array_column($newArrival,'goods_id');
  23. $bestGoodsId = array_column($bestseller,'goods_id');
  24. //获取评价数量
  25. $goodsIds = array_unique(array_merge($newGoodsId,$bestGoodsId));
  26. $commentModel = new \app\api\model\Comment();
  27. $rows = $commentModel->rowsTotalBatch($goodsIds)->toArray();
  28. $rowsByGoodsId = array_column($rows,'cnt','goods_id');
  29. //可考虑缓存
  30. foreach ($newArrival as &$item){
  31. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  32. }
  33. foreach ($bestseller as &$item){
  34. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  35. }
  36. $model = new ArticleModel;
  37. $article = $model->getList(0,5)->toArray()['data'];
  38. foreach ($article as &$datum){
  39. $datum['year'] = substr($datum['create_time'],0,4);
  40. $datum['month_day'] = substr($datum['create_time'],5,5);
  41. $datum['date'] = substr($datum['create_time'],0,10);
  42. }
  43. //dd($article);
  44. return view('index', [
  45. 'newGoods' => $newArrival,
  46. 'bestseller' => $bestseller,
  47. 'article' => $article,
  48. ]);
  49. echo '当前访问的index.php,请将index.html设为默认站点入口';
  50. }
  51. public function productDetail()
  52. {
  53. $goodsId = $this->request->param('goodsId',0);
  54. if (empty($goodsId)){
  55. return \redirect('index');
  56. }
  57. $goodsModel = new GoodsModel();
  58. $goods = $goodsModel->getDetails($goodsId)->toArray();
  59. $goods['content'] = html_entity_decode($goods['content']);
  60. return view('productDetails',['goods'=>$goods]);
  61. }
  62. }