From 49b64cd2e2432a6dcf0b9b96662afe52a3f631af Mon Sep 17 00:00:00 2001 From: h7ml Date: Sat, 20 Jul 2024 11:51:42 +0800 Subject: [PATCH] feat(get_bias_addr): update and optimize bias address retrieval - Add detailed Chinese comments to enhance code readability. - Improve error handling and user input validation. - Enhance the upload function with exception handling. - Apply consistent formatting and improve stylesheet handling. - Refactor and document `GetBiasAddrControl` and `MyThread` classes for better clarity. --- app/ui/tool/get_bias_addr/get_bias_addr.py | 67 ++++++++++++++++++---- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/app/ui/tool/get_bias_addr/get_bias_addr.py b/app/ui/tool/get_bias_addr/get_bias_addr.py index 6d247638..6d289cf0 100644 --- a/app/ui/tool/get_bias_addr/get_bias_addr.py +++ b/app/ui/tool/get_bias_addr/get_bias_addr.py @@ -11,7 +11,8 @@ from app.decrypt.get_bias_addr import BiasAddr from .getBiasAddrUi import Ui_Form -Stylesheet = """ +# 样式表常量 +STYLESHEET = """ QPushButton{ background-color: rgb(250,252,253); border-radius: 5px; @@ -66,70 +67,109 @@ Height:60px; image: url(:/icons/icons/按钮_开启.svg); } - """ - class GetBiasAddrControl(QWidget, Ui_Form, QCursorGif): + """ + 获取和上传微信偏移地址的主控件类。 + """ biasAddrSignal = pyqtSignal(dict) def __init__(self, parent=None): super(GetBiasAddrControl, self).__init__(parent) self.thread = None - self.setStyleSheet(Stylesheet) + self.setStyleSheet(STYLESHEET) self.setupUi(self) self.init_ui() def init_ui(self): - self.initCursor([':/icons/icons/Cursors/%d.png' % - i for i in range(8)], self) + """ + 初始化UI组件并连接信号。 + """ + self.initCursor([':/icons/icons/Cursors/%d.png' % i for i in range(8)], self) self.setCursorTimeout(100) self.btn_get_bias_addr.clicked.connect(self.get_bias_addr) self.commandLinkButton.clicked.connect(self.show_info) self.checkBox_send_error_log.clicked.connect(self.set_error_log) def set_error_log(self): + """ + 根据复选框状态更新错误日志标签。 + """ if self.checkBox_send_error_log.isChecked(): self.label_error_log.setText('开') else: self.label_error_log.setText('关') def show_info(self): + """ + 显示关于数据收集的信息框。 + """ QMessageBox.information(self, "收集版本信息", "为了适配更多版本,需要收集微信的版本信息,该操作不会上传包括手机号、微信号、昵称等在内的任何信息\n示例数据:\n\"3.9.9.27\": [68065304, 0, 68065112, 0, 68066576]" ) def upload(self, version_data): + """ + 上传版本数据到服务器。 + + 参数: + version_data (dict): 包含偏移地址信息的字典。 + """ url = urljoin(SERVER_API_URL, 'wxBiasAddr') try: - requests.post(url, json={'bias_dict': version_data}) + response = requests.post(url, json={'bias_dict': version_data}) + response.raise_for_status() print('版本信息上传成功') - except: - pass + except requests.RequestException as e: + print(f'版本信息上传失败: {e}') def get_bias_addr(self): + """ + 根据用户输入获取偏移地址,并启动后台线程。 + """ + # 从用户输入框获取微信账号、手机号和昵称 account = self.lineEdit_wx_alias.text() mobile = self.lineEdit_tel.text() name = self.lineEdit_wx_name.text() + + # 检查是否所有输入框都已填写,若有未填写的则弹出错误提示框 if not all([account, mobile, name]): - QMessageBox.critical(self, "错误", - "请把所有信息填写完整") + QMessageBox.critical(self, "错误", "请把所有信息填写完整") return + + # 初始化密钥和数据库路径,密钥目前未使用因此设为None,数据库路径设为"test" key = None db_path = "test" + + # 启动忙碌指示(例如光标变成加载状态) self.startBusy() + + # 创建一个后台线程实例,并传递用户输入的数据 self.thread = MyThread(account, mobile, name, key, db_path) + + # 连接线程的信号到主窗口的处理方法,以便线程完成后更新UI self.thread.signal.connect(self.set_bias_addr) + + # 启动线程 self.thread.start() def set_bias_addr(self, data): + """ + 设置偏移地址并选择性上传。 + + 参数: + data (dict): 包含偏移地址信息的字典。 + """ if self.checkBox_send_error_log.isChecked(): self.upload(data) self.stopBusy() self.biasAddrSignal.emit(data) - class MyThread(QThread): + """ + 获取偏移地址的后台线程类。 + """ signal = pyqtSignal(dict) def __init__(self, account, mobile, name, key, db_path): @@ -141,6 +181,9 @@ def __init__(self, account, mobile, name, key, db_path): self.db_path = db_path def run(self): + """ + 运行线程以获取偏移地址。 + """ bias_addr = BiasAddr(self.account, self.mobile, self.name, self.key, self.db_path) data = bias_addr.run(logging_path=True) self.signal.emit(data)