WeChatApps/pages/article/article.js
2025-05-15 18:54:22 +08:00

47 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { ArticleManager } = require('../../models/articleManager');
const { formatDate } = require('../../utils/util');
Page({
data: {
article: null,
formatDate: ''
},
onLoad: function (options) {
this.articleManager = new ArticleManager();
// 如果有指定文章ID则显示该文章
if (options.id) {
this.loadArticle(options.id);
} else {
// 否则显示最新文章
const latestArticle = this.articleManager.getLatestArticle();
if (latestArticle) {
this.loadArticle(latestArticle.id);
}
}
},
// 加载指定ID的文章
loadArticle: function(id) {
const article = this.articleManager.getArticleById(id);
if (article) { // 格式化日期
const date = new Date(article.createTime);
const formattedDate = formatDate(date); this.setData({
article: article,
formatDate: formattedDate
});
} },
// 分享功能
onShareAppMessage: function () {
if (this.data.article) {
return {
title: this.data.article.title,
path: `/pages/article/article?id=${this.data.article.id}`
};
}
return {
title: '精选文章',
path: '/pages/article/article'
};
}
})