ai-c/pages/notices/notices.js
2026-02-02 18:21:32 +08:00

78 lines
2.0 KiB
JavaScript

const api = require('../../utils/api')
Page({
data: {
notices: [],
loading: true,
error: null
},
onLoad() {
this.loadNotices()
},
onBack() {
wx.navigateBack()
},
async loadNotices() {
this.setData({ loading: true, error: null })
try {
const res = await api.common.getNotices()
console.log('[notices] 公告列表响应:', res)
if (res.success && res.data) {
const notices = res.data.map(item => ({
id: item.id,
content: item.content,
linkType: item.linkType || 'none',
linkValue: item.linkValue || '',
sortOrder: item.sortOrder || 0,
createdAt: this.formatDate(item.createdAt)
}))
this.setData({ notices })
} else {
this.setData({ error: res.error || '加载失败' })
}
} catch (err) {
console.error('[notices] 加载公告失败:', err)
this.setData({ error: err.message || '加载失败' })
} finally {
this.setData({ loading: false })
}
},
onNoticeTap(e) {
const notice = e.currentTarget.dataset.notice
console.log('[notices] 点击公告:', notice)
if (notice.linkType === 'web' && notice.linkValue) {
wx.navigateTo({
url: `/pages/webview/webview?url=${encodeURIComponent(notice.linkValue)}`
})
} else if (notice.linkType === 'miniprogram' && notice.linkValue) {
wx.navigateToMiniProgram({
appId: notice.linkValue
})
} else if (notice.linkType === 'article' && notice.linkValue) {
wx.navigateTo({
url: `/pages/academy/detail/detail?id=${notice.linkValue}`
})
} else {
wx.navigateTo({
url: `/pages/notices/detail/detail?id=${notice.id}`
})
}
},
formatDate(dateStr) {
if (!dateStr) return ''
const date = new Date(dateStr)
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
}
})