256 lines
9.4 KiB
PHP
256 lines
9.4 KiB
PHP
<?php
|
||
/**
|
||
* 商业使用授权协议
|
||
*
|
||
* Copyright (c) 2025 [云泽网]. 保留所有权利.
|
||
*
|
||
* 本软件仅供评估使用。任何商业用途必须获得书面授权许可。
|
||
* 未经授权商业使用本软件属于侵权行为,将承担法律责任。
|
||
*
|
||
* 授权购买请联系: 357099073@qq.com
|
||
* 官方网站: https://www.yunzer.cn
|
||
*
|
||
* 评估用户须知:
|
||
* 1. 禁止移除版权声明
|
||
* 2. 禁止用于生产环境
|
||
* 3. 禁止转售或分发
|
||
*/
|
||
|
||
namespace app\index\controller;
|
||
|
||
use think\Db;
|
||
use app\index\controller\BaseController;
|
||
use app\index\model\Articles\Articles;
|
||
use app\index\model\Articles\ArticlesCategory;
|
||
use app\index\model\Resources\Resources;
|
||
use app\index\model\Resources\ResourcesCategory;
|
||
|
||
class SearchController extends BaseController
|
||
{
|
||
public function index()
|
||
{
|
||
$keyword = input('keyword', '');
|
||
$type = input('type', 'articles'); // 搜索类型:articles-文章,resources-资源
|
||
$page = input('page', 1);
|
||
$limit = input('limit', 10);
|
||
|
||
if (empty($keyword)) {
|
||
$this->error('请输入搜索关键词');
|
||
}
|
||
|
||
// 根据类型选择对应的表和分类表
|
||
if ($type == 'articles') {
|
||
$model = new Articles();
|
||
$categoryModel = new ArticlesCategory();
|
||
$detailUrl = '/index/articles/detail';
|
||
$categoryUrl = '/index/articles/category';
|
||
$status = 2; // 文章状态为2
|
||
$fields = 'id, title, cate, image, author, FROM_UNIXTIME(create_time, "%Y-%m-%d") as publishdate';
|
||
} else if ($type == 'resources') {
|
||
$model = new Resources();
|
||
$categoryModel = new ResourcesCategory();
|
||
$detailUrl = '/index/resources/detail';
|
||
$categoryUrl = '/index/resources/category';
|
||
$status = 1; // 资源状态为1
|
||
$fields = 'id, title, cate, icon, uploader, FROM_UNIXTIME(create_time, "%Y-%m-%d") as publishdate';
|
||
} else {
|
||
$this->error('无效的搜索类型');
|
||
}
|
||
|
||
// 搜索内容
|
||
$items = $model->where('title', 'like', "%{$keyword}%")
|
||
->where('status', $status)
|
||
->field($fields)
|
||
->order('create_time desc')
|
||
->page($page, $limit)
|
||
->select();
|
||
|
||
// 获取总数
|
||
$count = $model->where('title', 'like', "%{$keyword}%")
|
||
->where('status', $status)
|
||
->count();
|
||
|
||
// 获取分类名称和图片
|
||
foreach ($items as &$item) {
|
||
if ($type == 'articles') {
|
||
$category = $categoryModel->where('id', $item['cate'])
|
||
->field('id, name, image')
|
||
->find();
|
||
|
||
$item['category'] = $category ?: ['id' => 0, 'name' => '未分类', 'image' => ''];
|
||
$item['cate'] = $item['category']['name']; // 使用分类名称替换分类ID
|
||
|
||
// 如果文章的图片为空,使用分类的图片
|
||
if (empty($item['image'])) {
|
||
$item['image'] = $item['category']['image'];
|
||
}
|
||
if (empty($item['image'])) {
|
||
$item['image'] = '/static/images/default.jpg';
|
||
}
|
||
} else {
|
||
$category = $categoryModel->where('id', $item['cate'])
|
||
->field('id, name, icon, cid')
|
||
->find();
|
||
|
||
$item['category'] = $category ?: ['id' => 0, 'name' => '未分类', 'icon' => '', 'cid' => 0];
|
||
$item['cate'] = $item['category']['name']; // 使用分类名称替换分类ID
|
||
|
||
// 如果资源的图片为空,使用分类的图片
|
||
if (empty($item['icon'])) {
|
||
$item['icon'] = $item['category']['icon'];
|
||
}
|
||
if (empty($item['icon'])) {
|
||
$item['icon'] = '/static/images/default.jpg';
|
||
}
|
||
|
||
// 根据分类cid判断资源类型
|
||
if ($item['category']['cid'] == 8) {
|
||
$item['detail_url'] = url('game/detail', ['id' => $item['id']]);
|
||
} else {
|
||
$item['detail_url'] = url('program/detail', ['id' => $item['id']]);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 准备视图数据
|
||
$viewData = [
|
||
'keyword' => $keyword,
|
||
'type' => $type,
|
||
'items' => $items,
|
||
'detailUrl' => $detailUrl,
|
||
'count' => $count,
|
||
'page' => $page,
|
||
'limit' => $limit
|
||
];
|
||
|
||
return view('index', $viewData);
|
||
}
|
||
|
||
// JSON API搜索接口
|
||
public function apiSearch()
|
||
{
|
||
try {
|
||
$keyword = input('keyword', '');
|
||
$type = input('type', 'articles'); // 搜索类型:articles-文章,resources-资源
|
||
$page = input('page/d', 1);
|
||
$limit = input('limit/d', 10);
|
||
|
||
if (empty($keyword)) {
|
||
return json([
|
||
'code' => 1,
|
||
'msg' => '请输入搜索关键词'
|
||
]);
|
||
}
|
||
|
||
if (empty($type)) {
|
||
return json([
|
||
'code' => 1,
|
||
'msg' => '请选择搜索类型'
|
||
]);
|
||
}
|
||
|
||
// 根据类型选择对应的表和分类表
|
||
if ($type == 'articles') {
|
||
$model = new Articles();
|
||
$categoryModel = new ArticlesCategory();
|
||
$status = 2; // 文章状态为2
|
||
$fields = 'id, title, cate, image, author, FROM_UNIXTIME(create_time, "%Y-%m-%d") as publishdate';
|
||
} else if ($type == 'resources') {
|
||
$model = new Resources();
|
||
$categoryModel = new ResourcesCategory();
|
||
$status = 1; // 资源状态为1
|
||
$fields = 'id, title, cate, icon, uploader, FROM_UNIXTIME(create_time, "%Y-%m-%d") as publishdate';
|
||
} else {
|
||
return json([
|
||
'code' => 1,
|
||
'msg' => '无效的搜索类型'
|
||
]);
|
||
}
|
||
|
||
// 搜索内容
|
||
$items = $model->where('title', 'like', "%{$keyword}%")
|
||
->where('status', $status)
|
||
->field($fields)
|
||
->order('create_time desc')
|
||
->page($page, $limit)
|
||
->select();
|
||
|
||
// 获取总数
|
||
$count = $model->where('title', 'like', "%{$keyword}%")
|
||
->where('status', $status)
|
||
->count();
|
||
|
||
// 获取分类名称和图片
|
||
$resultItems = [];
|
||
foreach ($items as $item) {
|
||
$itemData = $item->toArray();
|
||
|
||
if ($type == 'articles') {
|
||
$category = $categoryModel->where('id', $itemData['cate'])
|
||
->field('id, name, image')
|
||
->find();
|
||
|
||
$itemData['category'] = $category ? $category->toArray() : ['id' => 0, 'name' => '未分类', 'image' => ''];
|
||
|
||
// 如果文章的图片为空,使用分类的图片
|
||
if (empty($itemData['image'])) {
|
||
$itemData['image'] = $itemData['category']['image'];
|
||
}
|
||
if (empty($itemData['image'])) {
|
||
$itemData['image'] = '/static/images/default.jpg';
|
||
}
|
||
|
||
$itemData['detail_url'] = url('articles/detail', ['id' => $itemData['id']], true, true);
|
||
} else {
|
||
$category = $categoryModel->where('id', $itemData['cate'])
|
||
->field('id, name, icon, cid')
|
||
->find();
|
||
|
||
$itemData['category'] = $category ? $category->toArray() : ['id' => 0, 'name' => '未分类', 'icon' => '', 'cid' => 0];
|
||
|
||
// 如果资源的图片为空,使用分类的图片
|
||
if (empty($itemData['icon'])) {
|
||
$itemData['icon'] = $itemData['category']['icon'];
|
||
}
|
||
if (empty($itemData['icon'])) {
|
||
$itemData['icon'] = '/static/images/default.jpg';
|
||
}
|
||
|
||
// 根据分类cid判断资源类型
|
||
if ($itemData['category']['cid'] == 8) {
|
||
$itemData['detail_url'] = url('game/detail', ['id' => $itemData['id']], true, true);
|
||
} else {
|
||
$itemData['detail_url'] = url('program/detail', ['id' => $itemData['id']], true, true);
|
||
}
|
||
}
|
||
|
||
$resultItems[] = $itemData;
|
||
}
|
||
|
||
// 计算分页信息
|
||
$totalPages = ceil($count / $limit);
|
||
|
||
return json([
|
||
'code' => 0,
|
||
'msg' => '搜索成功',
|
||
'data' => [
|
||
'keyword' => $keyword,
|
||
'type' => $type,
|
||
'items' => $resultItems,
|
||
'total' => $count,
|
||
'page' => $page,
|
||
'limit' => $limit,
|
||
'total_pages' => $totalPages,
|
||
'has_more' => $page < $totalPages
|
||
]
|
||
]);
|
||
|
||
} catch (\Exception $e) {
|
||
return json([
|
||
'code' => 1,
|
||
'msg' => '搜索失败:' . $e->getMessage()
|
||
]);
|
||
}
|
||
}
|
||
}
|