modify: 封装webview通用模块

This commit is contained in:
2025-09-30 11:01:00 +08:00
parent 7abb876100
commit 01bd8e0f5e
14 changed files with 186 additions and 103 deletions
+4
View File
@@ -0,0 +1,4 @@
from .webview_queue_sqlite_operations import WebViewQueueSqliteOperations
__all__ = ["WebViewQueueSqliteOperations"]
+8
View File
@@ -0,0 +1,8 @@
from PySide6.QtCore import QObject
class BaseWebViewBridge(QObject):
def __init__(self, webview, parent=None):
self.webview = webview
super().__init__(parent)
pass
+44
View File
@@ -0,0 +1,44 @@
from PySide6.QtCore import QObject
from .webview_queue_sqlite_operations import WebViewQueueSqliteOperations
from .base_webview_bridge import BaseWebViewBridge
from typing import List
from PySide6.QtWebChannel import QWebChannel
class WebviewController(QObject):
def __init__(
self,
web_engine_page,
webview_bridge_list: List[type[BaseWebViewBridge]],
web_webview,
scheduler,
parent=None,
):
super().__init__(parent)
self.page = web_engine_page
self.bridge_instances = {}
# 创建bridge实例并保存
for bridge_class in webview_bridge_list:
instance = bridge_class(web_webview)
bridge_name = bridge_class.__name__.lower()
setattr(self, bridge_name, instance)
self.bridge_instances[bridge_name] = instance
self.queue_sqlite_operations = WebViewQueueSqliteOperations(
web_webview, scheduler
)
self.channel = QWebChannel()
# 使用已创建的实例注册到channel
for bridge_name, instance in self.bridge_instances.items():
self.channel.registerObject(bridge_name, instance)
# 为了向前兼容,确保注册名为 "entrance" 的对象
if "webviewbridge" in self.bridge_instances:
self.channel.registerObject(
"entrance", self.bridge_instances["webviewbridge"]
)
self.channel.registerObject("queueSqlite", self.queue_sqlite_operations)
self.page.setWebChannel(self.channel)
@@ -0,0 +1,54 @@
import json
from PySide6.QtCore import QObject, Slot, Signal
from qframelesswindow.webengine import FramelessWebEngineView
from queue_sqlite.model import MessageItem
from queue_sqlite.scheduler import QueueScheduler
class WebViewQueueSqliteOperations(QObject):
"""
用于前端调用QueueSqlite的队列操作
"""
update_queue_state_signal = Signal(dict, str)
def __init__(
self,
webview: FramelessWebEngineView,
queue_scheduler: QueueScheduler,
parent=None,
):
super().__init__(parent)
self.webview = webview
self.page = self.webview.page()
self.update_queue_state_signal.connect(self.update_queue_state)
self.queue_scheduler = queue_scheduler
@Slot(str, str, result=str)
def send_message(self, message: str, store_key: str):
def callback(result: MessageItem):
print(f"send_message callback: {result}")
self.update_queue_state_signal.emit(result.to_dict(), store_key)
self.queue_scheduler.send_message(
MessageItem.from_dict(json.loads(message)), callback
)
def update_queue_state(self, result: dict, store_key: str):
json_str = json.dumps(result)
print(f"result: {result}")
print(f"json_str: {json_str}")
js_code = f"""
try {{
if (typeof window.queueStore.updateQueueData === 'function') {{
window.queueStore.updateQueueData('{store_key}', {json_str});
}} else {{
console.log('updateQueueData function not found');
}}
}} catch (error) {{
console.error('Error in updateQueueData:', error);
}}
"""
print(f"执行的JavaScript代码: {js_code}")
self.page.runJavaScript(js_code)