54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
const { getArticlesByCategory, getCategories } = require('../../utils/knowledgeData');
|
|
|
|
Page({
|
|
data: {
|
|
category: '',
|
|
categoryName: '',
|
|
articles: []
|
|
},
|
|
|
|
handleBack() {
|
|
wx.navigateBack({ delta: 1 });
|
|
},
|
|
|
|
onLoad(options) {
|
|
const category = options.category || 'beginner';
|
|
const categories = getCategories();
|
|
const categoryInfo = categories.find(c => c.id === category);
|
|
|
|
this.setData({
|
|
category: category,
|
|
categoryName: categoryInfo ? categoryInfo.name : '文章列表',
|
|
articles: getArticlesByCategory(category)
|
|
});
|
|
},
|
|
|
|
// 点击文章
|
|
goToArticle(e) {
|
|
const id = e.currentTarget.dataset.id;
|
|
const articles = this.data.articles;
|
|
const article = articles.find(a => a.id === id);
|
|
|
|
if (!article) {
|
|
wx.showToast({
|
|
title: '文章不存在',
|
|
icon: 'none'
|
|
});
|
|
return;
|
|
}
|
|
|
|
// 根据文章类型跳转
|
|
if (article.type === 'web') {
|
|
// 外链文章 → 跳转到 webview
|
|
wx.navigateTo({
|
|
url: `/pages/webview/index?url=${encodeURIComponent(article.url)}`
|
|
});
|
|
} else {
|
|
// 本地文章 → 跳转到 article 详情页
|
|
wx.navigateTo({
|
|
url: `/pages/knowledge/article?id=${id}`
|
|
});
|
|
}
|
|
}
|
|
});
|