diff --git a/app/common.php b/app/common.php index efa1bbd..071cd75 100644 --- a/app/common.php +++ b/app/common.php @@ -1308,3 +1308,324 @@ if (!function_exists('deal_image_url')) { return $tmpUrl; } } + +/** + * 同时查询主栏目和副栏目内容 + * @param int $cateId 栏目ID + * @param int $sellerId 商户ID + * @param int $limit 限制数量 + * @param string $sort 排序方式 + * @param array $with 关联查询 + * @param string $field 查询字段 + * @return array + */ +if (!function_exists('getContentByCategory')) { + function getContentByCategory(int $cateId, int $sellerId, int $limit = 10, string $sort = 'publish_time desc', array $with = ['thumbnail'], string $field = '*'): array + { + try { + // 1. 查询主栏目内容(通过中间表) + $mainSubIds = \app\model\CategorySubContent::where('category_id', $cateId) + ->where('seller_id', $sellerId) + ->column('sub_content_id'); + + // 2. 查询副栏目内容(通过 sub_category_ids 字段) + $subContentModel = new \app\model\SubContent(); + $subQuery = $subContentModel->where('seller_id', $sellerId) + ->where('is_del', 1) + ->where('sub_category_ids', 'like', '%' . $cateId . '%') + ->column('id'); + + // 3. 合并去重 + $allIds = array_unique(array_merge($mainSubIds, $subQuery)); + + if (empty($allIds)) { + return []; + } + + // 4. 查询内容详情 + $query = $subContentModel->whereIn('id', $allIds) + ->where('seller_id', $sellerId) + ->where('is_del', 1); + + // 关联查询时,需要包含外键字段 + if (!empty($with)) { + // 确保 field 中包含关联所需的外键字段 + if ($field !== '*') { + $fields = explode(',', $field); + foreach ($with as $relation) { + $relName = is_string($relation) ? $relation : (is_array($relation) ? key($relation) : ''); + if ($relName && !in_array($relName, $fields)) { + $fields[] = $relName; + } + } + $field = implode(',', $fields); + } + $query = $query->with($with); + } + + // 排序和限制 + $result = $query->field($field) + ->order($sort) + ->limit($limit) + ->select() + ->toArray(); + + return $result; + } catch (\Exception $e) { + return []; + } + } +} + + + +/** + * 获取内容URL + * @param array $content 内容数据 + * @param int $siteId 站点ID + * @return string + */ +if (!function_exists('getContentUrl')) { + function getContentUrl(array $content, int $siteId = 0): string + { + try { + $website = \app\model\Website::where('id', $siteId) + ->where('seller_id', $content['seller_id'] ?? 0) + ->find(); + + if ($website) { + return 'http://' . $website['domain'] . '/' . ($content['category'][0]['alias'] ?? '') . '/' . $content['id']; + } + return '#'; + } catch (\Exception $e) { + return '#'; + } + } +} + +/** + * 根据 related_solution_names 查询文章 + * @param string $name 关键词 + * @param int $sellerId 商户ID + * @param int $limit 限制数量 + * @return array + */ +if (!function_exists('getArticlesBySolutionName')) { + function getArticlesBySolutionName(string $name, int $sellerId, int $limit = 10): array + { + if (empty($name)) { + return []; + } + try { + // 查询主表获取 sub_id + $articles = \app\model\customModel\Article::where('seller_id', $sellerId) + ->where('related_solution_names', 'like', '%' . $name . '%') + ->select() + ->toArray(); + + if (empty($articles)) { + return []; + } + $subIds = array_column($articles, 'sub_id'); + // 查询副表中 related_solution_names 匹配的内容 + $result = \app\model\SubContent::whereIn('id', $subIds) + ->where('seller_id', $sellerId) + ->where('is_del', 1) + ->with(['thumbnail' => function ($query) { + $query->field('id,name,url,type'); + }]) + ->field('id,title,description,thumbnail,category_id') + ->order('publish_time desc') + ->limit($limit) + ->select() + ->toArray(); + return $result; + } catch (\Exception $e) { + return []; + } + } +} + +if (!function_exists('getOtherCategoryCategoryId')) { + /** + * 根据分类ID,查询同父级下的其他分类及每个分类下最新一条内容 + * @param int $categoryId 分类ID + * @param int $sellerId 商户ID + * @param int $limit 每个分类返回的内容条数 + * @return array + */ + function getOtherCategoryCategoryId(int $categoryId, int $sellerId, int $limit = 1): array + { + if (empty($categoryId)) { + return []; + } + try { + $categoryModel = new \app\model\Category(); + + // 1. 查询当前分类 + $currentCate = $categoryModel->where('id', $categoryId) + ->where('seller_id', $sellerId) + ->find(); + + if (empty($currentCate)) { + return []; + } + + // 2. 获取父级分类ID + $parentId = $currentCate['parent_id'] ?? 0; + + // 3. 查询父级下的其他兄弟分类(排除当前分类) + $where = [ + ['seller_id', '=', $sellerId], + ['id', '<>', $categoryId], + ]; + if ($parentId == 0) { + $where[] = ['parent_id', '=', 0]; + } else { + $where[] = ['parent_id', '=', $parentId]; + } + + $siblingCates = $categoryModel->where($where) + ->with(['thumbnail' => function ($query) { + $query->field('id,name,url,type'); + }]) + ->order('sort asc') + ->select() + ->toArray(); + + if (empty($siblingCates)) { + return []; + } + + // 4. 查询每个分类下最新一条内容 + $cateSubModel = new \app\model\CategorySubContent(); + $subContentModel = new \app\model\SubContent(); + + foreach ($siblingCates as &$cate) { + $cateSubIds = $cateSubModel->where('category_id', $cate['id']) + ->where('seller_id', $sellerId) + ->column('sub_content_id'); + + if (!empty($cateSubIds)) { + $content = $subContentModel->whereIn('id', $cateSubIds) + ->where('seller_id', $sellerId) + ->where('is_del', 1) + ->with(['thumbnail' => function ($query) { + $query->field('id,name,url,type'); + }]) + ->field('id,title,description') + ->order('publish_time desc') + ->limit($limit) + ->select() + ->toArray(); + + $cate['articles'] = $content; + } else { + $cate['articles'] = []; + } + } + + return $siblingCates; + } catch (\Exception $e) { + return []; + } + } +} + +/** + * 根据 related_solution_names 查询文章 + 主副栏目内容,合并打乱返回 + * @param string $solutionName 关键词 + * @param int $cateId 栏目ID + * @param int $sellerId 商户ID + * @param int $limit 返回条数 + * @return array + */ +if (!function_exists('getMixContentBySolutionName')) { + function getMixContentBySolutionName(string $solutionName, int $cateId, int $sellerId, int $limit = 6): array + { + try { + // 1. 根据 related_solution_names 查询文章表(数组A) + $arrayA = []; + if (!empty($solutionName)) { + $articles = \app\model\customModel\Article::where('related_solution_names', 'like', '%' . $solutionName . '%') + ->where('seller_id', $sellerId) + ->limit($limit * 2) + ->select() + ->toArray(); + if (!empty($articles)) { + $subIds = array_column($articles, 'sub_id'); + $arrayA = \app\model\SubContent::whereIn('id', $subIds) + ->where('seller_id', $sellerId) + ->where('is_del', 1) + ->field('id,main_id,title,category_id,thumbnail') + ->order('publish_time desc') + ->limit($limit * 2) + ->select() + ->toArray(); + + // 批量查询附件URL + $thumbIds = array_unique(array_filter(array_column($arrayA, 'thumbnail'))); + if (!empty($thumbIds)) { + $attachments = \app\model\Attachment::whereIn('id', $thumbIds) + ->column('url', 'id'); + foreach ($arrayA as &$item) { + $item['thumbnail_url'] = $attachments[$item['thumbnail']] ?? ''; + } + unset($item); + } + } + } + // 2. 查询主栏目和副栏目内容(数组B) + $arrayB = []; + if (!empty($cateId)) { + $arrayB = getContentByCategory($cateId, $sellerId, $limit * 2, 'publish_time desc', ['thumbnail'], 'id,main_id,title,category_id'); + // 统一 thumbnail 字段为 URL 字符串 + foreach ($arrayB as &$item) { + $item['thumbnail_url'] = $item['thumbnail']['url'] ?? ''; + unset($item['thumbnail']); + } + unset($item); + } + + // 3. 标记来源 + foreach ($arrayA as &$item) { + $item['source'] = 'solution'; + } + unset($item); + foreach ($arrayB as &$item) { + $item['source'] = 'category'; + } + unset($item); + + // 4. 合并去重打乱(按 id 去重,优先保留 solution 来源) + $merged = []; + $seenIds = []; + foreach (array_merge($arrayB, $arrayA) as $item) { + $id = $item['id'] ?? 0; + if (!in_array($id, $seenIds)) { + $seenIds[] = $id; + $merged[] = $item; + } + } + shuffle($merged); + + // 4. 取 limit 条,并格式化返回字段 + $result = array_slice($merged, 0, $limit); + $formatted = []; + foreach ($result as $item) { + $formatted[] = [ + 'id' => $item['id'] ?? 0, + 'main_id' => $item['main_id'] ?? 0, + 'title' => $item['title'] ?? '', + 'category_id' => $item['category_id'] ?? 0, + 'thumbnail' => $item['thumbnail_url'] ?? '', + 'source' => $item['source'] ?? 'category', + ]; + } + return $formatted; + } catch (\Exception $e) { + return []; + } + } +} + diff --git a/app/controller/frontend/ListController.php b/app/controller/frontend/ListController.php index 50ebcb0..562ed58 100644 --- a/app/controller/frontend/ListController.php +++ b/app/controller/frontend/ListController.php @@ -53,8 +53,9 @@ class ListController extends BaseController // exit(); // 解决方案子分类 if (!empty($category)) { - $subCates = \app\service\ApiService::getSubCategory($category['id'], $this->sellerId, $this->siteId, $this->lang, 'id asc', 'all', '*'); + $subCates = \app\service\ApiService::getSubCategory($category['id'], $this->sellerId, $this->siteId, $this->lang, 'sort asc,id asc', 'all', '*'); $this->assign('sub_cates', $subCates); + $this->assign('seller_id', $this->sellerId); } return $this->fetch($template); } diff --git a/public/themes/website/1/zh/demo/solution.html b/public/themes/website/1/zh/demo/solution.html index 9b32285..d27633a 100644 --- a/public/themes/website/1/zh/demo/solution.html +++ b/public/themes/website/1/zh/demo/solution.html @@ -71,12 +71,15 @@

{$sub_cates[1]['title'] ?? ''}

{/if} @@ -109,11 +112,15 @@
- {hcTaglib:subcategory pid="$sub_cates[3]['id']" item="vo" sort="" } + {php} + $contents = getMixContentBySolutionName($current_cate['title'],$sub_cates[3]['id'],$seller_id, 6); + {/php} + {volist name="contents" id="vo"} - {/hcTaglib:subcategory} + {/volist}
@@ -148,9 +155,12 @@

{$sub_cates[4]['title'] ?? ''}

- {hcTaglib:subcategory pid="$sub_cates[4]['id']" item="vo" sort="" } + {php} + $contents = getContentByCategory($sub_cates[4]['id'], $seller_id, 6, 'sort asc', ['thumbnail', 'category']); + {/php} + {volist name="contents" id="vo"}
- {/hcTaglib:subcategory} + {/volist}
{/if} @@ -161,13 +171,16 @@
- {hcTaglib:subcategory pid="$sub_cates[5]['id']" item="vo" sort="" } + {php} + $contents = getContentByCategory($sub_cates[5]['id'], $seller_id, 6, 'sort asc', ['thumbnail', 'category']); + {/php} + {volist name="contents" id="vo"}

{$vo['title'] ?? ''}

{$vo['sub_title'] ?? ''}

{$vo['description'] ?? ''}

- {php}$descArr = explode(',',$vo['desc']);{/php} + {php}$descArr = explode(',',$vo['seo_description']);{/php} {if !empty($descArr)} {volist name="descArr" id="vv"} {$vv} @@ -175,7 +188,7 @@ {/if}
- {/hcTaglib:subcategory} + {/volist}
@@ -203,11 +216,14 @@ diff --git a/public/themes/website/1/zh/demo/solution2.html b/public/themes/website/1/zh/demo/solution2.html index dce9258..7e69e6f 100644 --- a/public/themes/website/1/zh/demo/solution2.html +++ b/public/themes/website/1/zh/demo/solution2.html @@ -76,15 +76,18 @@ $current_cate_id = $current_cate['id'] ?? 0;

{$sub_cates[1]['title'] ?? ''}

    - {hcTaglib:subcategory pid="$sub_cates[1]['id']" sort="id asc,sort asc" item="vo"} -
  • - -

    {$vo.title ?? ''}

    -

    {$vo.description ?? ''|raw}

    -
  • - {/hcTaglib:subcategory} + {php} + $contents = getContentByCategory($sub_cates[1]['id'], $seller_id, 6, 'sort asc', ['thumbnail', 'category']); + {/php} + {volist name="contents" id="vo"} +
  • + +

    {$vo.title ?? ''}

    +

    {$vo.description ?? ''}

    +
  • + {/volist}
{/if} @@ -117,19 +120,22 @@ $current_cate_id = $current_cate['id'] ?? 0;
- {hcTaglib:subcategory pid="$sub_cates[3]['id']" item="vo" sort=""} + {php} + $products = getArticlesBySolutionName($current_cate['title'],$seller_id,6); + {/php} + {volist name="products" id="vo"} - {/hcTaglib:subcategory} + {/volist}
@@ -156,7 +162,10 @@ $current_cate_id = $current_cate['id'] ?? 0;
- {hcTaglib:subcategory pid="$sub_cates[4]['id']" item="vo" sort="" } + {php} + $contents = getContentByCategory($sub_cates[4]['id'], $seller_id, 6, 'sort asc', ['thumbnail', 'category']); + {/php} + {volist name="contents" id="vo"}
@@ -164,11 +173,11 @@ $current_cate_id = $current_cate['id'] ?? 0;

{$vo['title'] ?? ''}

- {$vo['description'] ?? ''} + {$vo.description ?? ''}

- {/hcTaglib:subcategory} + {/volist}
@@ -194,8 +203,9 @@ $current_cate_id = $current_cate['id'] ?? 0;

{$sub_cates[5]['title'] ?? ''}

- {/hcTaglib:subcategory} + {/volist}
{/if}