update web sys

This commit is contained in:
MasonLiu committed 2024-12-10 16:24:41 +08:00
1 parent de298b6809
commit f912c39598
30 files changed
+516 -90

No files matched your search

Binary file not shown.
+59
View File
@@ -0,0 +1,59 @@
from flask import Flask, jsonify, render_template
import os
app = Flask(__name__)
# 配置文件路径
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
PARENT_DIR = os.path.dirname(BASE_DIR) # 上一个文件夹
SEC_NEWS_PATH = os.path.join(PARENT_DIR, 'history', 'sec_news.md')
TECH_PASSAGE_PATH = os.path.join(PARENT_DIR, 'history', 'tech_passage.md')
# print(f"Sec News Path: {SEC_NEWS_PATH}")
# print(f"Tech Passage Path: {TECH_PASSAGE_PATH}")
@app.route('/')
def index():
return render_template('index.html')
# 替换输出内容
def replace_content(content):
content = content.replace('####', '###')
# content = content.replace('\n\n', '###')
return content
@app.route('/get-sec-news')
def get_sec_news():
print(f"尝试打开安全新闻历史推送文件: {SEC_NEWS_PATH}")
try:
with open(SEC_NEWS_PATH, 'r', encoding='utf-8') as file:
content = file.read()
content = replace_content(content)
return jsonify({'content': content}), 200
except FileNotFoundError:
print(f"文件缺失: {SEC_NEWS_PATH}")
return jsonify({'error': '安全新闻历史推送文件缺失!'}), 404
except Exception as e:
print(f"读取时出错: {SEC_NEWS_PATH}, 原因: {str(e)}")
return jsonify({'error': str(e)}), 500
@app.route('/get-tech-passage')
def get_tech_passage():
print(f"尝试打开技术文章历史推送文件: {TECH_PASSAGE_PATH}")
try:
with open(TECH_PASSAGE_PATH, 'r', encoding='utf-8') as file:
content = file.read()
content = replace_content(content)
return jsonify({'content': content}), 200
except FileNotFoundError:
print(f"文件缺失: {TECH_PASSAGE_PATH}")
return jsonify({'error': '技术文章历史推送文件缺失!'}), 404
except Exception as e:
print(f"读取时出错: {TECH_PASSAGE_PATH}, 原因: {str(e)}")
return jsonify({'error': str(e)}), 500
def run_server():
app.run(host='0.0.0.0', port=5000)
if __name__ == '__main__':
app.run(debug=True) # 在生产环境中应设置为 False
Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

+133
View File
@@ -0,0 +1,133 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>资讯推送Web端</title>
<link rel="shortcut icon" href="./static/img/favicon.ico">
<!-- 引入 Layui 的 CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui-src/dist/css/layui.css">
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #333;
}
#markdown-content {
white-space: pre-wrap;
}
/* 返回顶部按钮样式 */
#back-to-top {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
font-size: 18px;
border: none;
outline: none;
background-color: #555;
color: white;
cursor: pointer;
padding: 15px;
border-radius: 4px;
}
#back-to-top:hover {
background-color: #777;
}
</style>
</head>
<body>
<div class="layui-container">
<h1 class="layui-title" id="page-title">历史推送读取</h1>
<button id="toggle-fetch-btn" class="layui-btn">切换读取源</button>
<div id="markdown-content" class="layui-card-body"></div>
</div>
<!-- 返回顶部按钮 -->
<button id="back-to-top" title="Go to top">Top</button>
<!-- 引入 Layui 的 JS -->
<script src="https://cdn.jsdelivr.net/npm/layui-src/dist/layui.js"></script>
<script>
layui.use(['layer'], function(){
var layer = layui.layer;
var currentUrl = '/get-sec-news';
var titleMap = {
'/get-sec-news': '安全新闻',
'/get-tech-passage': '技术文章'
};
function updateTitle(url) {
document.getElementById('page-title').innerText = titleMap[url] || '历史推送读取';
}
document.getElementById('toggle-fetch-btn').addEventListener('click', function() {
if (currentUrl === '/get-sec-news') {
currentUrl = '/get-tech-passage';
} else {
currentUrl = '/get-sec-news';
}
updateTitle(currentUrl);
fetch(currentUrl)
.then(response => response.json())
.then(data => {
if (data.content) {
const htmlContent = marked.parse(data.content);
document.getElementById('markdown-content').innerHTML = htmlContent;
} else {
document.getElementById('markdown-content').innerHTML = '<p>加载历史推送文件时出错!</p>';
}
})
.catch(error => {
console.error('获取源文件失败:', error);
document.getElementById('markdown-content').innerHTML = '<p>无法加载历史推送文件!</p>';
layer.msg('Failed to load markdown.', {icon: 5});
});
});
// Initial fetch on page load
updateTitle(currentUrl);
fetch(currentUrl)
.then(response => response.json())
.then(data => {
if (data.content) {
const htmlContent = marked.parse(data.content);
document.getElementById('markdown-content').innerHTML = htmlContent;
} else {
document.getElementById('markdown-content').innerHTML = '<p>加载历史推送文件时出错!</p>';
}
})
.catch(error => {
console.error('获取源文件失败:', error);
document.getElementById('markdown-content').innerHTML = '<p>无法加载历史推送文件!</p>';
layer.msg('Failed to load markdown.', {icon: 5});
});
// 显示或隐藏返回顶部按钮
window.onscroll = function() {
scrollFunction();
};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
document.getElementById("back-to-top").style.display = "block";
} else {
document.getElementById("back-to-top").style.display = "none";
}
}
// 返回顶部按钮点击事件
document.getElementById("back-to-top").addEventListener("click", function() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
});
});
</script>
</body>
</html>