|
@@ -5,6 +5,7 @@ namespace app\index\controller;
|
|
|
use app\index\model\Article as ArticleModel;
|
|
|
use app\index\model\Comment as CommentModel;
|
|
|
use app\index\model\Goods as GoodsModel;
|
|
|
+use think\facade\Cache;
|
|
|
|
|
|
/**
|
|
|
* 默认控制器
|
|
@@ -16,71 +17,85 @@ class Index extends Controller
|
|
|
public function index()
|
|
|
{
|
|
|
//banner位
|
|
|
-
|
|
|
- //商品区。new,video,bestseller
|
|
|
- $model = new GoodsModel;
|
|
|
- $newArrival = $model->getList(['listType' => 'on_sale'], 4)->toArray()['data'];
|
|
|
- $bestseller = $model->getList(['sortType' => 'sales'], 4)->toArray()['data'];
|
|
|
-
|
|
|
- $superDeals = $model->getList(['categoryId' => '10002',], 3)->toArray()['data'];
|
|
|
- //dd($superDeals);
|
|
|
- //return $this->renderSuccess(compact('bestseller'));
|
|
|
-
|
|
|
- $newGoodsId = array_column($newArrival, 'goods_id');
|
|
|
- $bestGoodsId = array_column($bestseller, 'goods_id');
|
|
|
- $superDealsGoodsId = array_column($superDeals, 'goods_id');
|
|
|
-
|
|
|
- //获取评价数量
|
|
|
- $goodsIds = array_unique(array_merge($newGoodsId, $bestGoodsId, $superDealsGoodsId));
|
|
|
- $commentModel = new CommentModel();
|
|
|
- $rows = $commentModel->rowsTotalBatch($goodsIds)->toArray();
|
|
|
- $rowsByGoodsId = array_column($rows, 'cnt', 'goods_id');
|
|
|
-
|
|
|
- //获取评论分数
|
|
|
- $commentScores = $commentModel->getTotalAll($goodsIds)->toArray();
|
|
|
- $scoresByGoodsId = array_column($commentScores, 'score_total', 'goods_id');
|
|
|
-
|
|
|
- //dd($rowsByGoodsId);
|
|
|
-
|
|
|
- //可考虑缓存
|
|
|
-
|
|
|
- foreach ($newArrival as &$item) {
|
|
|
- $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
|
|
|
- $temp = bcmul($item['comment_cnt'], 10, 0);
|
|
|
- if ($temp){
|
|
|
- $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
|
|
|
- }else{
|
|
|
- $item['avg_score'] = 5;
|
|
|
+ $newArrivalCache = Cache::get('index_newArrival');
|
|
|
+ $bestsellerCache = Cache::get('index_bestseller');
|
|
|
+ $superDealsCache = Cache::get('index_superDeals');
|
|
|
+ $articleCache = Cache::get('index_article');
|
|
|
+ if ($newArrivalCache && $bestsellerCache && $superDealsCache && $articleCache) {
|
|
|
+ $newArrival = json_decode($newArrivalCache, true);
|
|
|
+ $bestseller = json_decode($bestsellerCache, true);
|
|
|
+ $superDeals = json_decode($superDealsCache, true);
|
|
|
+ $article = json_decode($articleCache, true);
|
|
|
+ } else {
|
|
|
+ //商品区。new,video,bestseller
|
|
|
+ $model = new GoodsModel;
|
|
|
+ $newArrival = $model->getList(['listType' => 'on_sale'], 4)->toArray()['data'];
|
|
|
+ $bestseller = $model->getList(['sortType' => 'sales'], 4)->toArray()['data'];
|
|
|
+
|
|
|
+ $superDeals = $model->getList(['categoryId' => '10002',], 3)->toArray()['data'];
|
|
|
+ //dd($superDeals);
|
|
|
+ //return $this->renderSuccess(compact('bestseller'));
|
|
|
+
|
|
|
+ $newGoodsId = array_column($newArrival, 'goods_id');
|
|
|
+ $bestGoodsId = array_column($bestseller, 'goods_id');
|
|
|
+ $superDealsGoodsId = array_column($superDeals, 'goods_id');
|
|
|
+
|
|
|
+ //获取评价数量
|
|
|
+ $goodsIds = array_unique(array_merge($newGoodsId, $bestGoodsId, $superDealsGoodsId));
|
|
|
+ $commentModel = new CommentModel();
|
|
|
+ $rows = $commentModel->rowsTotalBatch($goodsIds)->toArray();
|
|
|
+ $rowsByGoodsId = array_column($rows, 'cnt', 'goods_id');
|
|
|
+
|
|
|
+ //获取评论分数
|
|
|
+ $commentScores = $commentModel->getTotalAll($goodsIds)->toArray();
|
|
|
+ $scoresByGoodsId = array_column($commentScores, 'score_total', 'goods_id');
|
|
|
+
|
|
|
+ //dd($rowsByGoodsId);
|
|
|
+
|
|
|
+ //可考虑缓存
|
|
|
+
|
|
|
+ foreach ($newArrival as &$item) {
|
|
|
+ $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
|
|
|
+ $temp = bcmul($item['comment_cnt'], 10, 0);
|
|
|
+ if ($temp) {
|
|
|
+ $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
|
|
|
+ } else {
|
|
|
+ $item['avg_score'] = 5;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- foreach ($bestseller as &$item) {
|
|
|
- $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
|
|
|
- if ($temp){
|
|
|
- $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
|
|
|
- }else{
|
|
|
- $item['avg_score'] = 5;
|
|
|
+ Cache::set('index_newArrival', json_encode($newArrival), 1200);
|
|
|
+
|
|
|
+ foreach ($bestseller as &$item) {
|
|
|
+ $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
|
|
|
+ if ($temp) {
|
|
|
+ $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
|
|
|
+ } else {
|
|
|
+ $item['avg_score'] = 5;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- foreach ($superDeals as &$item) {
|
|
|
- $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
|
|
|
- if ($temp){
|
|
|
- $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
|
|
|
- }else{
|
|
|
- $item['avg_score'] = 5;
|
|
|
+ Cache::set('index_bestseller', json_encode($bestseller), 1200);
|
|
|
+
|
|
|
+ foreach ($superDeals as &$item) {
|
|
|
+ $item['comment_cnt'] = $rowsByGoodsId[$item['goods_id']] ?? 0;
|
|
|
+ if ($temp) {
|
|
|
+ $item['avg_score'] = bcdiv($scoresByGoodsId[$item['goods_id']] ?? '0', $temp, 0);
|
|
|
+ } else {
|
|
|
+ $item['avg_score'] = 5;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
+ Cache::set('index_superDeals', json_encode($superDeals), 1200);
|
|
|
+
|
|
|
+ $model = new ArticleModel;
|
|
|
+ $article = $model->getList(0, 5)->toArray()['data'];
|
|
|
|
|
|
- $model = new ArticleModel;
|
|
|
- $article = $model->getList(0, 5)->toArray()['data'];
|
|
|
+ foreach ($article as &$datum) {
|
|
|
+ $datum['year'] = substr($datum['create_time'], 0, 4);
|
|
|
+ $datum['month_day'] = substr($datum['create_time'], 5, 5);
|
|
|
+ $datum['date'] = substr($datum['create_time'], 0, 10);
|
|
|
+ }
|
|
|
+ Cache::set('index_article', json_encode($article), 1200);
|
|
|
|
|
|
- foreach ($article as &$datum) {
|
|
|
- $datum['year'] = substr($datum['create_time'], 0, 4);
|
|
|
- $datum['month_day'] = substr($datum['create_time'], 5, 5);
|
|
|
- $datum['date'] = substr($datum['create_time'], 0, 10);
|
|
|
}
|
|
|
- //dd($article);
|
|
|
|
|
|
return view('/index/index', [
|
|
|
'newGoods' => $newArrival,
|