Index.php 6.9 KB

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