Files
soul-yongping/miniprogram/pages/search/search.js
卡若 76965adb23 chore: 清理敏感与开发文档,仅同步代码
- 永久忽略并从仓库移除 开发文档/
- 移除并忽略 .env 与小程序私有配置
- 同步小程序/管理端/API与脚本改动

Made-with: Cursor
2026-03-17 17:50:12 +08:00

159 lines
4.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Soul创业派对 - 章节搜索页
* 搜索章节标题和内容
*/
const app = getApp()
const { trackClick } = require('../../utils/trackClick')
Page({
data: {
statusBarHeight: 44,
keyword: '',
results: [],
loading: false,
searched: false,
total: 0,
// 热门搜索关键词(运行时根据热门章节/目录动态生成)
hotKeywords: [],
// 热门章节推荐
hotChapters: [
{ id: '1.1', title: '荷包:电动车出租的被动收入模式', tag: '免费', part: '真实的人' },
{ id: '9.12', title: '美业整合:一个人的公司如何月入十万', tag: '热门', part: '真实的赚钱' },
{ id: '3.1', title: '3000万流水如何跑出来', tag: '热门', part: '真实的行业' },
{ id: '8.1', title: '流量杠杆:抖音、Soul、飞书', tag: '推荐', part: '真实的赚钱' },
{ id: '9.13', title: 'AI工具推广一个隐藏的高利润赛道', tag: '最新', part: '真实的赚钱' }
]
},
onLoad() {
wx.showShareMenu({ withShareTimeline: true })
this.setData({
statusBarHeight: app.globalData.statusBarHeight || 44
})
// 加载热门章节
this.loadHotChapters()
},
// 加载热门章节(从服务器获取点击量高的章节)
async loadHotChapters() {
try {
const res = await app.request('/api/miniprogram/book/hot?limit=50')
const list = (res && res.data) || (res && res.chapters) || []
if (list.length > 0) {
const hotChapters = list.map((c, i) => ({
id: c.id,
mid: c.mid ?? c.MID ?? 0,
title: c.section_title || c.sectionTitle || c.title || c.chapterTitle || '',
part: c.part_title || c.partTitle || c.part || '',
tag: ['免费', '热门', '推荐', '最新'][i % 4] || '热门'
}))
this.setData({
hotChapters,
hotKeywords: this.buildHotKeywords(hotChapters)
})
} else {
this.setData({ hotKeywords: this.buildHotKeywords(app.globalData.bookData || []) })
}
} catch (e) {
this.setData({ hotKeywords: this.buildHotKeywords(app.globalData.bookData || []) })
}
},
buildHotKeywords(sourceList) {
const words = []
const pushWord = (word) => {
const w = (word || '').trim()
if (!w || w.length < 2 || words.includes(w)) return
words.push(w)
}
;(sourceList || []).forEach((item) => {
const title = String(item.title || '').replace(/[|:,.,。!?]/g, ' ')
const part = String(item.part || '').replace(/[|:,.,。!?]/g, ' ')
title.split(/\s+/).forEach(pushWord)
part.split(/\s+/).forEach(pushWord)
})
return words.slice(0, 8)
},
// 输入关键词
onInput(e) {
this.setData({ keyword: e.detail.value })
},
// 清空搜索
clearSearch() {
this.setData({
keyword: '',
results: [],
searched: false,
total: 0
})
},
// 点击热门关键词
onHotKeyword(e) {
const keyword = e.currentTarget.dataset.keyword
this.setData({ keyword })
this.doSearch()
},
// 执行搜索
async doSearch() {
const { keyword } = this.data
if (!keyword || keyword.trim().length < 1) {
wx.showToast({ title: '请输入搜索关键词', icon: 'none' })
return
}
trackClick('search', 'btn_click', keyword.trim())
this.setData({ loading: true, searched: true })
try {
const res = await app.request(`/api/miniprogram/book/search?q=${encodeURIComponent(keyword.trim())}`)
if (res && res.success) {
this.setData({
results: res.results || [],
total: res.total || 0
})
} else {
this.setData({ results: [], total: 0 })
}
} catch (e) {
wx.showToast({ title: '搜索失败', icon: 'none' })
this.setData({ results: [], total: 0 })
} finally {
this.setData({ loading: false })
}
},
// 跳转阅读(优先传 mid与分享逻辑一致
goToRead(e) {
const id = e.currentTarget.dataset.id
const mid = e.currentTarget.dataset.mid || app.getSectionMid(id)
trackClick('search', 'card_click', id)
const q = mid ? `mid=${mid}` : `id=${id}`
wx.navigateTo({ url: `/pages/read/read?${q}` })
},
// 返回上一页
goBack() {
getApp().goBackOrToHome()
},
onShareAppMessage() {
const ref = app.getMyReferralCode()
return {
title: 'Soul创业派对 - 搜索',
path: ref ? `/pages/search/search?ref=${ref}` : '/pages/search/search'
}
},
onShareTimeline() {
const ref = app.getMyReferralCode()
return { title: 'Soul创业派对 - 搜索', query: ref ? `ref=${ref}` : '' }
}
})