fix: 修复查询bug

This commit is contained in:
2025-01-22 17:16:54 +08:00
parent bfda10176f
commit c4ab3af43a

View File

@@ -313,7 +313,7 @@ class TopsNas extends BaseController
} }
// 根据分类获取文章 // 根据分类获取文章
private function getArticleByCategory($categorys, $limit = 3) private function getArticleByCategory($categorys, $limit = null)
{ {
if (!is_array($categorys)) { if (!is_array($categorys)) {
throw new \Exception('请确认分类正确'); throw new \Exception('请确认分类正确');
@@ -322,7 +322,7 @@ class TopsNas extends BaseController
return []; return [];
} }
$model = Loader::model('Article') $sub_query = Loader::model('Article')
->field([ ->field([
'id', 'id',
'cid', 'cid',
@@ -331,7 +331,14 @@ class TopsNas extends BaseController
]) ])
->where('stat', '=', 0) ->where('stat', '=', 0)
->where('cid', '=', $categorys[0]['id']) ->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) { foreach ($categorys as $key => $val) {
if ($key == 0) continue; if ($key == 0) continue;
$model->union(function($query) use($val, $limit) { $model->union(function($query) use($val, $limit) {
@@ -344,13 +351,15 @@ class TopsNas extends BaseController
->where('stat', '=', 0) ->where('stat', '=', 0)
->where('cid', '=', $val['id']) ->where('cid', '=', $val['id'])
->where('country_code', '=', $this->country_code) ->where('country_code', '=', $this->country_code)
->order(['sort' => 'asc', 'id' => 'desc']) ->order(['sort' => 'asc', 'id' => 'desc']);
->limit($limit); if (!empty($limit)) {
$query->limit($limit);
}
}); });
} }
$map = []; $map = [];
$data = $model->order(['sort' => 'asc', 'id' => 'desc'])->limit($limit)->select(); $data = $data = $model->select();
foreach ($data as $key => $val) { foreach ($data as $key => $val) {
$map['cid_' . $val['cid']][] = $val; $map['cid_' . $val['cid']][] = $val;
} }
@@ -369,8 +378,8 @@ class TopsNas extends BaseController
$val['articles'] = []; $val['articles'] = [];
} }
if (isset($articles['cid_' . $val['id']])) { if (isset($articles['cid_' . $val['id']])) {
$articles = $articles['cid_' . $val['id']]; $items = $articles['cid_' . $val['id']];
foreach ($articles as $k => $v) { foreach ($items as $k => $v) {
$val['articles'][] = [ $val['articles'][] = [
'id' => $v['id'], 'id' => $v['id'],
'name' => $v['name'] 'name' => $v['name']
@@ -441,15 +450,15 @@ class TopsNas extends BaseController
// 获取分类 // 获取分类
$categorys = $this->getCategoryTree(76); $categorys = $this->getCategoryTree(76);
// 获取文章 // 获取文章
$articles = $this->getArticleByCategory($categorys, 3); $articles = $this->getArticleByCategory($categorys, );
// 组装数据 // 组装数据
foreach ($categorys as $key => &$val) { foreach ($categorys as $key => &$val) {
if (!isset($val['articles'])) { if (!isset($val['articles'])) {
$val['articles'] = []; $val['articles'] = [];
} }
if (isset($articles['cid_' . $val['id']])) { if (isset($articles['cid_' . $val['id']])) {
$articles = $articles['cid_' . $val['id']]; $items = $articles['cid_' . $val['id']];
foreach ($articles as $k => $v) { foreach ($items as $k => $v) {
$val['articles'][] = [ $val['articles'][] = [
'id' => $v['id'], 'id' => $v['id'],
'name' => $v['name'] 'name' => $v['name']