/** * Soul创业派对 - 首页 * 开发: 卡若 * 技术支持: 存客宝 */ console.log('[Index] ===== 首页文件开始加载 =====') const app = getApp() Page({ data: { // 系统信息 statusBarHeight: 44, navBarHeight: 88, // 用户信息 isLoggedIn: false, hasFullBook: false, readCount: 0, // 书籍数据 totalSections: 62, bookData: [], // 推荐章节 featuredSections: [ { id: '1.1', title: '荷包:电动车出租的被动收入模式', tag: '免费', tagClass: 'tag-free', part: '真实的人' }, { id: '3.1', title: '3000万流水如何跑出来', tag: '热门', tagClass: 'tag-pink', part: '真实的行业' }, { id: '8.1', title: '流量杠杆:抖音、Soul、飞书', tag: '推荐', tagClass: 'tag-purple', part: '真实的赚钱' } ], // 最新章节(动态计算) latestSection: null, latestLabel: '最新更新', // 内容概览 partsList: [ { id: 'part-1', number: '一', title: '真实的人', subtitle: '人与人之间的底层逻辑' }, { id: 'part-2', number: '二', title: '真实的行业', subtitle: '电商、内容、传统行业解析' }, { id: 'part-3', number: '三', title: '真实的错误', subtitle: '我和别人犯过的错' }, { id: 'part-4', number: '四', title: '真实的赚钱', subtitle: '底层结构与真实案例' }, { id: 'part-5', number: '五', title: '真实的社会', subtitle: '未来职业与商业生态' } ], // 超级个体(VIP会员) superMembers: [], superMembersLoading: true, // 最新新增章节 latestChapters: [], // 篇章数(从 bookData 计算) partCount: 0, // 加载状态 loading: true }, onLoad(options) { console.log('[Index] ===== onLoad 触发 =====') // 获取系统信息 this.setData({ statusBarHeight: app.globalData.statusBarHeight, navBarHeight: app.globalData.navBarHeight }) // 处理分享参数(推荐码绑定) if (options && options.ref) { console.log('[Index] 检测到推荐码:', options.ref) app.handleReferralCode({ query: options }) } wx.showShareMenu({ withShareTimeline: true }) this.initData() }, onShow() { console.log('[Index] onShow 触发') // 设置TabBar选中状态 if (typeof this.getTabBar === 'function' && this.getTabBar()) { const tabBar = this.getTabBar() console.log('[Index] TabBar 组件:', tabBar ? '已找到' : '未找到') // 主动触发配置加载 if (tabBar && tabBar.loadFeatureConfig) { console.log('[Index] 主动调用 TabBar.loadFeatureConfig()') tabBar.loadFeatureConfig() } // 更新选中状态 if (tabBar && tabBar.updateSelected) { tabBar.updateSelected() } else if (tabBar) { tabBar.setData({ selected: 0 }) } } else { console.log('[Index] TabBar 组件未找到或 getTabBar 方法不存在') } // 更新用户状态 this.updateUserStatus() }, // 初始化数据 async initData() { this.setData({ loading: true }) try { await this.loadBookData() await this.loadFeaturedFromServer() this.loadSuperMembers() this.loadLatestChapters() } catch (e) { console.error('初始化失败:', e) } finally { this.setData({ loading: false }) } }, async loadSuperMembers() { this.setData({ superMembersLoading: true }) try { // 优先加载 VIP 会员(购买 1980 fullbook/vip 订单的用户) let members = [] try { const res = await app.request({ url: '/api/miniprogram/vip/members', silent: true }) if (res && res.success && res.data) { // 不再过滤无头像用户,无头像时用首字母展示 members = (Array.isArray(res.data) ? res.data : []).slice(0, 4).map(u => ({ id: u.id, name: u.vipName || u.vip_name || u.nickname || '会员', avatar: u.vipAvatar || u.vip_avatar || u.avatar || '', isVip: true })) if (members.length > 0) { console.log('[Index] 超级个体加载成功:', members.length, '人') } } } catch (e) { console.log('[Index] vip/members 请求失败:', e) } // 不足 4 个则用有头像的普通用户补充 if (members.length < 4) { try { const dbRes = await app.request({ url: '/api/miniprogram/users?limit=20', silent: true }) if (dbRes && dbRes.success && dbRes.data) { const existIds = new Set(members.map(m => m.id)) const extra = (Array.isArray(dbRes.data) ? dbRes.data : []) .filter(u => u.avatar && u.nickname && !existIds.has(u.id)) .slice(0, 4 - members.length) .map(u => ({ id: u.id, name: u.nickname, avatar: u.avatar, isVip: u.is_vip === 1 })) members = members.concat(extra) } } catch (e) {} } this.setData({ superMembers: members, superMembersLoading: false }) } catch (e) { console.log('[Index] 加载超级个体失败:', e) this.setData({ superMembersLoading: false }) } }, // 从服务端获取精选推荐(加权算法:阅读量50% + 时效30% + 付款率20%)和最新更新 async loadFeaturedFromServer() { try { const res = await app.request({ url: '/api/miniprogram/book/all-chapters', silent: true }) const chapters = (res && res.data) ? res.data : (res && res.chapters) ? res.chapters : [] let featured = (res && res.featuredSections) ? res.featuredSections : [] // 服务端未返回精选时,从前端按更新时间取前3条有效章节作为回退 if (featured.length === 0 && chapters.length > 0) { const valid = chapters.filter(c => { const id = (c.id || '').toLowerCase() const pt = (c.part_title || c.partTitle || '').toLowerCase() return !id.includes('preface') && !id.includes('epilogue') && !id.includes('appendix') && !pt.includes('序言') && !pt.includes('尾声') && !pt.includes('附录') }) featured = valid .sort((a, b) => new Date(b.updated_at || b.updatedAt || 0) - new Date(a.updated_at || a.updatedAt || 0)) .slice(0, 5) } const tagMap = ['热门', '推荐', '精选'] const tagClassMap = ['tag-hot', 'tag-rec', 'tag-rec'] if (featured.length > 0) { this.setData({ featuredSections: featured.slice(0, 3).map((s, i) => ({ id: s.id || s.section_id, title: s.section_title || s.sectionTitle || s.title || s.chapterTitle || '', part: (s.cleanPartTitle || s.part_title || s.partTitle || '').replace(/[_||]/g, ' ').trim(), tag: tagMap[i] || '精选', tagClass: tagClassMap[i] || 'tag-rec' })) }) } // 最新更新 = 按 updated_at 排序第1篇(排除序言/尾声/附录) const validChapters = chapters.filter(c => { const id = (c.id || '').toLowerCase() const pt = (c.part_title || c.partTitle || '').toLowerCase() return !id.includes('preface') && !id.includes('epilogue') && !id.includes('appendix') && !pt.includes('序言') && !pt.includes('尾声') && !pt.includes('附录') }) if (validChapters.length > 0) { validChapters.sort((a, b) => new Date(b.updated_at || b.updatedAt || 0) - new Date(a.updated_at || a.updatedAt || 0)) const latest = validChapters[0] this.setData({ latestSection: { id: latest.id || latest.section_id, title: latest.section_title || latest.sectionTitle || latest.title || latest.chapterTitle || '', part: latest.cleanPartTitle || latest.part_title || latest.partTitle || '' } }) } } catch (e) { console.log('[Index] 从服务端加载推荐失败,使用默认:', e) } }, async loadBookData() { try { const res = await app.request({ url: '/api/miniprogram/book/all-chapters', silent: true }) if (res && (res.data || res.chapters)) { const chapters = res.data || res.chapters || [] const partIds = new Set(chapters.map(c => c.partId || c.part_id || '').filter(Boolean)) this.setData({ bookData: chapters, totalSections: res.total || chapters.length || 62, partCount: partIds.size || 5 }) } } catch (e) { console.error('加载书籍数据失败:', e) } }, // 更新用户状态(已读数 = 用户实际打开过的章节数,仅统计有权限阅读的) updateUserStatus() { const { isLoggedIn, hasFullBook, purchasedSections } = app.globalData const readCount = Math.min(app.getReadCount(), this.data.totalSections || 62) this.setData({ isLoggedIn, hasFullBook, readCount }) }, // 跳转到目录 goToChapters() { wx.switchTab({ url: '/pages/chapters/chapters' }) }, // 跳转到搜索页 goToSearch() { wx.navigateTo({ url: '/pages/search/search' }) }, // 跳转到阅读页 goToRead(e) { const id = e.currentTarget.dataset.id wx.navigateTo({ url: `/pages/read/read?id=${id}` }) }, // 跳转到匹配页 goToMatch() { wx.switchTab({ url: '/pages/match/match' }) }, goToVip() { wx.navigateTo({ url: '/pages/vip/vip' }) }, goToAbout() { wx.navigateTo({ url: '/pages/about/about' }) }, goToSuperList() { wx.switchTab({ url: '/pages/match/match' }) }, async loadLatestChapters() { try { const res = await app.request({ url: '/api/miniprogram/book/all-chapters', silent: true }) const chapters = (res && res.data) || (res && res.chapters) || [] const sortOrder = c => (c.sortOrder ?? c.sectionOrder ?? c.sort_order ?? 0) // 优先取 sort_order > 62 的「新增」章节;若无则取最近更新的前 10 章(排除序言/尾声/附录) let candidates = chapters.filter(c => sortOrder(c) > 62) if (candidates.length === 0) { const id = (c) => (c.id || '').toLowerCase() const pt = (c) => (c.partTitle || c.part_title || '').toLowerCase() candidates = chapters.filter(c => !id(c).includes('preface') && !id(c).includes('epilogue') && !id(c).includes('appendix') && !pt(c).includes('序言') && !pt(c).includes('尾声') && !pt(c).includes('附录') ) } const latest = candidates .sort((a, b) => new Date(b.updatedAt || b.updated_at || 0) - new Date(a.updatedAt || a.updated_at || 0)) .slice(0, 10) .map(c => { const d = new Date(c.updatedAt || c.updated_at || Date.now()) const title = c.section_title || c.sectionTitle || c.title || c.chapterTitle || '' const rawContent = (c.content || '').replace(/<[^>]+>/g, '').trim() // 描述仅用正文摘要,避免 #id 或标题重复;截取 36 字 let desc = '' if (rawContent && rawContent.length > 0) { const clean = rawContent.replace(/^#[\d.]+\s*/, '').trim() desc = clean.length > 36 ? clean.slice(0, 36) + '...' : clean } return { id: c.id, title, desc, price: c.price ?? 1, dateStr: `${d.getMonth() + 1}/${d.getDate()}` } }) this.setData({ latestChapters: latest }) } catch (e) { console.log('[Index] 加载最新新增失败:', e) } }, goToMemberDetail(e) { const id = e.currentTarget.dataset.id wx.navigateTo({ url: `/pages/member-detail/member-detail?id=${id}` }) }, // 跳转到我的页面 goToMy() { wx.switchTab({ url: '/pages/my/my' }) }, // 下拉刷新 async onPullDownRefresh() { await this.initData() this.updateUserStatus() wx.stopPullDownRefresh() }, onShareAppMessage() { const ref = app.getMyReferralCode() return { title: 'Soul创业派对 - 真实商业故事', path: ref ? `/pages/index/index?ref=${ref}` : '/pages/index/index' } }, onShareTimeline() { const ref = app.getMyReferralCode() return { title: 'Soul创业派对 - 真实商业故事', query: ref ? `ref=${ref}` : '' } } })