123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- 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;
- /**
- * 默认控制器
- * Class Index
- * @package app\api\controller
- */
- class Index extends Controller
- {
- public function index()
- {
- //banner位
- $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;
- }
- }
- 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;
- }
- }
- 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'];
- 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);
- }
- return view('/index/index', [
- 'newGoods' => $newArrival,
- 'bestseller' => $bestseller,
- 'superDealsOne' => $superDeals,
- 'superDealsTwo' => $superDeals,
- 'article' => $article,
- ]);
- }
- public function productDetails()
- {
- $goodsId = $this->request->param('goodsId', 0);
- if (empty($goodsId)) {
- return \redirect('index');
- }
- $goodsModel = new GoodsModel();
- $goods = $goodsModel->getDetails($goodsId)->toArray();
- $goods['content'] = html_entity_decode($goods['content']);
- $model = new CommentModel;
- $total = $model->rowsTotal($goodsId);
- return view('productDetails', ['goods' => $goods, 'comment_total' => $total]);
- }
- }
|