feat: implement miniapp C-group pages with real API calls and design tokens
This commit is contained in:
@@ -1 +1,76 @@
|
||||
Page({data:{categories:['推荐','认识自己','沟通成长','关系经营','婚恋反诈'],active:0,articles:[{id:'1',title:'第一次见面,怎样自然地开始交流',category:'沟通成长',read:'5分钟'},{id:'2',title:'建立健康关系,需要怎样的边界',category:'关系经营',read:'6分钟'},{id:'3',title:'警惕以投资为名的婚恋骗局',category:'婚恋反诈',read:'4分钟'}]},select(e){this.setData({active:e.currentTarget.dataset.index})},open(e){wx.navigateTo({url:'/pages/content-detail/content-detail?id='+e.currentTarget.dataset.id})}})
|
||||
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()
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user