71 lines
2.4 KiB
HTML
71 lines
2.4 KiB
HTML
{% extends "base.html" %}
|
|
{% block title %}文章 - 知识库{% endblock %}
|
|
{% block content %}
|
|
<div class="page-title" id="article-header">
|
|
<h1 id="article-title"></h1>
|
|
<p id="article-meta"></p>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<div class="prose" id="article-content"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<a href="/knowledge/" class="btn">← 返回知识库</a>
|
|
|
|
<script>
|
|
(async function() {
|
|
var id = window.location.pathname.match(/\/knowledge\/(\d+)\//)[1];
|
|
var res = await fetch('/api/knowledge/' + id + '/');
|
|
var data = await res.json();
|
|
if (!data.success) return;
|
|
|
|
var a = data.article;
|
|
document.getElementById('article-title').textContent = a.title;
|
|
document.getElementById('article-meta').innerHTML =
|
|
'<span class="badge badge-info">' + a.category + '</span>' +
|
|
' 由 ' + a.author_name + ' 发布于 ' + a.created_at +
|
|
' · ' + a.view_count + ' 次阅读' +
|
|
' · <a href="javascript:void(0)" id="fav-btn" class="' + (a.is_favorited ? 'favorited' : '') + '" onclick="toggleFav(' + id + ', \'knowledge\')">' + (a.is_favorited ? '⭐ 已收藏' : '☆ 收藏') + '</a>';
|
|
document.getElementById('article-content').innerHTML = a.content_html;
|
|
})();
|
|
</script>
|
|
<script>
|
|
function toggleFav(id, type) {
|
|
var btn = document.getElementById('fav-btn');
|
|
if (btn.classList.contains('favorited')) {
|
|
fetch('/api/favorites/check/?type=' + type + '&id=' + id)
|
|
.then(function(r) { return r.json(); })
|
|
.then(function(data) {
|
|
if (data.is_favorited && data.favorite_id) {
|
|
var fd = new FormData();
|
|
fd.append('csrf_token', CSRF_TOKEN);
|
|
fetch('/api/favorites/' + data.favorite_id + '/remove/', { method: 'POST', body: fd })
|
|
.then(function(r) { return r.json(); })
|
|
.then(function(d) {
|
|
if (d.success) {
|
|
btn.classList.remove('favorited');
|
|
btn.innerHTML = '☆ 收藏';
|
|
}
|
|
});
|
|
}
|
|
});
|
|
} else {
|
|
var fd = new FormData();
|
|
fd.append('csrf_token', CSRF_TOKEN);
|
|
fd.append('target_type', type);
|
|
fd.append('target_id', id);
|
|
fetch('/api/favorites/add/', { method: 'POST', body: fd })
|
|
.then(function(r) { return r.json(); })
|
|
.then(function(data) {
|
|
if (data.success) {
|
|
btn.classList.add('favorited');
|
|
btn.innerHTML = '⭐ 已收藏';
|
|
}
|
|
});
|
|
}
|
|
}
|
|
</script>
|
|
{% endblock %}
|