|
|
|
@ -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 []; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|