From 4d3e4a09fdfc62cf083c46fcf461ee3cfd5251fc Mon Sep 17 00:00:00 2001 From: yuyx <237899745@qq.com> Date: Thu, 11 Dec 2025 14:25:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=A3=E7=A0=81=E8=B4=A8?= =?UTF-8?q?=E9=87=8F=E9=97=AE=E9=A2=98(Bug=20#20,=20#1,=20#2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复的Bug: - Bug #20: 删除重复函数定义 (update_admin_username重复定义) - Bug #1: 删除类型转换冗余 (APIBrowseResult->BrowseResult无意义转换) - Bug #2: 统一时区处理 主要改进: 1. 删除重复函数: - 删除app.py:1023处重复的update_admin_username函数定义 - 避免函数覆盖导致的潜在问题 2. 删除冗余类型转换: - APIBrowseResult和BrowseResult字段完全相同 - 删除run_task中的冗余转换代码 - 提升性能,减少维护成本 3. 统一时区处理: - 新增BEIJING_TZ常量和get_beijing_now()辅助函数 - 替换所有分散的beijing_tz创建为统一函数调用 - 删除重复的datetime/pytz导入 - 涉及6处代码统一: * log_to_client - 日志时间戳 * take_screenshot_for_account - 截图文件名 * get_screenshots - 文件修改时间 * run_scheduled_task - 定时任务星期检查 * check_user_schedules - 用户定时任务检查 * get_server_info_api - 服务器运行时长 影响: - 提升代码质量和可维护性 - 统一时区处理,避免时间混淆 - 减少内存占用和CPU开销 受影响文件: - app.py (所有修复) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- app.py | 58 +++++++++++++++------------------------------------------- 1 file changed, 15 insertions(+), 43 deletions(-) diff --git a/app.py b/app.py index 7b0349e..82c8ad9 100755 --- a/app.py +++ b/app.py @@ -45,6 +45,13 @@ from app_security import ( from app_utils import verify_and_consume_captcha +# ========== 时区辅助函数 (Bug #2 fix) ========== +BEIJING_TZ = pytz.timezone('Asia/Shanghai') + +def get_beijing_now(): + """获取北京时间的当前时间(统一时区处理)""" + return datetime.now(BEIJING_TZ) + # ========== 初始化配置 ========== config = get_config() @@ -312,8 +319,7 @@ def admin_required(f): def log_to_client(message, user_id=None, account_id=None): """发送日志到Web客户端(用户隔离)""" - beijing_tz = timezone(timedelta(hours=8)) - timestamp = datetime.now(beijing_tz).strftime('%H:%M:%S') + timestamp = get_beijing_now().strftime('%H:%M:%S') log_data = { 'timestamp': timestamp, 'message': message, @@ -1019,23 +1025,6 @@ def update_admin_username(): return jsonify({"error": "修改失败,用户名可能已存在"}), 400 - -def update_admin_username(): - """修改管理员用户名""" - data = request.json - new_username = data.get('new_username', '').strip() - - if not new_username: - return jsonify({"error": "用户名不能为空"}), 400 - - old_username = session.get('admin_username') - if database.update_admin_username(old_username, new_username): - session['admin_username'] = new_username - return jsonify({"success": True}) - return jsonify({"error": "用户名已存在"}), 400 - - - # ==================== 密码重置API ==================== # 管理员直接重置用户密码 @@ -1645,13 +1634,7 @@ def run_task(user_id, account_id, browse_type, enable_screenshot=True, source="m browse_type=browse_type, should_stop_callback=should_stop ) - # 转换结果类型以兼容后续代码 - result = BrowseResult( - success=result.success, - total_items=result.total_items, - total_attachments=result.total_attachments, - error_message=result.error_message - ) + # APIBrowseResult和BrowseResult字段完全相同,无需转换 api_browser.close() else: # API 登录失败 @@ -1984,9 +1967,7 @@ def take_screenshot_for_account(user_id, account_id, browse_type="应读", sourc time.sleep(2) # 生成截图文件名 - beijing_tz = pytz.timezone('Asia/Shanghai') - now_beijing = datetime.now(beijing_tz) - timestamp = now_beijing.strftime('%Y%m%d_%H%M%S') + timestamp = get_beijing_now().strftime('%Y%m%d_%H%M%S') user_info = database.get_user_by_id(user_id) username_prefix = user_info['username'] if user_info else f"user{user_id}" @@ -2116,8 +2097,7 @@ def get_screenshots(): filepath = os.path.join(SCREENSHOTS_DIR, filename) stat = os.stat(filepath) # 转换为北京时间 - beijing_tz = pytz.timezone('Asia/Shanghai') - created_time = datetime.fromtimestamp(stat.st_mtime, tz=beijing_tz) + created_time = datetime.fromtimestamp(stat.st_mtime, tz=BEIJING_TZ) # 解析文件名获取显示名称 # 文件名格式:用户名_登录账号_浏览类型_时间.jpg parts = filename.rsplit('.', 1)[0].split('_', 1) # 移除扩展名并分割 @@ -2605,7 +2585,6 @@ def test_proxy_api(): def get_server_info_api(): """获取服务器信息""" import psutil - import datetime # CPU使用率 cpu_percent = psutil.cpu_percent(interval=1) @@ -2623,8 +2602,8 @@ def get_server_info_api(): disk_percent = disk.percent # 运行时长 - boot_time = datetime.datetime.fromtimestamp(psutil.boot_time()) - uptime_delta = datetime.datetime.now() - boot_time + boot_time = datetime.fromtimestamp(psutil.boot_time(), tz=BEIJING_TZ) + uptime_delta = get_beijing_now() - boot_time days = uptime_delta.days hours = uptime_delta.seconds // 3600 uptime = f"{days}天{hours}小时" @@ -2807,17 +2786,13 @@ def run_scheduled_task(skip_weekday_check=False): skip_weekday_check: 是否跳过星期检查(立即执行时为True) """ try: - from datetime import datetime - import pytz - config = database.get_system_config() browse_type = config.get('schedule_browse_type', '应读') # 检查今天是否在允许执行的星期列表中(立即执行时跳过此检查) if not skip_weekday_check: # 获取北京时间的星期几 (1=周一, 7=周日) - beijing_tz = pytz.timezone('Asia/Shanghai') - now_beijing = datetime.now(beijing_tz) + now_beijing = get_beijing_now() current_weekday = now_beijing.isoweekday() # 1-7 # 获取配置的星期列表 @@ -3001,9 +2976,7 @@ def scheduled_task_worker(): """检查并执行用户定时任务""" import json try: - from datetime import datetime - beijing_tz = pytz.timezone('Asia/Shanghai') - now = datetime.now(beijing_tz) + now = get_beijing_now() current_time = now.strftime('%H:%M') current_weekday = now.isoweekday() @@ -3168,7 +3141,6 @@ def scheduled_task_worker(): Returns: UTC时间字符串,格式为 HH:MM """ - from datetime import datetime, timedelta # 解析CST时间 hour, minute = map(int, cst_time_str.split(':')) # CST是UTC+8,所以UTC时间 = CST时间 - 8小时