Index.php 5.8 KB

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