From c4ab3af43a31036c6a4b00864b7d26c56c5ee299 Mon Sep 17 00:00:00 2001 From: liqian <735273025@qq.com> Date: Wed, 22 Jan 2025 17:16:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=9F=A5=E8=AF=A2bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/index/controller/TopsNas.php | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/app/index/controller/TopsNas.php b/app/index/controller/TopsNas.php index b27a174..ef485cd 100755 --- a/app/index/controller/TopsNas.php +++ b/app/index/controller/TopsNas.php @@ -313,7 +313,7 @@ class TopsNas extends BaseController } // 根据分类获取文章 - private function getArticleByCategory($categorys, $limit = 3) + private function getArticleByCategory($categorys, $limit = null) { if (!is_array($categorys)) { throw new \Exception('请确认分类正确'); @@ -322,7 +322,7 @@ class TopsNas extends BaseController return []; } - $model = Loader::model('Article') + $sub_query = Loader::model('Article') ->field([ 'id', 'cid', @@ -331,7 +331,14 @@ class TopsNas extends BaseController ]) ->where('stat', '=', 0) ->where('cid', '=', $categorys[0]['id']) - ->where('country_code', '=', $this->country_code); + ->where('country_code', '=', $this->country_code) + ->order(['sort' => 'asc', 'id' => 'desc']); + if (!empty($limit)) { + $sub_query = $sub_query->limit($limit); + } + $sub_query = $sub_query->buildSql(); + + $model = \think\Db::table($sub_query . ' a'); foreach ($categorys as $key => $val) { if ($key == 0) continue; $model->union(function($query) use($val, $limit) { @@ -344,13 +351,15 @@ class TopsNas extends BaseController ->where('stat', '=', 0) ->where('cid', '=', $val['id']) ->where('country_code', '=', $this->country_code) - ->order(['sort' => 'asc', 'id' => 'desc']) - ->limit($limit); + ->order(['sort' => 'asc', 'id' => 'desc']); + if (!empty($limit)) { + $query->limit($limit); + } }); } $map = []; - $data = $model->order(['sort' => 'asc', 'id' => 'desc'])->limit($limit)->select(); + $data = $data = $model->select(); foreach ($data as $key => $val) { $map['cid_' . $val['cid']][] = $val; } @@ -369,8 +378,8 @@ class TopsNas extends BaseController $val['articles'] = []; } if (isset($articles['cid_' . $val['id']])) { - $articles = $articles['cid_' . $val['id']]; - foreach ($articles as $k => $v) { + $items = $articles['cid_' . $val['id']]; + foreach ($items as $k => $v) { $val['articles'][] = [ 'id' => $v['id'], 'name' => $v['name'] @@ -441,15 +450,15 @@ class TopsNas extends BaseController // 获取分类 $categorys = $this->getCategoryTree(76); // 获取文章 - $articles = $this->getArticleByCategory($categorys, 3); + $articles = $this->getArticleByCategory($categorys, ); // 组装数据 foreach ($categorys as $key => &$val) { if (!isset($val['articles'])) { $val['articles'] = []; } if (isset($articles['cid_' . $val['id']])) { - $articles = $articles['cid_' . $val['id']]; - foreach ($articles as $k => $v) { + $items = $articles['cid_' . $val['id']]; + foreach ($items as $k => $v) { $val['articles'][] = [ 'id' => $v['id'], 'name' => $v['name']