Index.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\index\controller;
  3. use app\index\model\Article as ArticleModel;
  4. use app\index\model\Comment as CommentModel;
  5. use app\index\model\Goods as GoodsModel;
  6. /**
  7. * 默认控制器
  8. * Class Index
  9. * @package app\api\controller
  10. */
  11. class Index extends Controller
  12. {
  13. public function index()
  14. {
  15. //banner位
  16. //商品区。new,video,bestseller
  17. $model = new GoodsModel;
  18. $newArrival = $model->getList(['listType' => 'on_sale'], 4)->toArray()['data'];
  19. $bestseller = $model->getList(['sortType' => 'sales'], 4)->toArray()['data'];
  20. $superDeals = $model->getList(['categoryId' => '10002',], 3)->toArray()['data'];
  21. //dd($superDeals);
  22. //return $this->renderSuccess(compact('bestseller'));
  23. $newGoodsId = array_column($newArrival, 'goods_id');
  24. $bestGoodsId = array_column($bestseller, 'goods_id');
  25. $superDealsGoodsId = array_column($superDeals, 'goods_id');
  26. //获取评价数量
  27. $goodsIds = array_unique(array_merge($newGoodsId, $bestGoodsId, $superDealsGoodsId));
  28. $commentModel = new CommentModel();
  29. $rows = $commentModel->rowsTotalBatch($goodsIds)->toArray();
  30. $rowsByGoodsId = array_column($rows, 'cnt', 'goods_id');
  31. //获取评论分数
  32. $commentScores = $commentModel->getTotalAll($goodsIds)->toArray();
  33. $scoresByGoodsId = array_column($commentScores, 'score_total', 'goods_id');
  34. //dd($rowsByGoodsId);
  35. //可考虑缓存
  36. foreach ($newArrival as &$item) {
  37. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  38. $temp = bcmul($item['comment_cnt'], 10, 0);
  39. if ($temp){
  40. $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
  41. }else{
  42. $item['avg_score'] = 5;
  43. }
  44. }
  45. foreach ($bestseller as &$item) {
  46. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  47. if ($temp){
  48. $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
  49. }else{
  50. $item['avg_score'] = 5;
  51. }
  52. }
  53. foreach ($superDeals as &$item) {
  54. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  55. if ($temp){
  56. $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
  57. }else{
  58. $item['avg_score'] = 5;
  59. }
  60. }
  61. $model = new ArticleModel;
  62. $article = $model->getList(0, 5)->toArray()['data'];
  63. foreach ($article as &$datum) {
  64. $datum['year'] = substr($datum['create_time'], 0, 4);
  65. $datum['month_day'] = substr($datum['create_time'], 5, 5);
  66. $datum['date'] = substr($datum['create_time'], 0, 10);
  67. }
  68. //dd($article);
  69. return view('/index/index', [
  70. 'newGoods' => $newArrival,
  71. 'bestseller' => $bestseller,
  72. 'superDealsOne' => $superDeals,
  73. 'superDealsTwo' => $superDeals,
  74. 'article' => $article,
  75. ]);
  76. }
  77. public function productDetails()
  78. {
  79. $goodsId = $this->request->param('goodsId', 0);
  80. if (empty($goodsId)) {
  81. return \redirect('index');
  82. }
  83. $goodsModel = new GoodsModel();
  84. $goods = $goodsModel->getDetails($goodsId)->toArray();
  85. $goods['content'] = html_entity_decode($goods['content']);
  86. $model = new CommentModel;
  87. $total = $model->rowsTotal($goodsId);
  88. return view('productDetails', ['goods' => $goods, 'comment_total' => $total]);
  89. }
  90. }