Index.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. use think\facade\Cache;
  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. $newArrivalCache = Cache::get('index_newArrival');
  18. $bestsellerCache = Cache::get('index_bestseller');
  19. $superDealsCache = Cache::get('index_superDeals');
  20. $articleCache = Cache::get('index_article');
  21. if ($newArrivalCache && $bestsellerCache && $superDealsCache && $articleCache) {
  22. $newArrival = json_decode($newArrivalCache, true);
  23. $bestseller = json_decode($bestsellerCache, true);
  24. $superDeals = json_decode($superDealsCache, true);
  25. $article = json_decode($articleCache, true);
  26. } else {
  27. //商品区。new,video,bestseller
  28. $model = new GoodsModel;
  29. $newArrival = $model->getList(['listType' => 'on_sale'], 4)->toArray()['data'];
  30. $bestseller = $model->getList(['sortType' => 'sales'], 4)->toArray()['data'];
  31. $superDeals = $model->getList(['categoryId' => '10002',], 3)->toArray()['data'];
  32. //dd($superDeals);
  33. //return $this->renderSuccess(compact('bestseller'));
  34. $newGoodsId = array_column($newArrival, 'goods_id');
  35. $bestGoodsId = array_column($bestseller, 'goods_id');
  36. $superDealsGoodsId = array_column($superDeals, 'goods_id');
  37. //获取评价数量
  38. $goodsIds = array_unique(array_merge($newGoodsId, $bestGoodsId, $superDealsGoodsId));
  39. $commentModel = new CommentModel();
  40. $rows = $commentModel->rowsTotalBatch($goodsIds)->toArray();
  41. $rowsByGoodsId = array_column($rows, 'cnt', 'goods_id');
  42. //获取评论分数
  43. $commentScores = $commentModel->getTotalAll($goodsIds)->toArray();
  44. $scoresByGoodsId = array_column($commentScores, 'score_total', 'goods_id');
  45. //dd($rowsByGoodsId);
  46. //可考虑缓存
  47. foreach ($newArrival as &$item) {
  48. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  49. $temp = bcmul($item['comment_cnt'], 10, 0);
  50. if ($temp) {
  51. $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
  52. } else {
  53. $item['avg_score'] = 5;
  54. }
  55. }
  56. Cache::set('index_newArrival', json_encode($newArrival), 1200);
  57. foreach ($bestseller as &$item) {
  58. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  59. if ($temp) {
  60. $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
  61. } else {
  62. $item['avg_score'] = 5;
  63. }
  64. }
  65. Cache::set('index_bestseller', json_encode($bestseller), 1200);
  66. foreach ($superDeals as &$item) {
  67. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  68. if ($temp) {
  69. $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
  70. } else {
  71. $item['avg_score'] = 5;
  72. }
  73. }
  74. Cache::set('index_superDeals', json_encode($superDeals), 1200);
  75. $model = new ArticleModel;
  76. $article = $model->getList(0, 5)->toArray()['data'];
  77. foreach ($article as &$datum) {
  78. $datum['year'] = substr($datum['create_time'], 0, 4);
  79. $datum['month_day'] = substr($datum['create_time'], 5, 5);
  80. $datum['date'] = substr($datum['create_time'], 0, 10);
  81. }
  82. Cache::set('index_article', json_encode($article), 1200);
  83. }
  84. return view('/index/index', [
  85. 'newGoods' => $newArrival,
  86. 'bestseller' => $bestseller,
  87. 'superDealsOne' => $superDeals,
  88. 'superDealsTwo' => $superDeals,
  89. 'article' => $article,
  90. ]);
  91. }
  92. public function productDetails()
  93. {
  94. $goodsId = $this->request->param('goodsId', 0);
  95. if (empty($goodsId)) {
  96. return \redirect('index');
  97. }
  98. $goodsModel = new GoodsModel();
  99. $goods = $goodsModel->getDetails($goodsId)->toArray();
  100. $goods['content'] = html_entity_decode($goods['content']);
  101. $model = new CommentModel;
  102. $total = $model->rowsTotal($goodsId);
  103. return view('productDetails', ['goods' => $goods, 'comment_total' => $total]);
  104. }
  105. }