This commit is contained in:
mzltMpfqGS
2026-04-15 19:45:16 +08:00
commit e4a087e67e
301 changed files with 35048 additions and 0 deletions

View File

@@ -0,0 +1,152 @@
// pages/search/search.js
// 搜索逻辑处理
const app = getApp()
const api = require('../../services/api')
Page({
data: {
keyword: '',
hotSearchList: [],
historyList: [],
searchResult: [],
page: 1,
pageSize: 20,
hasMore: true,
loading: false,
showResult: false
},
onLoad() {
this.loadHotSearch()
this.loadHistorySearch()
},
onShow() {
this.loadHistorySearch()
},
// 加载热门搜索
loadHotSearch() {
api.getHotSearch().then(res => {
if (res.code === 200) {
this.setData({ hotSearchList: res.data || [] })
}
})
},
// 加载搜索历史
loadHistorySearch() {
const history = wx.getStorageSync('searchHistory') || []
this.setData({ historyList: history })
},
// 保存搜索历史
saveSearchHistory(keyword) {
if (!keyword) return
let history = wx.getStorageSync('searchHistory') || []
history = history.filter(item => item !== keyword)
history.unshift(keyword)
history = history.slice(0, 20)
wx.setStorageSync('searchHistory', history)
this.setData({ historyList: history })
},
// 搜索输入
onSearchInput(e) {
this.setData({ keyword: e.detail.value })
},
// 执行搜索
onSearch() {
const keyword = this.data.keyword.trim()
if (!keyword) {
wx.showToast({ title: '请输入搜索关键词', icon: 'none' })
return
}
this.saveSearchHistory(keyword)
this.setData({ showResult: true, page: 1, searchResult: [], hasMore: true })
this.loadSearchResult()
},
// 加载搜索结果
loadSearchResult() {
if (this.data.loading || !this.data.hasMore) return
this.setData({ loading: true })
api.search({
keyword: this.data.keyword,
page: this.data.page,
pageSize: this.data.pageSize
}).then(res => {
this.setData({ loading: false })
if (res.code === 200) {
const list = res.data.list || []
this.setData({
searchResult: this.data.page === 1 ? list : this.data.searchResult.concat(list),
hasMore: list.length >= this.data.pageSize
})
}
}).catch(() => {
this.setData({ loading: false })
})
},
// 触底加载更多
onReachBottom() {
if (this.data.showResult && this.data.hasMore && !this.data.loading) {
this.setData({ page: this.data.page + 1 })
this.loadSearchResult()
}
},
// 点击热门搜索词
onHotSearchTap(e) {
const keyword = e.currentTarget.dataset.keyword
this.setData({ keyword })
this.onSearch()
},
// 点击历史搜索词
onHistorySearchTap(e) {
const keyword = e.currentTarget.dataset.keyword
this.setData({ keyword })
this.onSearch()
},
// 清空历史记录
clearHistory() {
wx.showModal({
title: '提示',
content: '确定清空搜索历史吗?',
success: res => {
if (res.confirm) {
wx.removeStorageSync('searchHistory')
this.setData({ historyList: [] })
}
}
})
},
// 跳转到文章详情
goToDetail(e) {
const articleId = e.currentTarget.dataset.id
wx.navigateTo({
url: `/pages/article-detail/article-detail?id=${articleId}`
})
},
// 取消搜索
onCancel() {
wx.navigateBack()
}
})
/**
* @联系作者:16768118056
* @描述:(此项目非免费分享)源码百分百可用,搞定毕设不发愁,支持远程调试安装、二次开发、定制、讲解、文档类。
* @访问:https://www.notmaker.com/detail/8326a897119d4542896c863a69d7bbe7/gtt20260415,查看完整运行演示。
*/

View File

@@ -0,0 +1,11 @@
{
"usingComponents": {},
"navigationBarTitleText": "搜索",
"enablePullDownRefresh": false
}
/**
* @联系作者:16768118056
* @描述:(此项目非免费分享)源码百分百可用,搞定毕设不发愁,支持远程调试安装、二次开发、定制、讲解、文档类。
* @访问:https://www.notmaker.com/detail/8326a897119d4542896c863a69d7bbe7/gtt20260415,查看完整运行演示。
*/

View File

