1
1
mirror of https://github.com/KenanZhu/AutoLibrary.git synced 2026-08-02 06:09:36 +08:00

refactor(bulletin): 提取 ALBulletinFetchWorker 到独立文件

This commit is contained in:
2026-06-26 09:16:45 +08:00
parent 92c5fa0dfb
commit 2fe4584129
4 changed files with 134 additions and 105 deletions
+4 -45
View File
@@ -7,14 +7,11 @@ This software is provided "as is", without any warranty of any kind.
You may use, modify, and distribute this file under the terms of the MIT License.
See the LICENSE file for details.
"""
import requests
from datetime import datetime, timedelta
from typing import Any
from PySide6.QtCore import (
Qt,
QThread,
Signal,
Slot,
QTimer
@@ -32,53 +29,12 @@ from PySide6.QtWidgets import (
QWidget
)
from gui.ALBulletinWorker import ALBulletinFetchWorker
from gui.ALStatusLabel import ALStatusLabel
from gui.resources.ui.Ui_ALBulletinDialog import Ui_ALBulletinDialog
from managers.bulletin.BulletinManager import instance as bulletinInstance
class ALBulletinFetchWorker(QThread):
"""
Worker thread for fetching bulletins from the server.
"""
fetchWorkerIsFinished = Signal(dict)
fetchWorkerFinishedWithError = Signal(str)
def __init__(
self,
parent=None,
request_url: str = "",
params: dict = None
):
super().__init__(parent)
self.__request_url = request_url.rstrip("/") if request_url else ""
self.__params = params or {}
def run(
self
):
try:
response = requests.get(
self.__request_url, params=self.__params, timeout=5
)
response.raise_for_status()
r = response.json()
if r.get("code") == 200:
data = r.get("data", {})
self.fetchWorkerIsFinished.emit(data)
else:
raise Exception(
f"服务器返回错误: [{r.get('code', '未知代码')}] {r.get('msg', '未知错误')}"
)
except requests.RequestException as e:
self.fetchWorkerFinishedWithError.emit(f"获取公告数据时发生网络错误: \n{e}")
except Exception as e:
self.fetchWorkerFinishedWithError.emit(f"获取公告数据时发生未知错误: \n{e}")
class ALBulletinItemWidget(QWidget):
"""
Single bulletin item widget for the bulletin list.
@@ -216,6 +172,9 @@ class ALBulletinDialog(QDialog, Ui_ALBulletinDialog):
self,
iso_str: str
):
"""
Set last sync time to update the UI status.
"""
try:
dt = datetime.fromisoformat(iso_str)