This commit is contained in:
Zylan
2025-03-27 21:40:28 +08:00
parent af19ce01d6
commit 6f0ebe0d7b
4 changed files with 249 additions and 2 deletions

104
app.py
View File

@@ -13,6 +13,8 @@ import time
import os
import json
import traceback
import requests
from datetime import datetime
app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*", ping_timeout=30, ping_interval=5, max_http_buffer_size=50 * 1024 * 1024)
@@ -64,7 +66,14 @@ def create_tray_icon():
@app.route('/')
def index():
local_ip = get_local_ip()
return render_template('index.html', local_ip=local_ip)
# 检查更新
try:
update_info = check_for_updates()
except:
update_info = {'has_update': False}
return render_template('index.html', local_ip=local_ip, update_info=update_info)
@socketio.on('connect')
def handle_connect():
@@ -490,6 +499,99 @@ def before_request_handler():
init_model_config()
app._model_config_initialized = True
# 版本检查函数
def check_for_updates():
"""检查GitHub上是否有新版本"""
try:
# 读取当前版本信息
version_file = os.path.join(CONFIG_DIR, 'version.json')
with open(version_file, 'r', encoding='utf-8') as f:
version_info = json.load(f)
current_version = version_info.get('version', '0.0.0')
repo = version_info.get('github_repo', 'Zippland/Snap-Solver')
# 请求GitHub API获取最新发布版本
api_url = f"https://api.github.com/repos/{repo}/releases/latest"
# 添加User-Agent以符合GitHub API要求
headers = {'User-Agent': 'Snap-Solver-Update-Checker'}
response = requests.get(api_url, headers=headers, timeout=5)
if response.status_code == 200:
latest_release = response.json()
latest_version = latest_release.get('tag_name', '').lstrip('v')
# 如果版本号为空,尝试从名称中提取
if not latest_version and 'name' in latest_release:
import re
version_match = re.search(r'v?(\d+\.\d+\.\d+)', latest_release['name'])
if version_match:
latest_version = version_match.group(1)
# 比较版本号(简单比较,可以改进为更复杂的语义版本比较)
has_update = compare_versions(latest_version, current_version)
update_info = {
'has_update': has_update,
'current_version': current_version,
'latest_version': latest_version,
'release_url': latest_release.get('html_url', f"https://github.com/{repo}/releases/latest"),
'release_date': latest_release.get('published_at', ''),
'release_notes': latest_release.get('body', ''),
}
# 缓存更新信息
update_info_file = os.path.join(CONFIG_DIR, 'update_info.json')
with open(update_info_file, 'w', encoding='utf-8') as f:
json.dump(update_info, f, ensure_ascii=False, indent=2)
return update_info
# 如果无法连接GitHub尝试读取缓存的更新信息
update_info_file = os.path.join(CONFIG_DIR, 'update_info.json')
if os.path.exists(update_info_file):
with open(update_info_file, 'r', encoding='utf-8') as f:
return json.load(f)
return {'has_update': False, 'current_version': current_version}
except Exception as e:
print(f"检查更新失败: {str(e)}")
# 出错时返回一个默认的值
return {'has_update': False, 'error': str(e)}
def compare_versions(version1, version2):
"""比较两个版本号如果version1比version2更新则返回True"""
try:
v1_parts = [int(x) for x in version1.split('.')]
v2_parts = [int(x) for x in version2.split('.')]
# 确保两个版本号的组成部分长度相同
while len(v1_parts) < len(v2_parts):
v1_parts.append(0)
while len(v2_parts) < len(v1_parts):
v2_parts.append(0)
# 逐部分比较
for i in range(len(v1_parts)):
if v1_parts[i] > v2_parts[i]:
return True
elif v1_parts[i] < v2_parts[i]:
return False
# 完全相同的版本
return False
except:
# 如果解析出错,默认不更新
return False
@app.route('/api/check-update', methods=['GET'])
def api_check_update():
"""检查更新的API端点"""
update_info = check_for_updates()
return jsonify(update_info)
if __name__ == '__main__':
local_ip = get_local_ip()
print(f"Local IP Address: {local_ip}")

5
config/version.json Normal file
View File

@@ -0,0 +1,5 @@
{
"version": "1.0.0",
"build_date": "2025-03-27",
"github_repo": "Zippland/Snap-Solver"
}

View File

