Index.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\Article as ArticleModel;
  4. use app\api\model\Goods as GoodsModel;
  5. use think\helper\Str;
  6. use think\response\Redirect;
  7. /**
  8. * 默认控制器
  9. * Class Index
  10. * @package app\api\controller
  11. */
  12. class Index extends Controller
  13. {
  14. public function index()
  15. {
  16. //banner位
  17. //商品区。new,video,bestseller
  18. $model = new GoodsModel;
  19. $newArrival = $model->getList(['listType' => 'on_sale'], 4)->toArray()['data'];
  20. $bestseller = $model->getList(['sortType' => 'sales'], 4)->toArray()['data'];
  21. $superDeals = $model->getList(['categoryId' => '10002',], 3)->toArray()['data'];
  22. //dd($superDeals);
  23. //return $this->renderSuccess(compact('bestseller'));
  24. $newGoodsId = array_column($newArrival, 'goods_id');
  25. $bestGoodsId = array_column($bestseller, 'goods_id');
  26. $superDealsGoodsId = array_column($superDeals, 'goods_id');
  27. //获取评价数量
  28. $goodsIds = array_unique(array_merge($newGoodsId, $bestGoodsId, $superDealsGoodsId));
  29. $commentModel = new \app\api\model\Comment();
  30. $rows = $commentModel->rowsTotalBatch($goodsIds)->toArray();
  31. $rowsByGoodsId = array_column($rows, 'cnt', 'goods_id');
  32. //可考虑缓存
  33. foreach ($newArrival as &$item) {
  34. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  35. }
  36. foreach ($bestseller as &$item) {
  37. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  38. }
  39. foreach ($superDeals as &$item) {
  40. $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
  41. }
  42. $model = new ArticleModel;
  43. $article = $model->getList(0, 5)->toArray()['data'];
  44. foreach ($article as &$datum) {
  45. $datum['year'] = substr($datum['create_time'], 0, 4);
  46. $datum['month_day'] = substr($datum['create_time'], 5, 5);
  47. $datum['date'] = substr($datum['create_time'], 0, 10);
  48. }
  49. //dd($article);
  50. return view('index', [
  51. 'newGoods' => $newArrival,
  52. 'bestseller' => $bestseller,
  53. 'superDealsOne' => $superDeals,
  54. 'superDealsTwo' => $superDeals,
  55. 'article' => $article,
  56. ]);
  57. echo '当前访问的index.php,请将index.html设为默认站点入口';
  58. }
  59. public function productDetail()
  60. {
  61. $goodsId = $this->request->param('goodsId', 0);
  62. if (empty($goodsId)) {
  63. return \redirect('index');
  64. }
  65. $goodsModel = new GoodsModel();
  66. $goods = $goodsModel->getDetails($goodsId)->toArray();
  67. $goods['content'] = html_entity_decode($goods['content']);
  68. return view('productDetails', ['goods' => $goods]);
  69. }
  70. }