77 lines
2.2 KiB
JavaScript
77 lines
2.2 KiB
JavaScript
const api = require('../../utils/api')
|
|
|
|
const formatDate = (value) => {
|
|
if (!value) return ''
|
|
const date = new Date(value)
|
|
if (Number.isNaN(date.getTime())) return ''
|
|
const month = String(date.getMonth() + 1).padStart(2, '0')
|
|
const day = String(date.getDate()).padStart(2, '0')
|
|
return `${date.getFullYear()}-${month}-${day}`
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
categories: [{ id: '', name: '推荐' }],
|
|
activeCategoryId: '',
|
|
articles: [],
|
|
loading: true,
|
|
loadError: false,
|
|
},
|
|
|
|
onLoad() {
|
|
this.loadPage()
|
|
},
|
|
|
|
async loadPage() {
|
|
this.setData({ loading: true, loadError: false })
|
|
try {
|
|
const [categories, articles] = await Promise.all([
|
|
api.get('/content/categories'),
|
|
api.get('/content/articles'),
|
|
])
|
|
this.setData({
|
|
categories: [{ id: '', name: '推荐' }, ...(categories || [])],
|
|
articles: this.normalizeArticles(articles || [], categories || []),
|
|
loading: false,
|
|
})
|
|
} catch (error) {
|
|
console.error('加载婚恋课堂失败', error)
|
|
this.setData({ loading: false, loadError: true })
|
|
}
|
|
},
|
|
|
|
normalizeArticles(articles, categories) {
|
|
const categoryNames = new Map(categories.map((item) => [item.id, item.name]))
|
|
return articles.map((item) => ({
|
|
...item,
|
|
categoryName: categoryNames.get(item.categoryId) || '婚恋课堂',
|
|
publishAtText: formatDate(item.publishAt || item.createdAt),
|
|
}))
|
|
},
|
|
|
|
async select(e) {
|
|
const categoryId = e.currentTarget.dataset.id || ''
|
|
if (categoryId === this.data.activeCategoryId) return
|
|
this.setData({ activeCategoryId: categoryId, loading: true, loadError: false })
|
|
try {
|
|
const articles = await api.get('/content/articles', categoryId ? { categoryId } : undefined)
|
|
this.setData({
|
|
articles: this.normalizeArticles(articles || [], this.data.categories),
|
|
loading: false,
|
|
})
|
|
} catch (error) {
|
|
console.error('加载课堂分类失败', error)
|
|
this.setData({ loading: false, loadError: true })
|
|
}
|
|
},
|
|
|
|
open(e) {
|
|
const id = e.currentTarget.dataset.id
|
|
if (id) wx.navigateTo({ url: `/pages/content-detail/content-detail?id=${id}` })
|
|
},
|
|
|
|
onRetry() {
|
|
this.loadPage()
|
|
},
|
|
})
|