优化搜索界面及控制器

This commit is contained in:
李志强 2025-06-06 11:15:11 +08:00
parent a81aff626e
commit eca0f18501
3 changed files with 583 additions and 239 deletions

View File

@ -28,14 +28,14 @@ class SearchController extends BaseController
$detailUrl = '/index/articles/detail';
$categoryUrl = '/index/articles/category';
$status = 2; // 文章状态为2
$fields = 'id, title, cate, image, author, create_time as publishdate';
$fields = 'id, title, cate, image, author, FROM_UNIXTIME(create_time, "%Y-%m-%d") as publishdate';
} else if ($type == 'resource') {
$model = new Resources();
$categoryModel = new ResourcesCategory();
$detailUrl = '/index/resources/detail';
$categoryUrl = '/index/resources/category';
$status = 1; // 资源状态为1
$fields = 'id, title, cate, icon, author, create_time as publishdate';
$fields = 'id, title, cate, icon, uploader, FROM_UNIXTIME(create_time, "%Y-%m-%d") as publishdate';
} else {
$this->error('无效的搜索类型');
}
@ -61,6 +61,7 @@ class SearchController extends BaseController
->find();
$item['category'] = $category ?: ['id' => 0, 'name' => '未分类', 'image' => ''];
$item['cate'] = $item['category']['name']; // 使用分类名称替换分类ID
// 如果文章的图片为空,使用分类的图片
if (empty($item['image'])) {
@ -71,10 +72,11 @@ class SearchController extends BaseController
}
} else {
$category = $categoryModel->where('id', $item['cate'])
->field('id, name, icon')
->field('id, name, icon, cid')
->find();
$item['category'] = $category ?: ['id' => 0, 'name' => '未分类', 'icon' => ''];
$item['category'] = $category ?: ['id' => 0, 'name' => '未分类', 'icon' => '', 'cid' => 0];
$item['cate'] = $item['category']['name']; // 使用分类名称替换分类ID
// 如果资源的图片为空,使用分类的图片
if (empty($item['icon'])) {
@ -83,6 +85,13 @@ class SearchController extends BaseController
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']]);
}
}
}

View File

@ -19,29 +19,24 @@
{if $items}
{volist name="items" id="item"}
<div class="layui-col-md12">
<div class="layui-card">
<div class="layui-card-body">
<div style="display: flex; align-items: center;">
<div style="margin-right: 20px;">
{if $type == 'article'}
<img src="{$item.image}" alt="{$item.title}" style="width: 250px; height: 140px; object-fit: cover;">
{else}
<img src="{$item.icon}" alt="{$item.title}" style="width: 250px; height: 140px; object-fit: cover;">
{/if}
</div>
<div class="layui-col-md8">
<h3 class="layui-text">
<a href="{$detailUrl}?id={$item.id}" class="layui-text" style="color: #333;">{$item.title}</a>
</h3>
<div class="layui-text" style="margin: 10px 0;">
<span class="layui-badge layui-bg-gray">{$item.category.name}</span>
<span class="layui-badge layui-bg-gray">{$item.author}</span>
<span class="layui-badge layui-bg-gray">{$item.publishdate|date="Y-m-d"}</span>
</div>
<div class="layui-text" style="margin-top: 10px;">
<a href="{$detailUrl}?id={$item.id}" class="layui-btn layui-btn-sm layui-btn-normal">查看详情</a>
</div>
</div>
<div class="search-result-item" onclick="window.location.href='<?php echo $type == 'article' ? url('article/detail', ['id' => $item['id']]) : $item['detail_url'] ?>'">
<div class="search-result-image">
<?php if($type == 'article'): ?>
<img src="<?php echo $item['image'] ?: '/static/images/default.jpg' ?>" alt="<?php echo $item['title'] ?>">
<?php else: ?>
<img src="<?php echo $item['icon'] ?: '/static/images/default.jpg' ?>" alt="<?php echo $item['title'] ?>">
<?php endif; ?>
</div>
<div class="search-result-content">
<h3 class="search-result-title"><?php echo $item['title'] ?></h3>
<div class="search-result-meta">
<span class="layui-badge layui-bg-blue"><?php echo $item['cate'] ?></span>
<?php if($type == 'article'): ?>
<span class="layui-badge layui-bg-green">作者:<?php echo $item['author'] ?></span>
<?php else: ?>
<span class="layui-badge layui-bg-green">上传者:<?php echo $item['uploader'] ?></span>
<?php endif; ?>
<span class="layui-badge layui-bg-gray"><?php echo $item['publishdate'] ?></span>
</div>
</div>
</div>
@ -86,3 +81,52 @@ layui.use(['laypage'], function(){
});
});
</script>
<style>
.search-result-item {
display: flex;
align-items: center;
padding: 15px;
transition: all 0.3s ease;
border-radius: 4px;
background-color: #fff;
}
.search-result-item:hover {
background-color:rgb(255, 255, 255);
transform: translateY(-2px);
box-shadow: 0 2px 8px rgba(0, 174, 255, 0.5);
}
.search-result-image {
margin-right: 20px;
flex-shrink: 0;
}
.search-result-image img {
width: 250px;
height: 140px;
object-fit: cover;
border-radius: 4px;
}
.search-result-content {
flex: 1;
}
.search-result-title {
font-size: 18px;
color: #333;
margin: 0 0 10px 0;
}
.search-result-meta {
display: flex;
gap: 10px;
flex-wrap: wrap;
}
.search-result-meta .layui-badge {
margin-right: 5px;
}
</style>

