Index.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. $superDeals = $bestseller;
  38. $newGoodsId = array_column($newArrival, 'goods_id');
  39. $bestGoodsId = array_column($bestseller, 'goods_id');
  40. $superDealsGoodsId = array_column($superDeals, 'goods_id');
  41. //获取评价数量
  42. $goodsIds = array_unique(array_merge($newGoodsId, $bestGoodsId, $superDealsGoodsId));
  43. $commentModel = new CommentModel();
  44. $rows = $commentModel->rowsTotalBatch($goodsIds)->toArray();
  45. $rowsByGoodsId = array_column($rows, 'cnt', 'goods_id');
  46. //获取评论分数
  47. $commentScores = $commentModel->getTotalAll($goodsIds)->toArray();
  48. $scoresByGoodsId = array_column($commentScores, 'score_total', 'goods_id');
  49. //dd($rowsByGoodsId);
  50. //可考虑缓存
  51. foreach ($newArrival as &$item) {
  52. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  53. $temp = bcmul($item['comment_cnt'], 10, 0);
  54. if ($temp) {
  55. $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
  56. } else {
  57. $item['avg_score'] = 5;
  58. }
  59. }
  60. Cache::set('index_newArrival', json_encode($newArrival), 1200);
  61. foreach ($bestseller as &$item) {
  62. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  63. $temp = bcmul($item['comment_cnt'], 10, 0);
  64. if ($temp) {
  65. $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
  66. } else {
  67. $item['avg_score'] = 5;
  68. }
  69. }
  70. Cache::set('index_bestseller', json_encode($bestseller), 1200);
  71. foreach ($superDeals as &$item) {
  72. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  73. $temp = bcmul($item['comment_cnt'], 10, 0);
  74. if ($temp) {
  75. $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
  76. } else {
  77. $item['avg_score'] = 5;
  78. }
  79. }
  80. Cache::set('index_superDeals', json_encode($superDeals), 1200);
  81. $model = new ArticleModel;
  82. $article = $model->getList(0, 5)->toArray()['data'];
  83. foreach ($article as &$datum) {
  84. $datum['year'] = substr($datum['create_time'], 0, 4);
  85. $datum['month_day'] = substr($datum['create_time'], 5, 5);
  86. $datum['date'] = substr($datum['create_time'], 0, 10);
  87. }
  88. Cache::set('index_article', json_encode($article), 1200);
  89. }
  90. $superDeals1 = $superDeals2 = [];
  91. isset($superDeals[0]) && $superDeals1[] = $superDeals[0];
  92. isset($superDeals[1]) && $superDeals1[] = $superDeals[1];
  93. isset($superDeals[2]) && $superDeals1[] = $superDeals[2];
  94. isset($superDeals[3]) && $superDeals1[] = $superDeals[3];
  95. isset($superDeals[4]) && $superDeals2[] = $superDeals[4];
  96. isset($superDeals[5]) && $superDeals2[] = $superDeals[5];
  97. isset($superDeals[6]) && $superDeals2[] = $superDeals[6];
  98. isset($superDeals[7]) && $superDeals2[] = $superDeals[7];
  99. return view('/index/index', [
  100. 'newGoods' => $newArrival,
  101. 'bestseller' => $bestseller,
  102. 'superDealsOne' => $superDeals1,
  103. 'superDealsTwo' => $superDeals2,
  104. 'article' => $article,
  105. ]);
  106. }
  107. /**
  108. * 产品详情页
  109. * @return \think\response\Redirect|View
  110. * @throws \cores\exception\BaseException
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\DbException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. */
  115. public function productDetails()
  116. {
  117. $goodsId = intval($this->request->param('goodsId', 0));
  118. if (empty($goodsId)) {
  119. return \redirect('index');
  120. }
  121. $key = $this->request->param('key', '');
  122. $goodsModel = new GoodsModel();
  123. $goods = $goodsModel->getDetails($goodsId);
  124. if (empty($goods) || $goods['status'] != 200){
  125. return \view('/error');
  126. }
  127. $goods = $goods->toArray();
  128. $goods['content'] = html_entity_decode($goods['content']);
  129. $model = new CommentModel;
  130. $total = $model->rowsTotal($goodsId);
  131. $flavors = ['STRAWBERRY-MANGO', 'BLUE RAZZ-ICE', 'WATERMELON', 'GRAPE', 'KIWI-PASSION-FRUIT-GUAVA', 'JUICY-PEACH', 'BLUEBERRY-ICE'];
  132. return view('productDetails', ['goods' => $goods, 'comment_total' => $total, 'key' => $key]);
  133. }
  134. public function aboutUs(): View
  135. {
  136. return view('aboutUs');
  137. }
  138. public function privacyPolicy(): View
  139. {
  140. return view('privacyPolicy');
  141. }
  142. public function salesTerms(): View
  143. {
  144. return view('salesTerms');
  145. }
  146. public function vapePoints(): View
  147. {
  148. return view('vapePoints');
  149. }
  150. public function returnWarranty(): View
  151. {
  152. return view('returnWarranty');
  153. }
  154. public function newsDetail(): View
  155. {
  156. $newsId = $this->request->param('newsId');
  157. if (empty($newsId)) {
  158. return view('/error');
  159. }
  160. $model = new ArticleModel;
  161. $article = $model->find($newsId)->toArray();
  162. return view('newsDetails', ['article' => $article]);
  163. }
  164. public function goldTest()
  165. {
  166. $service = new OrderGetYundanService();
  167. $r = $service->orderCreateYundans($this->storeId);
  168. dd($r);
  169. $orderModel = new OrderModel();
  170. $r = $orderModel->getUsableList(10001);
  171. dd($r->toArray());
  172. $Usps = new Usps();
  173. // 请求查询接口
  174. $res = $Usps->getItdidaToken();
  175. dd($res);
  176. }
  177. }