feat: 益选 OCR 订单处理系统初始提交
- 智能供应商识别(蓉城易购/烟草/杨碧月/通用) - 百度 OCR 表格识别集成 - 规则引擎(列映射/数据清洗/单位转换/规格推断) - 条码映射管理与云端同步(Gitea REST API) - 云端同步支持:条码映射、供应商配置、商品资料、采购模板 - 拖拽一键处理(图片→OCR→Excel→合并) - 191 个单元测试 - 移除无用的模板管理功能 - 清理 IDE 产物目录 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""用户设置与最近文件管理模块"""
|
||||
|
||||
import os
|
||||
import json
|
||||
import re
|
||||
import tkinter as tk
|
||||
from typing import Dict, List, Any
|
||||
|
||||
from app.core.utils.log_utils import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
RECENT_LIST_WIDGET = None
|
||||
|
||||
|
||||
def load_user_settings():
|
||||
try:
|
||||
path = os.path.abspath(os.path.join('data', 'user_settings.json'))
|
||||
if os.path.exists(path):
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
except Exception as e:
|
||||
logger.debug(f"加载用户设置失败: {e}")
|
||||
return {}
|
||||
|
||||
|
||||
def save_user_settings(settings: Dict[str, Any]):
|
||||
try:
|
||||
os.makedirs('data', exist_ok=True)
|
||||
path = os.path.abspath(os.path.join('data', 'user_settings.json'))
|
||||
with open(path, 'w', encoding='utf-8') as f:
|
||||
json.dump(settings, f, ensure_ascii=False, indent=2)
|
||||
except Exception as e:
|
||||
logger.debug(f"保存用户设置失败: {e}")
|
||||
|
||||
|
||||
def get_recent_files() -> List[str]:
|
||||
s = load_user_settings()
|
||||
items = s.get('recent_files', [])
|
||||
if not isinstance(items, list):
|
||||
return []
|
||||
|
||||
def _allowed(p: str) -> bool:
|
||||
try:
|
||||
if not isinstance(p, str) or not os.path.isfile(p):
|
||||
return False
|
||||
ext = os.path.splitext(p)[1].lower()
|
||||
return ext in {'.xlsx', '.xls', '.jpg', '.jpeg', '.png', '.bmp'}
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
kept = [p for p in items if _allowed(p)]
|
||||
if not kept:
|
||||
candidates = []
|
||||
for d in ['data/output', 'data/result']:
|
||||
try:
|
||||
if os.path.exists(d):
|
||||
for name in os.listdir(d):
|
||||
p = os.path.join(d, name)
|
||||
if _allowed(p):
|
||||
candidates.append(p)
|
||||
except Exception:
|
||||
pass
|
||||
if candidates:
|
||||
kept = candidates
|
||||
try:
|
||||
kept_sorted = sorted(kept, key=lambda p: os.path.getmtime(p), reverse=True)
|
||||
except Exception:
|
||||
kept_sorted = kept
|
||||
if kept_sorted != items or len(kept_sorted) != len(items):
|
||||
s['recent_files'] = kept_sorted[:20]
|
||||
save_user_settings(s)
|
||||
return kept_sorted[:10]
|
||||
|
||||
|
||||
def refresh_recent_list_widget():
|
||||
try:
|
||||
global RECENT_LIST_WIDGET
|
||||
if RECENT_LIST_WIDGET is None:
|
||||
return
|
||||
RECENT_LIST_WIDGET.delete(0, tk.END)
|
||||
for i, p in enumerate(get_recent_files(), start=1):
|
||||
RECENT_LIST_WIDGET.insert(tk.END, f"{i}. {p}")
|
||||
except Exception as e:
|
||||
logger.debug(f"刷新最近文件列表失败: {e}")
|
||||
|
||||
|
||||
def _extract_path_from_recent_item(s: str) -> str:
|
||||
try:
|
||||
m = re.match(r'^(\d+)\.\s+(.*)$', s)
|
||||
p = m.group(2) if m else s
|
||||
return p.strip().strip('"')
|
||||
except Exception:
|
||||
return s.strip().strip('"')
|
||||
|
||||
|
||||
def add_recent_file(path: str) -> None:
|
||||
try:
|
||||
if not path:
|
||||
return
|
||||
try:
|
||||
if not os.path.isfile(path):
|
||||
return
|
||||
ext = os.path.splitext(path)[1].lower()
|
||||
if ext not in {'.xlsx', '.xls', '.jpg', '.jpeg', '.png', '.bmp'}:
|
||||
return
|
||||
except Exception:
|
||||
return
|
||||
s = load_user_settings()
|
||||
items = s.get('recent_files', [])
|
||||
items = [p for p in items if p != path]
|
||||
items.insert(0, path)
|
||||
s['recent_files'] = items[:20]
|
||||
save_user_settings(s)
|
||||
refresh_recent_list_widget()
|
||||
except Exception as e:
|
||||
logger.debug(f"添加最近文件失败: {e}")
|
||||
|
||||
|
||||
def clear_recent_files():
|
||||
try:
|
||||
s = load_user_settings()
|
||||
s['recent_files'] = []
|
||||
save_user_settings(s)
|
||||
except Exception as e:
|
||||
logger.debug(f"清空最近文件失败: {e}")
|
||||
Reference in New Issue
Block a user