From c4938f6f4e839890b032c75c7a57333a6a9157a9 Mon Sep 17 00:00:00 2001
From: peng <peng.com>
Date: 星期四, 06 十一月 2025 17:06:10 +0800
Subject: [PATCH] 添加新闻功能
---
wx/pages/index/index.js | 199 +++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 190 insertions(+), 9 deletions(-)
diff --git a/wx/pages/index/index.js b/wx/pages/index/index.js
index cf405ea..0769b68 100644
--- a/wx/pages/index/index.js
+++ b/wx/pages/index/index.js
@@ -8,6 +8,8 @@
banners: [],
// 璧涗簨鍒楄〃
activities: [],
+ // 鏈�鏂版柊闂诲垪琛�
+ latestNews: [],
// 鍔犺浇鐘舵��
loading: true,
// 鏄惁杩樻湁鏇村鏁版嵁
@@ -21,19 +23,20 @@
// 绛涢�夋潯浠�
filterStatus: 'all', // all, upcoming, ongoing, ended
// 杞挱鍥惧綋鍓嶇储寮�
- currentBannerIndex: 0
+ currentBannerIndex: 0,
+ // 鍒嗕韩鐩稿叧鏁版嵁
+ shareActivityId: null,
+ shareActivityName: null
},
onLoad(options) {
console.log('棣栭〉鍔犺浇')
- this.loadBanners()
- this.loadActivities()
},
onShow() {
console.log('棣栭〉鏄剧ず')
// 缁熶竴绯荤粺瀵艰埅鏍忔爣棰�
- try { wx.setNavigationBarTitle({ title: '钃夋槗鍒�' }) } catch (e) {}
+ try { wx.setNavigationBarTitle({ title: '钃塭鍒�' }) } catch (e) {}
// 妫�鏌ョ櫥褰曠姸鎬�
if (!app.globalData.token) {
app.login()
@@ -42,6 +45,10 @@
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().init();
}
+ // 鍔犺浇鏁版嵁
+ this.loadBanners()
+ this.loadLatestNews()
+ this.loadActivities()
},
onPullDownRefresh() {
@@ -66,6 +73,7 @@
Promise.all([
this.loadBanners(),
+ this.loadLatestNews(),
this.loadActivities()
]).finally(() => {
wx.stopPullDownRefresh()
@@ -143,7 +151,7 @@
type: 'image',
url: banner.coverImage.fullUrl,
thumbUrl: banner.coverImage.fullThumbUrl
- })
+ });
}
return {
@@ -161,6 +169,47 @@
})
},
+ // 鍔犺浇鏈�鏂版柊闂伙紙鍙姞杞藉墠3鏉★級
+ loadLatestNews() {
+ return app.graphqlRequest(`
+ query getPublishedNewsList($page: Int!, $size: Int!) {
+ publishedNewsList(page: $page, size: $size) {
+ content {
+ id
+ title
+ summary
+ coverImage
+ author
+ viewCount
+ createTime
+ }
+ totalElements
+ page
+ size
+ }
+ }
+ `, {
+ page: 1,
+ size: 3
+ }).then(data => {
+ if (data.publishedNewsList) {
+ // 鏍煎紡鍖栨椂闂存樉绀�
+ const latestNews = data.publishedNewsList.content.map(news => {
+ if (news.createTime) {
+ news.createTime = utils.formatDate(news.createTime, 'YYYY-MM-DD HH:mm:ss');
+ }
+ return news;
+ });
+
+ this.setData({
+ latestNews: latestNews
+ })
+ }
+ }).catch(err => {
+ console.error('鍔犺浇鏈�鏂版柊闂诲け璐�:', err)
+ })
+ },
+
// 鍔犺浇璧涗簨鍒楄〃
loadActivities(isLoadMore = false) {
this.setData({ loading: true })
@@ -175,9 +224,9 @@
if (filterStatus !== 'all') {
// 鏍规嵁filterStatus鏄犲皠鍒板搴旂殑state鍊�
const stateMapping = {
- 'upcoming': 1, // 鍗冲皢寮�濮�
- 'ongoing': 2, // 杩涜涓�
- 'ended': 3 // 宸茬粨鏉�
+ 'upcoming': 1, // 鍗冲皢寮�濮� -> 鍙戝竷鐘舵��
+ 'ongoing': 1, // 杩涜涓� -> 鍙戝竷鐘舵��
+ 'ended': 2 // 宸茬粨鏉� -> 鍏抽棴鐘舵��
}
stateFilter = stateMapping[filterStatus]
}
@@ -214,7 +263,7 @@
page: currentPage,
size: pageSize,
name: nameFilter,
- state: stateFilter
+ state: 1
}).then(data => {
if (data.activities) {
let newActivities = data.activities.content
@@ -337,6 +386,22 @@
utils.navigateTo('/pages/activity/detail', { id: activityId })
},
+ // 璺宠浆鍒版柊闂昏鎯�
+ goToNewsDetail(e) {
+ const newsId = e.currentTarget.dataset.id
+ if (newsId) {
+ wx.navigateTo({
+ url: `/pages/news/detail?id=${newsId}`
+ })
+ }
+ },
+
+ // 璺宠浆鍒版柊闂诲垪琛�
+ goToNewsList() {
+ wx.navigateTo({
+ url: '/pages/news/list'
+ })
+ },
// 鏍煎紡鍖栨棩鏈�
formatDate(date) {
@@ -434,5 +499,121 @@
return now <= signupDeadline &&
activity.state === 'SIGNUP' &&
activity.playerCount < activity.playerMax
+ },
+
+ // 鍒嗕韩鍗曚釜姣旇禌
+ onShareActivity(e) {
+ const { id, name } = e.currentTarget.dataset
+
+ // 鏄剧ず鍒嗕韩閫夐」
+ wx.showActionSheet({
+ itemList: ['鍒嗕韩缁欐湅鍙�', '鐢熸垚鍒嗕韩娴锋姤'],
+ success: (res) => {
+ if (res.tapIndex === 0) {
+ // 鍒嗕韩缁欐湅鍙�
+ this.shareToFriend(id, name)
+ } else if (res.tapIndex === 1) {
+ // 鐢熸垚鍒嗕韩娴锋姤
+ this.generateSharePoster(id, name)
+ }
+ },
+ fail: (res) => {
+ console.log('鐢ㄦ埛鍙栨秷鍒嗕韩')
+ }
+ })
+ },
+
+ // 鍒嗕韩缁欐湅鍙�
+ shareToFriend(activityId, activityName) {
+ wx.showShareMenu({
+ withShareTicket: true,
+ menus: ['shareAppMessage', 'shareTimeline']
+ })
+
+ // 璁剧疆褰撳墠瑕佸垎浜殑娲诲姩淇℃伅
+ this.setData({
+ shareActivityId: activityId,
+ shareActivityName: activityName
+ })
+
+ // 瑙﹀彂鍒嗕韩
+ wx.updateShareMenu({
+ withShareTicket: true,
+ isUpdatableMessage: true,
+ activityId: 'share_activity_' + activityId,
+ templateInfo: {
+ parameterList: [{
+ name: 'activity_name',
+ value: activityName
+ }]
+ }
+ })
+
+ wx.showToast({
+ title: '璇风偣鍑诲彸涓婅鍒嗕韩',
+ icon: 'none',
+ duration: 2000
+ })
+ },
+
+ // 鐢熸垚鍒嗕韩娴锋姤
+ generateSharePoster(activityId, activityName) {
+ wx.showLoading({
+ title: '鐢熸垚娴锋姤涓�...'
+ })
+
+ // 杩欓噷鍙互璋冪敤鍚庣API鐢熸垚鍒嗕韩娴锋姤
+ // 鎴栬�呬娇鐢╟anvas鍦ㄥ墠绔敓鎴�
+ setTimeout(() => {
+ wx.hideLoading()
+ wx.showToast({
+ title: '娴锋姤鐢熸垚鍔熻兘寮�鍙戜腑',
+ icon: 'none',
+ duration: 2000
+ })
+ }, 1500)
+ },
+
+ // 椤甸潰鍒嗕韩鍔熻兘 - 鍒嗕韩缁欐湅鍙�
+ onShareAppMessage(res) {
+ console.log('鍒嗕韩缁欐湅鍙�', res)
+
+ // 濡傛灉鏄粠姣旇禌鍗$墖鍒嗕韩
+ if (this.data.shareActivityId && this.data.shareActivityName) {
+ const shareData = {
+ title: `${this.data.shareActivityName} - 钃塭鍒涙瘮璧涘钩鍙癭,
+ path: `/pages/activity/detail?id=${this.data.shareActivityId}`,
+ imageUrl: '', // 鍙互璁剧疆鍒嗕韩鍥剧墖
+ success: (res) => {
+ console.log('鍒嗕韩鎴愬姛', res)
+ },
+ fail: (res) => {
+ console.log('鍒嗕韩澶辫触', res)
+ }
+ }
+ return shareData
+ }
+
+ // 榛樿鍒嗕韩
+ return {
+ title: '钃塭鍒涙瘮璧涘钩鍙�',
+ path: '/pages/index/index',
+ imageUrl: '',
+ success: (res) => {
+ console.log('鍒嗕韩鎴愬姛', res)
+ },
+ fail: (res) => {
+ console.log('鍒嗕韩澶辫触', res)
+ }
+ }
+ },
+
+ // 椤甸潰鍒嗕韩鍔熻兘 - 鍒嗕韩鍒版湅鍙嬪湀
+ onShareTimeline() {
+ return {
+ title: '钃塭鍒涙瘮璧涘钩鍙�',
+ query: '',
+ imageUrl: ''
+ }
}
})
\ No newline at end of file
--
Gitblit v1.8.0