Index.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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',], 8)->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]) && $superDeals1[] = $superDeals[3];
  96. isset($superDeals[4]) && $superDeals2[] = $superDeals[4];
  97. isset($superDeals[5]) && $superDeals2[] = $superDeals[5];
  98. isset($superDeals[6]) && $superDeals2[] = $superDeals[6];
  99. isset($superDeals[7]) && $superDeals2[] = $superDeals[7];
  100. return view('/index/index', [
  101. 'newGoods' => $newArrival,
  102. 'bestseller' => $bestseller,
  103. 'superDealsOne' => $superDeals1,
  104. 'superDealsTwo' => $superDeals2,
  105. 'article' => $article,
  106. ]);
  107. }
  108. /**
  109. * 产品详情页
  110. * @return \think\response\Redirect|View
  111. * @throws \cores\exception\BaseException
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\DbException
  114. * @throws \think\db\exception\ModelNotFoundException
  115. */
  116. public function productDetails()
  117. {
  118. $goodsId = $this->request->param('goodsId', 0);
  119. if (empty($goodsId)) {
  120. return \redirect('index');
  121. }
  122. $key = $this->request->param('key', '');
  123. $goodsModel = new GoodsModel();
  124. $goods = $goodsModel->getDetails($goodsId)->toArray();
  125. $goods['content'] = html_entity_decode($goods['content']);
  126. $model = new CommentModel;
  127. $total = $model->rowsTotal($goodsId);
  128. return view('productDetails', ['goods' => $goods, 'comment_total' => $total, 'key' => $key]);
  129. }
  130. public function aboutUs(): View
  131. {
  132. return view('aboutUs');
  133. }
  134. public function privacyPolicy(): View
  135. {
  136. return view('privacyPolicy');
  137. }
  138. public function salesTerms(): View
  139. {
  140. return view('salesTerms');
  141. }
  142. public function vapePoints(): View
  143. {
  144. return view('vapePoints');
  145. }
  146. public function returnWarranty(): View
  147. {
  148. return view('returnWarranty');
  149. }
  150. public function newsDetail(): View
  151. {
  152. $newsId = $this->request->param('newsId');
  153. if (empty($newsId)) {
  154. return view('/error');
  155. }
  156. $model = new ArticleModel;
  157. $article = $model->find($newsId)->toArray();
  158. return view('newsDetails', ['article' => $article]);
  159. }
  160. public function goldTest()
  161. {
  162. $service = new OrderGetYundanService();
  163. $r = $service->orderCreateYundans($this->storeId);
  164. dd($r);
  165. $orderModel = new OrderModel();
  166. $r = $orderModel->getUsableList(10001);
  167. dd($r->toArray());
  168. $Usps = new Usps();
  169. // 请求查询接口
  170. $res = $Usps->getItdidaToken();
  171. dd($res);
  172. }
  173. }