feat: 新增 API 获取内容方式并优化微信发布流程

This commit is contained in:
王鹏
2026-04-13 15:41:39 +08:00
parent 165b32f74a
commit 245f9cdf41
4 changed files with 106 additions and 23 deletions

74
gui.py
View File

@@ -5,6 +5,7 @@ from tkinter import ttk, messagebox, scrolledtext
import threading
import sys
import os
import subprocess
# 添加当前目录到路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
@@ -137,6 +138,14 @@ class YidaimaGUI:
# 刷新按钮
ttk.Button(scheme_frame, text="刷新", command=self.refresh_css_schemes, width=8).grid(row=0, column=1, padx=(5, 0))
# 调用API方式选项
self.use_api_var = tk.BooleanVar(value=False)
ttk.Checkbutton(
config_frame,
text="调用API方式 (跳过浏览器自动化直接调用API获取文章内容)",
variable=self.use_api_var
).grid(row=2, column=0, columnspan=2, sticky=tk.W, pady=(10, 0))
# === 操作按钮区域 ===
button_frame = ttk.Frame(self.tab_publish)
button_frame.grid(row=1, column=0, pady=(10, 10))
@@ -414,25 +423,62 @@ class YidaimaGUI:
self.root.after(0, lambda: self.log("=" * 50))
self.root.after(0, lambda: self.log("开始运行完整流程..."))
self.root.after(0, lambda: self.log("=" * 50))
# 获取配置(从配置文件读取)
chrome_path = self.config.get("chrome.path", "")
username = self.config.get("step1.username", "")
password = self.config.get("step1.password", "")
project_name = self.project_name_var.get()
use_api = self.use_api_var.get()
# Step 1
self.root.after(0, lambda: self.log("\n[Step 1] 开始执行..."))
# 定义日志回调函数,将 step1 的日志输出到 GUI
def step1_log_callback(message: str):
self.root.after(0, lambda msg=message: self.log(msg))
step1 = Step1FeastCoding(chrome_path, username, password, log_callback=step1_log_callback)
step1.project_name = project_name # 设置项目名称
step1.run()
if use_api:
# API 方式获取内容
self.root.after(0, lambda: self.log("[Step 1] 使用 API 方式获取文章内容..."))
import requests
import re
# 项目名称中提取编号,如"【A167】" -> "A167"
match = re.search(r'【(.+?)】', project_name)
code_name = match.group(1) if match else project_name
api_url = "https://feast.yidaima.cn/prod-api/office/copyTemplate/open/generate"
params = {
"templateName": "FeastCoding",
"codeName": code_name
}
try:
response = requests.get(api_url, params=params, timeout=30)
response.raise_for_status()
json_data = response.json()
# 从 JSON 中提取 msg 字段内容
content = json_data.get("msg", "")
# 将 \\n 替换为空行
content = content.replace("\\n", "\n")
# 设置剪贴板内容
subprocess.run(
["powershell", "-command", f"Set-Clipboard -Value '{content.replace(chr(39), chr(39)+chr(39))}'"],
capture_output=True
)
self.root.after(0, lambda: self.log(f"[Step 1] API 调用成功,已复制到剪贴板,内容长度: {len(content)} 字符"))
except Exception as e:
self.root.after(0, lambda: self.log(f"[Step 1] API 调用失败: {e}"))
raise
else:
# 浏览器方式
self.root.after(0, lambda: self.log("[Step 1] 使用浏览器方式获取文章内容..."))
# 定义日志回调函数,将 step1 的日志输出到 GUI
def step1_log_callback(message: str):
self.root.after(0, lambda msg=message: self.log(msg))
step1 = Step1FeastCoding(chrome_path, username, password, log_callback=step1_log_callback)
step1.project_name = project_name # 设置项目名称
step1.run()
self.root.after(0, lambda: self.log("[Step 1] 浏览器方式执行完成!"))
self.root.after(0, lambda: self.log("[Step 1] 执行完成!"))
# Step 2
self.root.after(0, lambda: self.log("\n[Step 2] 开始执行..."))
@@ -444,15 +490,15 @@ class YidaimaGUI:
scheme_name = self.css_scheme_var.get()
css_scheme_id = self._get_scheme_id_by_name(scheme_name)
step2 = Step2Converter(log_callback=log_callback, css_scheme_id=css_scheme_id)
step2 = Step2Converter(log_callback=log_callback, css_scheme_id=css_scheme_id, project_name=project_name)
step2.run()
self.root.after(0, lambda: self.log("[Step 2] 执行完成!"))
self.root.after(0, lambda: self.log("\n" + "=" * 50))
self.root.after(0, lambda: self.log("完整流程执行成功!"))
self.root.after(0, lambda: self.log("=" * 50))
self.root.after(0, lambda: messagebox.showinfo("成功", "完整流程执行成功!"))
except Exception as e:
error_msg = str(e)
self.root.after(0, lambda: self.log(f"\n[ERROR] {error_msg}"))