You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
320 lines
9.0 KiB
320 lines
9.0 KiB
const app = getApp<IAppOption>() |
|
|
|
Page({ |
|
data: { |
|
statusBarHeight: 44, |
|
activeTab: 0, |
|
// 首页聚合数据 |
|
searchPlaceholder: '请搜索你想要的内容', |
|
notifications: [] as any[], |
|
quickEntries: [] as any[], |
|
hotActivities: [] as any[], |
|
recommendAgents: [] as any[], |
|
bannerItems: [] as any[], |
|
currentBannerIndex: 0, |
|
currentBannerImage: '', |
|
// 模块排序 |
|
sortedModuleTypes: [] as string[], |
|
// 推荐活动分页数据 |
|
latestActivities: [] as any[], |
|
activityPagination: { |
|
page: 1, |
|
pages: 1, |
|
count: 0, |
|
}, |
|
activityLoading: false, |
|
// 智能体数据 |
|
agentList: [] as any[], |
|
}, |
|
|
|
bannerTimer: null as number | null, |
|
|
|
onLoad() { |
|
const sysInfo = wx.getSystemInfoSync() |
|
this.setData({ statusBarHeight: sysInfo.statusBarHeight || 44 }) |
|
app.waitLogin().then(() => { |
|
this.fetchHomeData() |
|
this.fetchRecommendedActivities(1) |
|
// 智能体推荐列表暂不联调 |
|
// this.fetchAgentList() |
|
}) |
|
}, |
|
|
|
onUnload() { |
|
if (this.bannerTimer) { |
|
clearInterval(this.bannerTimer) |
|
this.bannerTimer = null |
|
} |
|
}, |
|
|
|
onReachBottom() { |
|
// 触底加载更多推荐活动 |
|
if (this.data.activeTab === 0) { |
|
this.loadMoreActivities() |
|
} |
|
}, |
|
|
|
fetchHomeData() { |
|
wx.ajax({ |
|
url: '/home/index', |
|
method: 'GET', |
|
}) |
|
.then((res: any) => { |
|
const data: any = {} |
|
|
|
// 搜索模块 |
|
if (res.searchModule?.config?.placeholder) { |
|
data.searchPlaceholder = res.searchModule.config.placeholder |
|
} |
|
|
|
// 收集有数据的模块及其 sort 值,用于动态排序 |
|
const moduleSorts: { type: string; sort: number }[] = [] |
|
|
|
// Banner 模块 |
|
if (res.bannerModule?.items?.length) { |
|
data.bannerItems = res.bannerModule.items |
|
data.currentBannerImage = res.bannerModule.items[0].imageUrl |
|
moduleSorts.push({ type: 'banner', sort: res.bannerModule.sort ?? 99 }) |
|
this.startBannerRotation(res.bannerModule.items) |
|
} |
|
|
|
// 快捷入口模块 |
|
if (res.quickEntryModule?.items?.length) { |
|
data.quickEntries = res.quickEntryModule.items |
|
moduleSorts.push({ type: 'quickEntry', sort: res.quickEntryModule.sort ?? 99 }) |
|
} |
|
|
|
// 通知栏模块 |
|
if (res.notificationModule?.data?.length) { |
|
data.notifications = res.notificationModule.data |
|
moduleSorts.push({ type: 'notification', sort: res.notificationModule.sort ?? 99 }) |
|
} |
|
|
|
// 热门活动模块 - 处理mainImages JSON字符串解析 |
|
if (res.hotActivityModule?.data?.length) { |
|
const hotActivities = res.hotActivityModule.data.map((item: any) => { |
|
let mainImages = item.mainImages |
|
// 如果mainImages是字符串,解析为数组 |
|
if (typeof mainImages === 'string') { |
|
try { |
|
mainImages = JSON.parse(mainImages) |
|
} catch (e) { |
|
console.error('解析mainImages失败', e) |
|
mainImages = [] |
|
} |
|
} |
|
return { ...item, mainImages } |
|
}) |
|
data.hotActivities = hotActivities |
|
moduleSorts.push({ type: 'hotActivity', sort: res.hotActivityModule.sort ?? 99 }) |
|
} |
|
|
|
// 推荐智能体模块 |
|
if (res.recommendAgentModule?.data?.length) { |
|
data.recommendAgents = res.recommendAgentModule.data |
|
moduleSorts.push({ type: 'recommendAgent', sort: res.recommendAgentModule.sort ?? 99 }) |
|
} |
|
|
|
// 按 sort 升序排列 |
|
moduleSorts.sort((a, b) => a.sort - b.sort) |
|
data.sortedModuleTypes = moduleSorts.map((m) => m.type) |
|
|
|
this.setData(data) |
|
}) |
|
.catch((err) => { |
|
console.error('获取首页数据失败', err) |
|
}) |
|
}, |
|
|
|
fetchRecommendedActivities(page: number) { |
|
if (this.data.activityLoading) return |
|
|
|
this.setData({ activityLoading: true }) |
|
|
|
wx.ajax({ |
|
url: '/activity/list', |
|
method: 'GET', |
|
data: { getRecommended: 1, page, pageSize: 10 }, |
|
}) |
|
.then((res: any) => { |
|
const list = res.list || [] |
|
const pagination = res.pagination || {} |
|
|
|
// 处理mainImages JSON字符串解析 |
|
const activities = list.map((item: any) => { |
|
let mainImages = item.mainImages |
|
if (typeof mainImages === 'string') { |
|
try { |
|
mainImages = JSON.parse(mainImages) |
|
} catch (e) { |
|
console.error('解析mainImages失败', e) |
|
mainImages = [] |
|
} |
|
} |
|
return { ...item, mainImages } |
|
}) |
|
|
|
// 如果是第一页,直接替换;否则追加 |
|
const latestActivities = page === 1 ? activities : [...this.data.latestActivities, ...activities] |
|
|
|
this.setData({ |
|
latestActivities, |
|
activityPagination: { |
|
page: pagination.page || page, |
|
pages: pagination.totalPages || 1, |
|
count: pagination.total || 0, |
|
}, |
|
activityLoading: false, |
|
}) |
|
}) |
|
.catch((err) => { |
|
console.error('获取推荐活动失败', err) |
|
this.setData({ activityLoading: false }) |
|
}) |
|
}, |
|
|
|
loadMoreActivities() { |
|
const { activityPagination, activityLoading } = this.data |
|
if (activityLoading || activityPagination.page >= activityPagination.pages) return |
|
|
|
this.fetchRecommendedActivities(activityPagination.page + 1) |
|
}, |
|
|
|
fetchAgentList() { |
|
wx.ajax({ |
|
url: '/agent/list', |
|
method: 'GET', |
|
data: { page: 1, pageSize: 10 }, |
|
}) |
|
.then((res: any) => { |
|
if (res.list?.length) { |
|
this.setData({ agentList: res.list }) |
|
} |
|
}) |
|
.catch((err) => { |
|
console.error('获取智能体列表失败', err) |
|
}) |
|
}, |
|
|
|
startBannerRotation(bannerItems: any[]) { |
|
if (bannerItems.length <= 1) return |
|
|
|
this.bannerTimer = setInterval(() => { |
|
const currentIndex = this.data.currentBannerIndex |
|
const nextIndex = (currentIndex + 1) % bannerItems.length |
|
this.setData({ |
|
currentBannerIndex: nextIndex, |
|
currentBannerImage: bannerItems[nextIndex].imageUrl, |
|
}) |
|
}, 6000) |
|
}, |
|
|
|
onSearchTap() { |
|
wx.showToast({ title: '搜索功能开发中', icon: 'none' }) |
|
}, |
|
|
|
onNavTap(e: WechatMiniprogram.TouchEvent) { |
|
const type = e.currentTarget.dataset.type |
|
wx.showToast({ title: `${type}功能开发中`, icon: 'none' }) |
|
}, |
|
|
|
onAnnounceTap() { |
|
wx.showToast({ title: '公告详情开发中', icon: 'none' }) |
|
}, |
|
|
|
onMoreAgentsTap() { |
|
wx.showToast({ title: '更多智能体开发中', icon: 'none' }) |
|
}, |
|
handleKkd(e) { |
|
const { index } = e.currentTarget.dataset |
|
const { quickEntries } = this.data |
|
const { jumpType, jumpTarget } = quickEntries[index] |
|
this.handleJump(jumpType, jumpTarget) |
|
}, |
|
|
|
handleBannerTap(e) { |
|
const { index } = e.currentTarget.dataset |
|
const { bannerItems } = this.data |
|
const { jumpType, jumpTarget } = bannerItems[index] || {} |
|
this.handleJump(jumpType, jumpTarget) |
|
}, |
|
|
|
handleNotificationTap(e) { |
|
const { index } = e.currentTarget.dataset |
|
const { notifications } = this.data |
|
const idx = index ?? 0 |
|
const { jumpType, jumpTarget } = notifications[idx] || {} |
|
if (!jumpType || jumpType === 0) return |
|
this.handleJump(jumpType, jumpTarget) |
|
}, |
|
|
|
handleJump(jumpType: number | string, jumpTarget: string) { |
|
const jt = Number(jumpType) |
|
if (!jt || !jumpTarget) return |
|
switch (jt) { |
|
case 2: |
|
wx.navigateTo({ url: `/pages/actDetail/index?id=${jumpTarget}` }) |
|
break |
|
case 3: { |
|
const tabPages = [ |
|
'/pages/index/index', |
|
'/pages/act/index', |
|
'/pages/notice/index', |
|
'/pages/agent/index', |
|
'/pages/my/index', |
|
] |
|
const isTab = tabPages.some((p) => jumpTarget.startsWith(p)) |
|
if (isTab) { |
|
wx.switchTab({ url: jumpTarget }) |
|
} else { |
|
wx.navigateTo({ url: jumpTarget }) |
|
} |
|
break |
|
} |
|
case 4: |
|
wx.navigateTo({ url: `/pages/webview/index?url=${encodeURIComponent(jumpTarget)}` }) |
|
break |
|
case 5: |
|
wx.navigateToMiniProgram({ appId: jumpTarget }) |
|
break |
|
default: |
|
break |
|
} |
|
}, |
|
|
|
onAgentTap(e: WechatMiniprogram.TouchEvent) { |
|
const id = e.currentTarget.dataset.id |
|
wx.navigateTo({ url: `/pages/agent/index?id=${id}` }) |
|
}, |
|
|
|
onActivityTap(e: WechatMiniprogram.TouchEvent) { |
|
const id = e.currentTarget.dataset.id |
|
wx.navigateTo({ url: `/pages/activity-detail/index?id=${id}` }) |
|
}, |
|
|
|
onTabTap(e: WechatMiniprogram.TouchEvent) { |
|
const index = e.currentTarget.dataset.index |
|
if (index === this.data.activeTab) return |
|
this.setData({ activeTab: index }) |
|
if (index === 4) { |
|
wx.switchTab({ url: '/pages/my/index' }) |
|
} |
|
}, |
|
handleSearchTap() { |
|
wx.navigateTo({ |
|
url: '/pages/search/index', |
|
}) |
|
}, |
|
handleActMore() { |
|
wx.switchTab({ |
|
url: '/pages/act/index', |
|
}) |
|
}, |
|
handleAgentMore() { |
|
wx.switchTab({ |
|
url: '/pages/agent/index', |
|
}) |
|
}, |
|
}) |
|
|
|
export {}
|
|
|