Browse Source

解决方案修改

master
loucongjun 3 weeks ago
parent
commit
8d3bbcca7e
  1. 321
      app/common.php
  2. 3
      app/controller/frontend/ListController.php
  3. 42
      public/themes/website/1/zh/demo/solution.html
  4. 36
      public/themes/website/1/zh/demo/solution2.html

321
app/common.php

@ -1308,3 +1308,324 @@ if (!function_exists('deal_image_url')) {
return $tmpUrl; 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 [];
}
}
}

3
app/controller/frontend/ListController.php

@ -53,8 +53,9 @@ class ListController extends BaseController
// exit(); // exit();
// 解决方案子分类 // 解决方案子分类
if (!empty($category)) { 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('sub_cates', $subCates);
$this->assign('seller_id', $this->sellerId);
} }
return $this->fetch($template); return $this->fetch($template);
} }

42
public/themes/website/1/zh/demo/solution.html

@ -71,12 +71,15 @@
<section class="sl-adv section__wrap" id="adv" data-header-dark> <section class="sl-adv section__wrap" id="adv" data-header-dark>
<h2 class="sl-adv__title">{$sub_cates[1]['title'] ?? ''}</h2> <h2 class="sl-adv__title">{$sub_cates[1]['title'] ?? ''}</h2>
<ul class="sl-adv__list"> <ul class="sl-adv__list">
{hcTaglib:subcategory pid="$sub_cates[1]['id']" item="vo" sort="" }
{php}
$contents = getContentByCategory($sub_cates[1]['id'], $seller_id, 6, 'sort asc', ['thumbnail', 'category']);
{/php}
{volist name="contents" id="vo"}
<li class="sl-adv__item"> <li class="sl-adv__item">
<h3 class="sl-adv__name">{$vo['title'] ?? ''}</h3> <h3 class="sl-adv__name">{$vo['title'] ?? ''}</h3>
<p class="sl-adv__text">{$vo['description'] ?? ''}</p> <p class="sl-adv__text">{$vo['description'] ?? ''}</p>
</li> </li>
{/hcTaglib:subcategory}
{/volist}
</ul> </ul>
</section> </section>
{/if} {/if}
@ -109,11 +112,15 @@
</div> </div>
<div class="sl-core__swiper swiper"> <div class="sl-core__swiper swiper">
<div class="swiper-wrapper"> <div class="swiper-wrapper">
{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"}
<article class="swiper-slide sl-core__card"> <article class="swiper-slide sl-core__card">
<a class="sl-core__card-link" href="{$vo['description'] ?? ''}">
<!-- TODO vo.source -> solution:商品 category:文章-->
<a class="sl-core__card-link" href="">
<div class="sl-core__card-media"> <div class="sl-core__card-media">
<img src="{$vo['thumbnail']['url'] ?? ''}" alt="{$vo['title'] ?? ''}" width="528" height="330" loading="lazy" />
<img src="{$vo['thumbnail'] ?? ''}" alt="{$vo['title'] ?? ''}" width="528" height="330" loading="lazy" />
</div> </div>
<div class="sl-core__card-foot"> <div class="sl-core__card-foot">
<h3 class="sl-core__card-name">{$vo['title'] ?? ''}</h3> <h3 class="sl-core__card-name">{$vo['title'] ?? ''}</h3>
@ -123,7 +130,7 @@
</div> </div>
</a> </a>
</article> </article>
{/hcTaglib:subcategory}
{/volist}
</div> </div>
</div> </div>
<div class="sl-core__arrows-mobile"> <div class="sl-core__arrows-mobile">
@ -148,9 +155,12 @@
<section class="sl-clients section__wrap" id="clients" data-header-dark> <section class="sl-clients section__wrap" id="clients" data-header-dark>
<h2 class="sl-clients__title">{$sub_cates[4]['title'] ?? ''}</h2> <h2 class="sl-clients__title">{$sub_cates[4]['title'] ?? ''}</h2>
<div class="sl-clients__grid"> <div class="sl-clients__grid">
{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"}
<div class="sl-clients__item"><img src="{$vo['thumbnail']['url'] ?? ''}" alt="" width="252" height="160" loading="lazy" /></div> <div class="sl-clients__item"><img src="{$vo['thumbnail']['url'] ?? ''}" alt="" width="252" height="160" loading="lazy" /></div>
{/hcTaglib:subcategory}
{/volist}
</div> </div>
</section> </section>
{/if} {/if}
@ -161,13 +171,16 @@
<div class="sl-cases__copy"> <div class="sl-cases__copy">
<div class="sl-cases__content-swiper swiper"> <div class="sl-cases__content-swiper swiper">
<div class="swiper-wrapper"> <div class="swiper-wrapper">
{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"}
<article class="swiper-slide sl-cases__panel"> <article class="swiper-slide sl-cases__panel">
<h3 class="sl-cases__name">{$vo['title'] ?? ''}</h3> <h3 class="sl-cases__name">{$vo['title'] ?? ''}</h3>
<p class="sl-cases__sub-name">{$vo['sub_title'] ?? ''}</p> <p class="sl-cases__sub-name">{$vo['sub_title'] ?? ''}</p>
<p class="sl-cases__desc">{$vo['description'] ?? ''}</p> <p class="sl-cases__desc">{$vo['description'] ?? ''}</p>
<div class="sl-cases__tags"> <div class="sl-cases__tags">
{php}$descArr = explode(',',$vo['desc']);{/php}
{php}$descArr = explode(',',$vo['seo_description']);{/php}
{if !empty($descArr)} {if !empty($descArr)}
{volist name="descArr" id="vv"} {volist name="descArr" id="vv"}
<span>{$vv}</span> <span>{$vv}</span>
@ -175,7 +188,7 @@
{/if} {/if}
</div> </div>
</article> </article>
{/hcTaglib:subcategory}
{/volist}
</div> </div>
</div> </div>
<div class="sl-cases__nav"> <div class="sl-cases__nav">
@ -203,11 +216,14 @@
<div class="sl-cases__stage" aria-hidden="true"> <div class="sl-cases__stage" aria-hidden="true">
<div class="sl-cases__media-swiper swiper"> <div class="sl-cases__media-swiper swiper">
<div class="swiper-wrapper"> <div class="swiper-wrapper">
{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"}
<div class="swiper-slide sl-cases__card"> <div class="swiper-slide sl-cases__card">
<img src="{$vo['thumbnail']['url'] ?? ''}" alt="" width="650" height="457" /> <img src="{$vo['thumbnail']['url'] ?? ''}" alt="" width="650" height="457" />
</div> </div>
{/hcTaglib:subcategory}
{/volist}
</div> </div>
</div> </div>
</div> </div>

36
public/themes/website/1/zh/demo/solution2.html

@ -76,15 +76,18 @@ $current_cate_id = $current_cate['id'] ?? 0;
<section class="sl2-adv section__wrap" id="adv" data-header-dark> <section class="sl2-adv section__wrap" id="adv" data-header-dark>
<h2 class="sl2-adv__title">{$sub_cates[1]['title'] ?? ''}</h2> <h2 class="sl2-adv__title">{$sub_cates[1]['title'] ?? ''}</h2>
<ul class="sl2-adv__list"> <ul class="sl2-adv__list">
{hcTaglib:subcategory pid="$sub_cates[1]['id']" sort="id asc,sort asc" item="vo"}
{php}
$contents = getContentByCategory($sub_cates[1]['id'], $seller_id, 6, 'sort asc', ['thumbnail', 'category']);
{/php}
{volist name="contents" id="vo"}
<li class="sl2-adv__item"> <li class="sl2-adv__item">
<span class="sl2-adv__icon" aria-hidden="true"> <span class="sl2-adv__icon" aria-hidden="true">
<img style="height: 48px;width: 48px;" src="{$vo.thumbnail.url ?? ''}">
<img style="height: 48px;width: 48px;" src="{$vo['thumbnail']['url'] ?? ''}">
</span> </span>
<h3 class="sl2-adv__name">{$vo.title ?? ''}</h3> <h3 class="sl2-adv__name">{$vo.title ?? ''}</h3>
<p class="sl2-adv__text">{$vo.description ?? ''|raw}</p>
<p class="sl2-adv__text">{$vo.description ?? ''}</p>
</li> </li>
{/hcTaglib:subcategory}
{/volist}
</ul> </ul>
</section> </section>
{/if} {/if}
@ -117,19 +120,22 @@ $current_cate_id = $current_cate['id'] ?? 0;
</div> </div>
<div class="sl2-core__swiper swiper"> <div class="sl2-core__swiper swiper">
<div class="swiper-wrapper"> <div class="swiper-wrapper">
{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"}
<article class="swiper-slide sl2-core__slide"> <article class="swiper-slide sl2-core__slide">
<div class="sl2-core__content"> <div class="sl2-core__content">
<h3 class="sl2-core__name">{$vo.title ?? ''}</h3> <h3 class="sl2-core__name">{$vo.title ?? ''}</h3>
<p class="sl2-core__desc">{$vo.description ?? ''}</p> <p class="sl2-core__desc">{$vo.description ?? ''}</p>
<a class="s-btn s-btn--primary s-btn--pill" href="/product.html">探索更多</a>
<a class="s-btn s-btn--primary s-btn--pill" href="{:hcUrl('detail/index',['id'=>$vo['id'],'cid'=>$vo['category_id']])}">探索更多</a>
</div> </div>
<div class="sl2-core__media"> <div class="sl2-core__media">
<img src="{$vo.thumbnail.url ?? ''}" alt="{$vo.title ?? ''}" width="520" height="520" <img src="{$vo.thumbnail.url ?? ''}" alt="{$vo.title ?? ''}" width="520" height="520"
loading="lazy" /> loading="lazy" />
</div> </div>
</article> </article>
{/hcTaglib:subcategory}
{/volist}
</div> </div>
<div class="sl2-core__pagination"></div> <div class="sl2-core__pagination"></div>
</div> </div>
@ -156,7 +162,10 @@ $current_cate_id = $current_cate['id'] ?? 0;
</div> </div>
<div class="sl2-scene__swiper swiper"> <div class="sl2-scene__swiper swiper">
<div class="swiper-wrapper"> <div class="swiper-wrapper">
{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"}
<article class="swiper-slide sl2-scene__slide sl2-scene__slide--detail"> <article class="swiper-slide sl2-scene__slide sl2-scene__slide--detail">
<img class="sl2-scene__bg" src="{$vo.thumbnail.url ?? ''}" alt="" width="960" <img class="sl2-scene__bg" src="{$vo.thumbnail.url ?? ''}" alt="" width="960"
height="560" loading="lazy" /> height="560" loading="lazy" />
@ -164,11 +173,11 @@ $current_cate_id = $current_cate['id'] ?? 0;
<div class="sl2-scene__panel"> <div class="sl2-scene__panel">
<h3 class="sl2-scene__name">{$vo['title'] ?? ''}</h3> <h3 class="sl2-scene__name">{$vo['title'] ?? ''}</h3>
<p class="sl2-scene__desc"> <p class="sl2-scene__desc">
{$vo['description'] ?? ''}
{$vo.description ?? ''}
</p> </p>
</div> </div>
</article> </article>
{/hcTaglib:subcategory}
{/volist}
</div> </div>
</div> </div>
<div class="sl2-scene__arrows-mobile"> <div class="sl2-scene__arrows-mobile">
@ -194,8 +203,9 @@ $current_cate_id = $current_cate['id'] ?? 0;
<section class="sl2-more section__wrap" id="more" data-header-dark> <section class="sl2-more section__wrap" id="more" data-header-dark>
<h2 class="sl2-more__title">{$sub_cates[5]['title'] ?? ''}</h2> <h2 class="sl2-more__title">{$sub_cates[5]['title'] ?? ''}</h2>
<div class="sl2-more__grid"> <div class="sl2-more__grid">
{hcTaglib:subcategory pid="$sub_cates[5]['id']" item="vo" sort="" }
<a class="sl2-more__card" href="{$vo.description ?? ''}">
{php}$others = getOtherCategoryCategoryId($current_cate['id'],$seller_id,5);{/php}
{volist name="others" id="vo"}
<a class="sl2-more__card" href="">
<div class="sl2-more__media"> <div class="sl2-more__media">
<img src="{$vo.thumbnail.url ?? ''}" alt="" width="400" height="300" loading="lazy" /> <img src="{$vo.thumbnail.url ?? ''}" alt="" width="400" height="300" loading="lazy" />
</div> </div>
@ -209,7 +219,7 @@ $current_cate_id = $current_cate['id'] ?? 0;
</span> </span>
</div> </div>
</a> </a>
{/hcTaglib:subcategory}
{/volist}
</div> </div>
</section> </section>
{/if} {/if}

Loading…
Cancel
Save