@@ -0,0 +1,99 @@
<!-- pages/search/search.wxml -->
<!-- 搜索视图 -->
<view class="search-container">
<!-- 搜索框 -->
<view class="search-bar">
<view class="search-input-wrapper">
<text class="iconfont icon-search search-icon"></text>
<input
class="search-input"
type="text"
placeholder="搜索文章、用户..."
bindinput="onSearchInput"
bindconfirm="onSearch"
value="{{keyword}}"
focus="{{true}}"
/>
<text class="iconfont icon-close" wx:if="{{keyword}}" bindtap="onClear"></text>
</view>
<text class="cancel-btn" bindtap="onCancel">取消</text>
</view>
<!-- 搜索结果 -->
<view class="search-result" wx:if="{{showResult}}">
<view class="result-list">
<view class="result-item" wx:for="{{searchResult}}" wx:key="id" bindtap="goToDetail" data-id="{{item.id}}">
<image class="result-cover" wx:if="{{item.coverImage}}" src="{{item.coverImage}}" mode="aspectFill" />
<view class="result-info">
<text class="result-title">{{item.title}}</text>
<text class="result-summary">{{item.summary}}</text>
<view class="result-meta">
<text class="author">{{item.authorName}}</text>
<text class="time">{{item.createTime}}</text>
</view>
</view>
</view>
</view>
<view class="loading-more" wx:if="{{loading}}">
<text>加载中...</text>
</view>
<view class="no-more" wx:if="{{!hasMore && searchResult.length > 0}}">
<text>没有更多了</text>
</view>
<view class="empty-state" wx:if="{{!loading && searchResult.length === 0}}">
<text class="iconfont icon-search empty-icon"></text>
<text class="empty-text">未找到相关结果</text>
</view>
</view>
<!-- 搜索历史和热门搜索 -->
<view class="search-content" wx:if="{{!showResult}}">
<!-- 搜索历史 -->
<view class="history-section" wx:if="{{historyList.length > 0}}">
<view class="section-header">
<text class="section-title">搜索历史</text>
<text class="iconfont icon-trash" bindtap="clearHistory"></text>
</view>
<view class="history-list">
<text
class="history-item"
wx:for="{{historyList}}"
wx:key="index"
bindtap="onHistorySearchTap"
data-keyword="{{item}}"
>{{item}}</text>
</view>
</view>
<!-- 热门搜索 -->
<view class="hot-section">
<view class="section-header">
<text class="section-title">热门搜索</text>
<text class="iconfont icon-fire hot-icon"></text>
</view>
<view class="hot-list">
<view
class="hot-item"
wx:for="{{hotSearchList}}"
wx:key="id"
bindtap="onHotSearchTap"
data-keyword="{{item.keyword}}"
>
<text class="rank {{index < 3 ? 'top' : ''}}">{{index + 1}}</text>
<view class="hot-info">
<text class="hot-keyword">{{item.keyword}}</text>
<text class="hot-count">{{item.searchCount}}次搜索</text>
</view>
</view>
</view>
</view>
</view>
</view>
/**
* @联系作者:16768118056
* @描述:(此项目非免费分享)源码百分百可用,搞定毕设不发愁,支持远程调试安装、二次开发、定制、讲解、文档类。
* @访问:https://www.notmaker.com/detail/8326a897119d4542896c863a69d7bbe7/gtt20260415,查看完整运行演示。
*/

View File

@@ -0,0 +1,227 @@
/* pages/search/search.wxss */
/* 搜索页面样式 */
.search-container {
min-height: 100vh;
background-color: #f5f5f5;
}
.search-bar {
display: flex;
align-items: center;
padding: 20rpx 30rpx;
background-color: #fff;
}
.search-input-wrapper {
flex: 1;
display: flex;
align-items: center;
height: 72rpx;
padding: 0 24rpx;
background-color: #f5f5f5;
border-radius: 36rpx;
}
.search-icon {
font-size: 32rpx;
color: #999;
margin-right: 12rpx;
}
.search-input {
flex: 1;
height: 72rpx;
font-size: 28rpx;
color: #333;
}
.cancel-btn {
margin-left: 24rpx;
font-size: 28rpx;
color: #667eea;
}
.search-result {
padding: 20rpx 30rpx;
}
.result-item {
display: flex;
background-color: #fff;
border-radius: 16rpx;
padding: 24rpx;
margin-bottom: 20rpx;
}
.result-cover {
width: 160rpx;
height: 120rpx;
border-radius: 12rpx;
margin-right: 20rpx;
flex-shrink: 0;
}
.result-info {
flex: 1;
display: flex;
flex-direction: column;
min-width: 0;
}
.result-title {
font-size: 30rpx;
font-weight: bold;
color: #333;
margin-bottom: 12rpx;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.result-summary {
font-size: 24rpx;
color: #999;
margin-bottom: 12rpx;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
flex: 1;
}
.result-meta {
display: flex;
gap: 20rpx;
}
.result-meta text {
font-size: 22rpx;
color: #999;
}
.loading-more,
.no-more {
text-align: center;
padding: 30rpx;
font-size: 24rpx;
color: #999;
}
.empty-state {
display: flex;
flex-direction: column;
align-items: center;
padding: 100rpx 0;
}
.empty-icon {
font-size: 100rpx;
color: #ddd;
margin-bottom: 20rpx;
}
.empty-text {
font-size: 28rpx;
color: #999;
}
.search-content {
padding: 30rpx;
}
.history-section {
margin-bottom: 50rpx;
}
.section-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 24rpx;
}
.section-title {
font-size: 30rpx;
font-weight: bold;
color: #333;
}
.section-header .iconfont {
font-size: 36rpx;
color: #999;
}
.history-list {
display: flex;
flex-wrap: wrap;
gap: 20rpx;
}
.history-item {
padding: 12rpx 28rpx;
font-size: 26rpx;
color: #666;
background-color: #fff;
border-radius: 28rpx;
}
.hot-section .section-header .hot-icon {
font-size: 32rpx;
color: #F56C6C;
}
.hot-list {
background-color: #fff;
border-radius: 16rpx;
overflow: hidden;
}
.hot-item {
display: flex;
align-items: center;
padding: 24rpx;
border-bottom: 1rpx solid #f5f5f5;
}
.hot-item:last-child {
border-bottom: none;
}
.hot-item .rank {
width: 40rpx;
font-size: 28rpx;
font-weight: bold;
color: #999;
margin-right: 20rpx;
}
.hot-item .rank.top {
color: #F56C6C;
}
.hot-info {
flex: 1;
display: flex;
flex-direction: column;
}
.hot-keyword {
font-size: 28rpx;
color: #333;
margin-bottom: 8rpx;
}
.hot-count {
font-size: 22rpx;
color: #999;
}
/**
* @联系作者:16768118056
* @描述:(此项目非免费分享)源码百分百可用,搞定毕设不发愁,支持远程调试安装、二次开发、定制、讲解、文档类。
* @访问:https://www.notmaker.com/detail/8326a897119d4542896c863a69d7bbe7/gtt20260415,查看完整运行演示。
*/