diff --git a/app/ui/tool/setting/setting.py b/app/ui/tool/setting/setting.py index b82a2e3..028b938 100644 --- a/app/ui/tool/setting/setting.py +++ b/app/ui/tool/setting/setting.py @@ -1,8 +1,12 @@ import json import os.path -from PyQt5.QtCore import pyqtSignal +import time + +import requests +from PyQt5.QtCore import pyqtSignal, QThread from PyQt5.QtWidgets import QWidget, QMessageBox from app.config import SEND_LOG_FLAG +from app.person import Me from .settingUi import Ui_Form Stylesheet = """ @@ -79,6 +83,8 @@ class SettingControl(QWidget, Ui_Form): self.btn_addstopword.clicked.connect(self.add_stopwords) self.btn_addnewword_2.clicked.connect(self.add_new_words) + self.commandLinkButton_send_error_log.clicked.connect(self.show_info) + self.btn_send_error_log.clicked.connect(self.send_error_log) self.init_ui() self.read_data() @@ -86,6 +92,11 @@ class SettingControl(QWidget, Ui_Form): self.checkBox.setText('是') self.checkBox_send_error_log.clicked.connect(self.set_error_log) + def show_info(self): + QMessageBox.information(self, "收集错误信息", + "为了更好的解决用户问题,需要收集软件崩溃导致的错误信息,该操作不会上传包括手机号、微信号、昵称等在内的任何信息\n" + ) + def set_error_log(self): if self.checkBox_send_error_log.isChecked(): self.label_error_log.setText('开') @@ -132,3 +143,66 @@ class SettingControl(QWidget, Ui_Form): with open('./app/data/new_words.txt', 'w', encoding='utf-8') as f: f.write(new_words) QMessageBox.about(self, "添加成功", "自定义词添加成功") + + def send_error_log(self): + self.send_thread = MyThread() + self.send_thread.signal.connect(self.show_resp) + self.send_thread.start() + + def show_resp(self, message): + if message.get('code') == 200: + QMessageBox.about(self, "发送结果", f"日志发送成功\n{message.get('message')}") + else: + QMessageBox.about(self, "发送结果", f"{message.get('code')}:{message.get('errmsg')}") + + +class MyThread(QThread): + signal = pyqtSignal(dict) + + def __init__(self, message=''): + super(MyThread, self).__init__() + if message: + self.message = message + else: + filename = time.strftime("%Y-%m-%d", time.localtime(time.time())) + file_path = f'{filename}-log.log' + if os.path.exists(file_path): + with open(file_path, 'r', encoding='gbk') as f: + self.message = f.read() + elif os.path.exists(f'./app/log/logs/{filename}-log.log'): + with open(f'./app/log/logs/{filename}-log.log', 'r', encoding='gbk') as f: + self.message = f.read() + + def __del__(self): + pass + + def send_error_msg(self, message): + url = "http://api.lc044.love/error" + if not message: + return { + 'code': 201, + 'errmsg': '日志为空' + } + data = { + 'username': Me().wxid, + 'error': message + } + try: + response = requests.post(url, json=data) + if response.status_code == 200: + resp_info = response.json() + return resp_info + else: + return { + 'code': 503, + 'errmsg': '服务器错误' + } + except: + return { + 'code': 404, + 'errmsg': '客户端错误' + } + + def run(self): + resp_info = self.send_error_msg(self.message) + self.signal.emit(resp_info) diff --git a/app/ui/tool/setting/settingUi.py b/app/ui/tool/setting/settingUi.py index 59688db..93661b5 100644 --- a/app/ui/tool/setting/settingUi.py +++ b/app/ui/tool/setting/settingUi.py @@ -146,6 +146,9 @@ class Ui_Form(object): self.commandLinkButton_send_error_log.setDefault(False) self.commandLinkButton_send_error_log.setObjectName("commandLinkButton_send_error_log") self.horizontalLayout_2.addWidget(self.commandLinkButton_send_error_log) + self.btn_send_error_log = QtWidgets.QPushButton(self.widget_2) + self.btn_send_error_log.setObjectName("btn_send_error_log") + self.horizontalLayout_2.addWidget(self.btn_send_error_log) spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_2.addItem(spacerItem) self.label_error_log = QtWidgets.QLabel(self.widget_2) @@ -193,6 +196,7 @@ class Ui_Form(object): self.btn_addnewword_2.setText(_translate("Form", "添加自定义词")) self.commandLinkButton_send_error_log.setText(_translate("Form", "收集错误日志")) self.commandLinkButton_send_error_log.setDescription(_translate("Form", "收集错误信息以帮助改进")) + self.btn_send_error_log.setText(_translate("Form", "手动发送")) self.label_error_log.setText(_translate("Form", "开")) self.checkBox_2.setText(_translate("Form", "CheckBox")) self.checkBox.setText(_translate("Form", "CheckBox"))