View File

@ -1,4 +1,4 @@
<?php /*a:5:{s:62:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\program\detail.php";i:1748423318;s:62:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\head.php";i:1747617129;s:71:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\header-simple.php";i:1748423658;s:64:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\footer.php";i:1747617266;s:62:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\foot.php";i:1747616844;}*/ ?>
<?php /*a:5:{s:62:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\program\detail.php";i:1748423318;s:62:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\head.php";i:1747617129;s:71:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\header-simple.php";i:1749174451;s:64:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\footer.php";i:1749170849;s:62:"E:\Demos\DemoOwns\PHP\yunzer\app\index\view\component\foot.php";i:1747616844;}*/ ?>
<!DOCTYPE html>
<html>
@ -66,6 +66,9 @@ $loginStatus = [
<li><a href="/index/game/index?cateid=8">游戏下载</a></li>
</ul>
</div>
<div class="main-menu__search">
<i class="layui-icon layui-icon-search search-icon" id="mainSearchIcon"></i>
</div>
<div class="main-menu__right">
<div class="username">
<?php if ($userInfo['is_login']): ?>
@ -123,6 +126,9 @@ $loginStatus = [
<li><a href="/index/game/index?cateid=8">游戏下载</a></li>
</ul>
</div>
<div class="sticky-nav__search">
<i class="layui-icon layui-icon-search search-icon" id="stickySearchIcon"></i>
</div>
<div class="sticky-nav__right">
<div class="main-menu__right">
<div class="username">
@ -165,216 +171,19 @@ $loginStatus = [
</div>
</div>
<style>
/* 用户头像样式 */
#userAvatar {
width: 40px;
height: 40px;
cursor: pointer;
transition: all 0.3s ease;
}
#userAvatar:hover {
transform: scale(1.05);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
/* 下拉菜单容器 */
.user-dropdown {
position: absolute;
top: 50px;
right: 0;
width: 160px;
background: #fff;
border-radius: 4px;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
opacity: 0;
visibility: hidden;
transform: translateY(-10px);
transition: all 0.3s ease;
z-index: 9999;
}
.user-dropdown.show {
opacity: 1;
visibility: visible;
transform: translateY(0);
}
/* 下拉菜单列表 */
.user-dropdown ul {
margin: 0;
padding: 5px 0;
list-style: none;
}
/* 下拉菜单项 */
.user-dropdown li {
margin: 0;
padding: 0;
}
/* 下拉菜单链接 */
.user-dropdown li a {
display: flex;
align-items: center;
padding: 10px 15px;
color: #333;
text-decoration: none;
transition: all 0.3s ease;
}
/* 下拉菜单图标 */
.user-dropdown li a i {
margin-right: 10px;
font-size: 16px;
color: #666;
}
/* 下拉菜单文字 */
.user-dropdown li a span {
font-size: 14px;
}
/* 下拉菜单悬停效果 */
.user-dropdown li a:hover {
background: #f5f5f5;
color: #1E9FFF;
}
.user-dropdown li a:hover i {
color: #1E9FFF;
}
/* 分隔线 */
.user-dropdown li:not(:last-child) {
border-bottom: 1px solid #f0f0f0;
}
#userDropdownSticky a {
color: #0d6efd !important;
}
/* Banner样式 */
.banner-content {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
}
.banner-image {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.banner-image img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
.banner-text {
position: absolute;
top: 40%;
left: 10%;
z-index: 1;
display: flex;
flex-direction: column;
align-items: flex-start;
color: #fff;
}
.banner-text a {
text-decoration: none;
margin-top: 30px;
}
.banner-title {
font-size: 4em;
font-weight: 600;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}
.banner-desc {
font-size: 2em;
font-weight: 400;
max-width: 800px;
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
}
.banner-btn {
background: #fff;
color: #000;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
transition: all 0.3s ease;
}
.banner-btn:hover {
background: #000;
color: #fff;
}
.banner-slider {
width: 100%;
height: 86vh;
overflow: hidden;
position: relative;
}
.banner-container {
width: 100%;
height: 100%;
}
.banner-slide {
display: block;
width: 100%;
height: 100%;
}
.banner-slide img {
width: 100%;
height: 100%;
object-fit: cover;
/* 关键:等比缩放并铺满 */
display: block;
}
.layui-carousel {
background: #f8f8f8;
margin: 0;
padding: 0;
}
/* 确保轮播容器和项目的高度正确 */
#test10,
#test10 [carousel-item],
#test10 [carousel-item]>* {
height: 86vh !important;
}
#test10 [carousel-item]>* {
background: none !important;
}
.main-menu__right {
display: flex;
align-items: center;
}
.username {
display: flex;
align-items: center;
}
</style>
<!-- 搜索遮罩 -->
<div class="search-mask" id="searchMask">
<div class="search-container">
<div class="search-box">
<select id="searchType" class="search-type">
<option value="article">文章</option>
<option value="resource">资源</option>
</select>
<input type="text" id="searchInput" placeholder="请输入搜索关键词">
<button id="searchBtn">搜索</button>
</div>
</div>
</div>
<script>
// 在页面加载时立即执行
@ -621,7 +430,485 @@ $loginStatus = [
return false;
});
});
// 搜索功能相关代码
layui.use(['layer'], function () {
var layer = layui.layer;
var $ = layui.jquery;
// 执行搜索
function executeSearch() {
var searchInput = document.getElementById('searchInput');
if (!searchInput) {
layer.msg('搜索组件初始化失败');
return;
}
var keyword = searchInput.value.trim();
var type = document.getElementById('searchType').value;
if (!keyword) {
layer.msg('请输入搜索关键词');
return;
}
// 跳转到统一的搜索结果页面
window.location.href = '/index/search/index?keyword=' + encodeURIComponent(keyword) + '&type=' + type;
}
// 绑定事件
$(function() {
var searchMask = $('#searchMask');
var searchInput = $('#searchInput');
var searchBtn = $('#searchBtn');
var mainSearchIcon = $('#mainSearchIcon');
var stickySearchIcon = $('#stickySearchIcon');
// 显示搜索框
function showSearch() {
searchMask.addClass('show');
setTimeout(function() {
searchInput.focus();
}, 300);
}
// 隐藏搜索框
function hideSearch() {
searchMask.removeClass('show');
searchInput.val('');
}
// 绑定搜索图标点击事件
mainSearchIcon.on('click', showSearch);
stickySearchIcon.on('click', showSearch);
// 绑定搜索按钮点击事件
searchBtn.on('click', function(e) {
e.preventDefault();
executeSearch();
});
// 绑定回车键搜索
searchInput.on('keypress', function(e) {
if (e.which === 13) {
e.preventDefault();
executeSearch();
}
});
// 点击遮罩层关闭搜索框
searchMask.on('click', function(e) {
if ($(e.target).hasClass('search-mask')) {
hideSearch();
}
});
// 绑定ESC键关闭搜索框
$(document).on('keydown', function(e) {
if (e.keyCode === 27 && searchMask.hasClass('show')) {
hideSearch();
}
});
// 输入框获得焦点时选中所有文本
searchInput.on('focus', function() {
this.select();
});
});
});
</script>
<style>
/* 用户头像样式 */
#userAvatar {
width: 40px;
height: 40px;
cursor: pointer;
transition: all 0.3s ease;
}
#userAvatar:hover {
transform: scale(1.05);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
/* 下拉菜单容器 */
.user-dropdown {
position: absolute;
top: 50px;
right: 0;
width: 160px;
background: #fff;
border-radius: 4px;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
opacity: 0;
visibility: hidden;
transform: translateY(-10px);
transition: all 0.3s ease;
z-index: 9999;
}
.user-dropdown.show {
opacity: 1;
visibility: visible;
transform: translateY(0);
}
/* 下拉菜单列表 */
.user-dropdown ul {
margin: 0;
padding: 5px 0;
list-style: none;
}
/* 下拉菜单项 */
.user-dropdown li {
margin: 0;
padding: 0;
}
/* 下拉菜单链接 */
.user-dropdown li a {
display: flex;
align-items: center;
padding: 10px 15px;
color: #333;
text-decoration: none;
transition: all 0.3s ease;
}
/* 下拉菜单图标 */
.user-dropdown li a i {
margin-right: 10px;
font-size: 16px;
color: #666;
}
/* 下拉菜单文字 */
.user-dropdown li a span {
font-size: 14px;
}
/* 下拉菜单悬停效果 */
.user-dropdown li a:hover {
background: #f5f5f5;
color: #1E9FFF;
}
.user-dropdown li a:hover i {
color: #1E9FFF;
}
/* 分隔线 */
.user-dropdown li:not(:last-child) {
border-bottom: 1px solid #f0f0f0;
}
#userDropdownSticky a {
color: #0d6efd !important;
}
/* Banner样式 */
.banner-content {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
}
.banner-image {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.banner-image img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
.banner-text {
position: absolute;
top: 40%;
left: 10%;
z-index: 1;
display: flex;
flex-direction: column;
align-items: flex-start;
color: #fff;
}
.banner-text a {
text-decoration: none;
margin-top: 30px;
}
.banner-title {
font-size: 4em;
font-weight: 600;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}
.banner-desc {
font-size: 2em;
font-weight: 400;
max-width: 800px;
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
}
.banner-btn {
background: #fff;
color: #000;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
transition: all 0.3s ease;
}
.banner-btn:hover {
background: #000;
color: #fff;
}
.banner-slider {
width: 100%;
height: 86vh;
overflow: hidden;
position: relative;
}
.banner-container {
width: 100%;
height: 100%;
}
.banner-slide {
display: block;
width: 100%;
height: 100%;
}
.banner-slide img {
width: 100%;
height: 100%;
object-fit: cover;
/* 关键:等比缩放并铺满 */
display: block;
}
.layui-carousel {
background: #f8f8f8;
margin: 0;
padding: 0;
}
/* 确保轮播容器和项目的高度正确 */
#test10,
#test10 [carousel-item],
#test10 [carousel-item]>* {
height: 86vh !important;
}
#test10 [carousel-item]>* {
background: none !important;
}
.main-menu__right {
display: flex;
align-items: center;
}
.username {
display: flex;
align-items: center;
}
/* 搜索相关样式 */
.search-mask {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
z-index: 9999;
display: none;
opacity: 0;
transition: opacity 0.3s ease;
justify-content: center;
align-items: center;
}
.search-mask.show {
display: flex;
opacity: 1;
}
.search-container {
position: relative;
width: 80%;
padding: 20px;
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
transform: translateY(-20px);
transition: transform 0.3s ease;
}
.search-mask.show .search-container {
transform: translateY(0);
}
.search-box {
display: flex;
align-items: center;
height: 60px;
background: #fff;
border-radius: 30px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
overflow: hidden;
}
.search-box input {
flex: 1;
height: 100%;
padding: 0 20px;
border: none;
outline: none;
font-size: 16px;
}
.search-box button {
height: 100%;
padding: 0 30px;
border: none;
background: #1E9FFF;
color: #fff;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s;
}
.search-box button:hover {
background: #1a8fe6;
}
.search-icon {
font-size: 20px;
cursor: pointer;
color: #333;
transition: color 0.3s ease;
}
.search-icon:hover {
color: #1e9fff;
}
.search-type {
height: 100%;
padding: 0 15px;
border: none;
border-right: 1px solid #eee;
background: #f8f8f8;
color: #666;
font-size: 14px;
cursor: pointer;
outline: none;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
background-repeat: no-repeat;
background-position: right 10px center;
background-size: 12px;
padding-right: 30px;
}
.search-type:hover {
background-color: #f0f0f0;
}
.search-type:focus {
background-color: #fff;
box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
}
.search-results {
padding: 15px;
max-height: 370px;
overflow-y: auto;
}
.search-section {
margin-bottom: 20px;
}
.search-section h3 {
font-size: 16px;
color: #333;
margin-bottom: 10px;
padding-bottom: 5px;
border-bottom: 1px solid #eee;
}
.search-section ul {
list-style: none;
padding: 0;
margin: 0;
}
.search-section li {
padding: 8px 0;
border-bottom: 1px dashed #eee;
display: flex;
align-items: center;
justify-content: space-between;
}
.search-section li:last-child {
border-bottom: none;
}
.search-section a {
color: #333;
text-decoration: none;
flex: 1;
}
.search-section a:hover {
color: #1E9FFF;
}
.search-section .downloads {
color: #1E9FFF;
font-size: 12px;
margin-left: 10px;
}
/* 自定义滚动条样式 */
.search-results::-webkit-scrollbar {
width: 6px;
}
.search-results::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 3px;
}
.search-results::-webkit-scrollbar-thumb {
background: #ccc;
border-radius: 3px;
}
.search-results::-webkit-scrollbar-thumb:hover {
background: #999;
}
</style>
<div class="main">
<div class="location">
<div class="container">
@ -825,6 +1112,10 @@ $loginStatus = [
<p class="copyright__text">Copyright <span class="dynamic-year">2025</span> | All Rights By <a
href="http://www.yunzer.cn">Yunzer</a></p>
</div>
<div class="container wow fadeInUp animated" data-wow-delay="400ms"
style="visibility: visible; animation-delay: 400ms; animation-name: fadeInUp;">
<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow"><?php echo htmlentities((string) $config['admin_icp']); ?></a>
</div>
<div class="tongji">
<script id="LA-DATA-WIDGET" crossorigin="anonymous" charset="UTF-8"
src="https://v6-widget.51.la/v6/KoyzaWWEcLvPzkQn/quote.js?theme=#1690FF,#FFFFFF,#999999,#FFFFFF,#FFFFFF,#1690FF,12&f=12"></script>