@@ -1515,3 +1515,78 @@ button:disabled {
.footer-link.xiaohongshu-link:hover i {
color: #ff4d6d;
}
/* 更新通知样式 */
.update-notice {
background-color: rgba(var(--primary-rgb), 0.1);
border-bottom: 1px solid rgba(var(--primary-rgb), 0.2);
padding: 0.5rem 0;
transition: all 0.3s ease;
overflow: hidden;
max-height: 3rem;
}
.update-notice.hidden {
max-height: 0;
padding: 0;
border-bottom: none;
}
.update-notice-content {
display: flex;
align-items: center;
justify-content: center;
gap: 0.75rem;
font-size: 0.85rem;
color: var(--text-secondary);
}
.update-notice-content i {
color: var(--primary);
}
.update-link {
color: var(--primary);
text-decoration: none;
font-weight: 500;
padding: 0.25rem 0.75rem;
border-radius: 3rem;
background: rgba(var(--primary-rgb), 0.1);
transition: all 0.2s ease;
}
.update-link:hover {
background: rgba(var(--primary-rgb), 0.2);
transform: translateY(-1px);
}
/* 版本徽章样式 */
.version-badge {
font-size: 0.75rem;
opacity: 0.6;
margin-left: 0.5rem;
padding: 0.1rem 0.35rem;
border-radius: 3rem;
background-color: rgba(var(--surface-rgb), 0.3);
}
/* 媒体查询 */
@media (max-width: 768px) {
.update-notice-content {
font-size: 0.75rem;
}
.update-link {
padding: 0.2rem 0.5rem;
}
}
@media (max-width: 480px) {
.update-notice-content > span:not(#updateVersion) {
display: none;
}
.update-notice-content {
gap: 0.5rem;
}
}

View File

@@ -26,6 +26,18 @@
</div>
</header>
<!-- 更新通知条 -->
<div id="updateNotice" class="update-notice hidden">
<div class="update-notice-content">
<i class="fas fa-arrow-alt-circle-up"></i>
<span>发现新版本: <span id="updateVersion"></span></span>
<a id="updateLink" href="#" target="_blank" class="update-link">查看更新</a>
<button id="closeUpdateNotice" class="btn-icon">
<i class="fas fa-times"></i>
</button>
</div>
</div>
<main class="app-main">
<div class="content-panel">
<div class="capture-section">
@@ -261,7 +273,7 @@
<footer class="app-footer">
<div class="footer-content">
<div class="footer-text">
<span>© 2024 Snap-Solver</span>
<span>© 2024 Snap-Solver <span class="version-badge">v<span id="currentVersion">{{ update_info.current_version }}</span></span></span>
<div class="user-counter">
<span>访问</span>
<div id="visitorCount" class="counter-number">--</div>
@@ -283,5 +295,58 @@
<script src="{{ url_for('static', filename='js/ui.js') }}"></script>
<script src="{{ url_for('static', filename='js/settings.js') }}"></script>
<script src="{{ url_for('static', filename='js/main.js') }}"></script>
<!-- 更新检查初始化 -->
<script>
document.addEventListener('DOMContentLoaded', function() {
// 初始化更新检查
const updateInfo = {{ update_info|tojson|safe }};
if (updateInfo && updateInfo.has_update) {
showUpdateNotice(updateInfo);
}
// 24小时后再次检查更新
setTimeout(checkForUpdates, 24 * 60 * 60 * 1000);
});
function showUpdateNotice(updateInfo) {
const updateNotice = document.getElementById('updateNotice');
const updateVersion = document.getElementById('updateVersion');
const updateLink = document.getElementById('updateLink');
if (updateInfo.latest_version) {
updateVersion.textContent = updateInfo.latest_version;
}
if (updateInfo.release_url) {
updateLink.href = updateInfo.release_url;
} else {
updateLink.href = 'https://github.com/Zippland/Snap-Solver/releases/latest';
}
updateNotice.classList.remove('hidden');
// 绑定关闭按钮事件
document.getElementById('closeUpdateNotice').addEventListener('click', function() {
updateNotice.classList.add('hidden');
// 记住用户已关闭此版本的通知
localStorage.setItem('dismissedUpdate', updateInfo.latest_version);
});
}
function checkForUpdates() {
fetch('/api/check-update')
.then(response => response.json())
.then(updateInfo => {
const dismissedVersion = localStorage.getItem('dismissedUpdate');
// 只有当有更新且用户没有关闭过此版本的通知时才显示
if (updateInfo.has_update && dismissedVersion !== updateInfo.latest_version) {
showUpdateNotice(updateInfo);
}
})
.catch(error => console.error('检查更新失败:', error));
}
</script>
</body>
</html>