Index.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. use think\response\View;
  8. /**
  9. * 默认控制器
  10. * Class Index
  11. * @package app\api\controller
  12. */
  13. class Index extends Controller
  14. {
  15. public function index()
  16. {
  17. //banner位
  18. $newArrivalCache = Cache::get('index_newArrival');
  19. $bestsellerCache = Cache::get('index_bestseller');
  20. $superDealsCache = Cache::get('index_superDeals');
  21. $articleCache = Cache::get('index_article');
  22. if ($newArrivalCache && $bestsellerCache && $superDealsCache && $articleCache) {
  23. $newArrival = json_decode($newArrivalCache, true);
  24. $bestseller = json_decode($bestsellerCache, true);
  25. $superDeals = json_decode($superDealsCache, true);
  26. $article = json_decode($articleCache, true);
  27. } else {
  28. //商品区。new,video,bestseller
  29. $model = new GoodsModel;
  30. $newArrival = $model->getList(['listType' => 'on_sale'], 4)->toArray()['data'];
  31. $bestseller = $model->getList(['sortType' => 'sales'], 4)->toArray()['data'];
  32. $superDeals = $model->getList(['categoryId' => '10002',], 3)->toArray()['data'];
  33. //dd($superDeals);
  34. //return $this->renderSuccess(compact('bestseller'));
  35. $newGoodsId = array_column($newArrival, 'goods_id');
  36. $bestGoodsId = array_column($bestseller, 'goods_id');
  37. $superDealsGoodsId = array_column($superDeals, 'goods_id');
  38. //获取评价数量
  39. $goodsIds = array_unique(array_merge($newGoodsId, $bestGoodsId, $superDealsGoodsId));
  40. $commentModel = new CommentModel();
  41. $rows = $commentModel->rowsTotalBatch($goodsIds)->toArray();
  42. $rowsByGoodsId = array_column($rows, 'cnt', 'goods_id');
  43. //获取评论分数
  44. $commentScores = $commentModel->getTotalAll($goodsIds)->toArray();
  45. $scoresByGoodsId = array_column($commentScores, 'score_total', 'goods_id');
  46. //dd($rowsByGoodsId);
  47. //可考虑缓存
  48. foreach ($newArrival as &$item) {
  49. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  50. $temp = bcmul($item['comment_cnt'], 10, 0);
  51. if ($temp) {
  52. $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
  53. } else {
  54. $item['avg_score'] = 5;
  55. }
  56. }
  57. Cache::set('index_newArrival', json_encode($newArrival), 1200);
  58. foreach ($bestseller as &$item) {
  59. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  60. if ($temp) {
  61. $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
  62. } else {
  63. $item['avg_score'] = 5;
  64. }
  65. }
  66. Cache::set('index_bestseller', json_encode($bestseller), 1200);
  67. foreach ($superDeals as &$item) {
  68. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  69. if ($temp) {
  70. $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
  71. } else {
  72. $item['avg_score'] = 5;
  73. }
  74. }
  75. Cache::set('index_superDeals', json_encode($superDeals), 1200);
  76. $model = new ArticleModel;
  77. $article = $model->getList(0, 5)->toArray()['data'];
  78. foreach ($article as &$datum) {
  79. $datum['year'] = substr($datum['create_time'], 0, 4);
  80. $datum['month_day'] = substr($datum['create_time'], 5, 5);
  81. $datum['date'] = substr($datum['create_time'], 0, 10);
  82. }
  83. Cache::set('index_article', json_encode($article), 1200);
  84. }
  85. return view('/index/index', [
  86. 'newGoods' => $newArrival,
  87. 'bestseller' => $bestseller,
  88. 'superDealsOne' => $superDeals,
  89. 'superDealsTwo' => $superDeals,
  90. 'article' => $article,
  91. ]);
  92. }
  93. /**
  94. * 产品详情页
  95. * @return \think\response\Redirect|View
  96. * @throws \cores\exception\BaseException
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\DbException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. */
  101. public function productDetails()
  102. {
  103. $goodsId = $this->request->param('goodsId', 0);
  104. if (empty($goodsId)) {
  105. return \redirect('index');
  106. }
  107. $goodsModel = new GoodsModel();
  108. $goods = $goodsModel->getDetails($goodsId)->toArray();
  109. $goods['content'] = html_entity_decode($goods['content']);
  110. $model = new CommentModel;
  111. $total = $model->rowsTotal($goodsId);
  112. return view('productDetails', ['goods' => $goods, 'comment_total' => $total]);
  113. }
  114. public function aboutUs(): View
  115. {
  116. return view('aboutUs');
  117. }
  118. public function privacyPolicy(): View
  119. {
  120. return view('privacyPolicy');
  121. }
  122. public function salesTerms(): View
  123. {
  124. return view('salesTerms');
  125. }
  126. public function vapePoints(): View
  127. {
  128. return view('vapePoints');
  129. }
  130. public function returnWarranty(): View
  131. {
  132. return view('returnWarranty');
  133. }
  134. public function newsDetail(): View
  135. {
  136. $newsId = $this->request->param('newsId');
  137. if (empty($newsId)) {
  138. return view('/error');
  139. }
  140. $model = new ArticleModel;
  141. $article = $model->find($newsId)->toArray();
  142. return view('newsDetails', ['article' => $article]);
  143. }
  144. }