444 lines
16 KiB
Python
444 lines
16 KiB
Python
import sys
|
|
import os
|
|
|
|
# 添加父目录到系统路径
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
parent_dir = os.path.dirname(current_dir)
|
|
sys.path.append(parent_dir)
|
|
|
|
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
|
|
QPushButton, QLabel, QLineEdit, QTextEdit, QMessageBox,
|
|
QHBoxLayout, QFrame)
|
|
from PyQt5.QtCore import Qt, QThread, pyqtSignal
|
|
from PyQt5.QtGui import QFont, QIcon
|
|
from update_cursor_token import CursorTokenUpdater
|
|
from logger import logging
|
|
|
|
class UpdateWorker(QThread):
|
|
"""后台更新线程"""
|
|
finished = pyqtSignal(bool, str)
|
|
progress = pyqtSignal(str)
|
|
|
|
def __init__(self, updater):
|
|
super().__init__()
|
|
self.updater = updater
|
|
|
|
def run(self):
|
|
try:
|
|
success = self.updater.full_update_process()
|
|
if success:
|
|
self.finished.emit(True, "更新成功!")
|
|
else:
|
|
self.finished.emit(False, "更新失败,请查看日志")
|
|
except Exception as e:
|
|
self.finished.emit(False, f"发生错误: {str(e)}")
|
|
|
|
class MainWindow(QMainWindow):
|
|
def __init__(self):
|
|
super().__init__()
|
|
try:
|
|
logging.info("正在初始化主窗口...")
|
|
self.updater = CursorTokenUpdater()
|
|
self.init_ui()
|
|
logging.info("主窗口初始化完成")
|
|
except Exception as e:
|
|
logging.error(f"初始化主窗口时发生错误: {str(e)}")
|
|
QMessageBox.critical(self, "错误", f"初始化失败: {str(e)}")
|
|
|
|
def init_ui(self):
|
|
# 设置窗口基本属性
|
|
self.setWindowTitle('听泉助手')
|
|
self.setMinimumSize(600, 500)
|
|
self.setStyleSheet("""
|
|
QMainWindow {
|
|
background-color: #f5f5f7;
|
|
}
|
|
QPushButton {
|
|
background-color: #0066cc;
|
|
color: white;
|
|
border: none;
|
|
border-radius: 6px;
|
|
padding: 10px;
|
|
font-size: 14px;
|
|
min-width: 120px;
|
|
}
|
|
QPushButton:hover {
|
|
background-color: #0052a3;
|
|
}
|
|
QPushButton:pressed {
|
|
background-color: #003d7a;
|
|
}
|
|
QLabel {
|
|
font-size: 14px;
|
|
color: #333333;
|
|
}
|
|
QTextEdit {
|
|
border: 1px solid #cccccc;
|
|
border-radius: 6px;
|
|
padding: 10px;
|
|
background-color: white;
|
|
font-family: Consolas, Monaco, monospace;
|
|
}
|
|
QLineEdit {
|
|
border: 1px solid #cccccc;
|
|
border-radius: 6px;
|
|
padding: 8px;
|
|
background-color: white;
|
|
}
|
|
QMessageBox {
|
|
background-color: #f5f5f7;
|
|
}
|
|
QMessageBox QPushButton {
|
|
min-width: 80px;
|
|
padding: 5px 15px;
|
|
}
|
|
""")
|
|
|
|
try:
|
|
# 创建主窗口部件
|
|
central_widget = QWidget()
|
|
self.setCentralWidget(central_widget)
|
|
layout = QVBoxLayout(central_widget)
|
|
layout.setSpacing(20)
|
|
layout.setContentsMargins(30, 30, 30, 30)
|
|
|
|
# 设备ID显示区域
|
|
device_frame = QFrame()
|
|
device_frame.setStyleSheet("""
|
|
QFrame {
|
|
background-color: white;
|
|
border-radius: 10px;
|
|
padding: 10px;
|
|
}
|
|
""")
|
|
device_layout = QHBoxLayout(device_frame)
|
|
device_layout.setContentsMargins(10, 5, 10, 5)
|
|
|
|
device_id_label = QLabel("设备标识:")
|
|
self.device_id_text = QLineEdit()
|
|
self.device_id_text.setReadOnly(True)
|
|
|
|
try:
|
|
hardware_id = self.updater.hardware_id
|
|
logging.info(f"获取到硬件ID: {hardware_id}")
|
|
self.device_id_text.setText(hardware_id)
|
|
except Exception as e:
|
|
logging.error(f"获取硬件ID失败: {str(e)}")
|
|
self.device_id_text.setText("获取失败")
|
|
|
|
copy_button = QPushButton("复制ID")
|
|
copy_button.setMaximumWidth(80)
|
|
copy_button.clicked.connect(self.copy_device_id)
|
|
|
|
device_layout.addWidget(device_id_label)
|
|
device_layout.addWidget(self.device_id_text)
|
|
device_layout.addWidget(copy_button)
|
|
|
|
layout.addWidget(device_frame)
|
|
|
|
# 会员状态区域
|
|
member_frame = QFrame()
|
|
member_frame.setStyleSheet("""
|
|
QFrame {
|
|
background-color: white;
|
|
border-radius: 10px;
|
|
padding: 20px;
|
|
}
|
|
""")
|
|
member_layout = QVBoxLayout(member_frame)
|
|
member_layout.setContentsMargins(15, 10, 15, 10)
|
|
member_layout.setSpacing(8)
|
|
|
|
# 会员信息显示区域
|
|
self.member_info = QTextEdit()
|
|
self.member_info.setReadOnly(True)
|
|
self.member_info.setFixedHeight(100) # 增加高度
|
|
self.member_info.setStyleSheet("""
|
|
QTextEdit {
|
|
border: none;
|
|
background-color: transparent;
|
|
font-family: Consolas, Monaco, monospace;
|
|
font-size: 14px;
|
|
line-height: 1.6;
|
|
}
|
|
""")
|
|
|
|
# 设置默认会员信息(简化版)
|
|
self.member_info.setText("会员状态: 正常\n到期时间: 2025-02-22 20:44:23")
|
|
|
|
member_layout.addWidget(self.member_info)
|
|
layout.addWidget(member_frame)
|
|
|
|
# 激活区域
|
|
activate_frame = QFrame()
|
|
activate_frame.setStyleSheet("""
|
|
QFrame {
|
|
background-color: white;
|
|
border-radius: 10px;
|
|
padding: 10px;
|
|
}
|
|
""")
|
|
activate_layout = QVBoxLayout(activate_frame)
|
|
activate_layout.setContentsMargins(10, 5, 10, 5)
|
|
activate_layout.setSpacing(5)
|
|
|
|
# 激活标题和说明
|
|
activate_title = QLabel("激活(益加)会员,多个激活码可益加整体时长")
|
|
activate_title.setStyleSheet("""
|
|
QLabel {
|
|
color: #333333;
|
|
}
|
|
""")
|
|
|
|
# 激活码输入区域
|
|
input_layout = QHBoxLayout()
|
|
input_layout.setSpacing(10)
|
|
self.activate_input = QLineEdit()
|
|
self.activate_input.setPlaceholderText("请输入激活码")
|
|
|
|
activate_button = QPushButton("激活")
|
|
activate_button.setMaximumWidth(80)
|
|
activate_button.clicked.connect(self.activate_license)
|
|
|
|
input_layout.addWidget(self.activate_input)
|
|
input_layout.addWidget(activate_button)
|
|
|
|
activate_layout.addWidget(activate_title)
|
|
activate_layout.addLayout(input_layout)
|
|
|
|
layout.addWidget(activate_frame)
|
|
|
|
# 功能按钮区域
|
|
button_frame = QFrame()
|
|
button_frame.setStyleSheet("""
|
|
QFrame {
|
|
background-color: white;
|
|
border-radius: 10px;
|
|
padding: 15px;
|
|
}
|
|
""")
|
|
button_layout = QVBoxLayout(button_frame)
|
|
button_layout.setSpacing(10)
|
|
|
|
# 第一行按钮
|
|
self.update_button = QPushButton("刷新Cursor编辑器授权")
|
|
self.update_button.clicked.connect(self.start_update)
|
|
|
|
# 第二行按钮
|
|
self.reset_button = QPushButton("实现Cursor0.45.+限制")
|
|
self.reset_button.clicked.connect(self.reset_machine)
|
|
|
|
# 第三行按钮
|
|
self.disable_update_button = QPushButton("禁用Cursor版本更新")
|
|
self.disable_update_button.clicked.connect(self.disable_cursor_update)
|
|
|
|
# 设置按钮样式
|
|
for button in [self.update_button, self.reset_button, self.disable_update_button]:
|
|
button.setStyleSheet("""
|
|
QPushButton {
|
|
background-color: #0066cc;
|
|
color: white;
|
|
border: none;
|
|
border-radius: 4px;
|
|
padding: 12px;
|
|
font-size: 14px;
|
|
}
|
|
QPushButton:hover {
|
|
background-color: #0052a3;
|
|
}
|
|
QPushButton:pressed {
|
|
background-color: #003d7a;
|
|
}
|
|
""")
|
|
|
|
button_layout.addWidget(self.update_button)
|
|
button_layout.addWidget(self.reset_button)
|
|
button_layout.addWidget(self.disable_update_button)
|
|
|
|
layout.addWidget(button_frame)
|
|
|
|
# 初始化工作线程
|
|
self.worker = None
|
|
|
|
logging.info("界面元素初始化完成")
|
|
|
|
except Exception as e:
|
|
logging.error(f"初始化界面元素时发生错误: {str(e)}")
|
|
raise
|
|
|
|
def copy_device_id(self):
|
|
"""复制设备ID到剪贴板"""
|
|
try:
|
|
clipboard = QApplication.clipboard()
|
|
clipboard.setText(self.device_id_text.text())
|
|
QMessageBox.information(self, "提示", "设备ID已复制到剪贴板")
|
|
logging.info("设备ID已复制到剪贴板")
|
|
except Exception as e:
|
|
logging.error(f"复制设备ID时发生错误: {str(e)}")
|
|
QMessageBox.warning(self, "错误", f"复制失败: {str(e)}")
|
|
|
|
def append_log(self, text):
|
|
"""添加日志到状态显示区域"""
|
|
try:
|
|
self.status_text.append(text)
|
|
self.status_text.verticalScrollBar().setValue(
|
|
self.status_text.verticalScrollBar().maximum()
|
|
)
|
|
logging.info(f"状态更新: {text}")
|
|
except Exception as e:
|
|
logging.error(f"更新状态显示时发生错误: {str(e)}")
|
|
|
|
def start_update(self):
|
|
"""开始更新流程"""
|
|
try:
|
|
self.update_button.setEnabled(False)
|
|
self.reset_button.setEnabled(False)
|
|
self.status_text.clear()
|
|
self.append_log("开始更新流程...")
|
|
|
|
self.worker = UpdateWorker(self.updater)
|
|
self.worker.finished.connect(self.update_finished)
|
|
self.worker.progress.connect(self.append_log)
|
|
self.worker.start()
|
|
|
|
logging.info("更新进程已启动")
|
|
except Exception as e:
|
|
logging.error(f"启动更新进程时发生错误: {str(e)}")
|
|
self.update_button.setEnabled(True)
|
|
self.reset_button.setEnabled(True)
|
|
QMessageBox.warning(self, "错误", f"启动更新失败: {str(e)}")
|
|
|
|
def update_finished(self, success, message):
|
|
"""更新完成的回调"""
|
|
try:
|
|
self.update_button.setEnabled(True)
|
|
self.reset_button.setEnabled(True)
|
|
|
|
if success:
|
|
QMessageBox.information(self, "成功", message)
|
|
else:
|
|
# 检查是否是未激活错误
|
|
if "设备未激活" in message:
|
|
QMessageBox.warning(self, "设备未激活",
|
|
"请先激活设备后再进行更新。\n"
|
|
"您可以:\n"
|
|
"1. 输入激活码进行激活\n"
|
|
"2. 联系客服获取激活码")
|
|
else:
|
|
QMessageBox.warning(self, "失败", message)
|
|
|
|
self.append_log(message)
|
|
logging.info(f"更新完成: {message}")
|
|
except Exception as e:
|
|
logging.error(f"处理更新完成回调时发生错误: {str(e)}")
|
|
|
|
def reset_machine(self):
|
|
"""重置机器码"""
|
|
try:
|
|
reply = QMessageBox.question(
|
|
self,
|
|
"确认",
|
|
"确定要重置机器码吗?",
|
|
QMessageBox.Yes | QMessageBox.No,
|
|
QMessageBox.No
|
|
)
|
|
|
|
if reply == QMessageBox.Yes:
|
|
logging.info("开始重置机器码")
|
|
success = self.updater.reset_machine_id()
|
|
if success:
|
|
QMessageBox.information(self, "成功", "机器码重置成功")
|
|
self.device_id_text.setText(self.updater.hardware_id)
|
|
logging.info("机器码重置成功")
|
|
else:
|
|
QMessageBox.warning(self, "失败", "机器码重置失败")
|
|
logging.error("机器码重置失败")
|
|
except Exception as e:
|
|
logging.error(f"重置机器码时发生错误: {str(e)}")
|
|
QMessageBox.warning(self, "错误", f"重置失败: {str(e)}")
|
|
|
|
def activate_license(self):
|
|
"""激活许可证"""
|
|
try:
|
|
activation_code = self.activate_input.text().strip()
|
|
if not activation_code:
|
|
QMessageBox.warning(self, "提示", "请输入激活码")
|
|
return
|
|
|
|
# 禁用激活按钮,防止重复点击
|
|
self.activate_input.setEnabled(False)
|
|
self.update_button.setEnabled(False)
|
|
|
|
# 显示处理中的提示
|
|
QApplication.processEvents()
|
|
|
|
# 调用激活接口
|
|
success, message, account_info = self.updater.check_activation_code(activation_code)
|
|
|
|
if success:
|
|
# 更新界面显示
|
|
self.member_info.clear()
|
|
self.member_info.append(f"会员状态: 已激活")
|
|
self.member_info.append(f"到期时间: {account_info['expire_time']}")
|
|
self.member_info.append(f"剩余天数: {account_info['days_left']}天")
|
|
|
|
# 显示成功消息
|
|
QMessageBox.information(self, "激活成功",
|
|
f"设备已成功激活!\n"
|
|
f"到期时间: {account_info['expire_time']}\n"
|
|
f"剩余天数: {account_info['days_left']}天")
|
|
|
|
# 清空激活码输入框
|
|
self.activate_input.clear()
|
|
|
|
logging.info(f"设备激活成功,到期时间: {account_info['expire_time']}")
|
|
else:
|
|
QMessageBox.warning(self, "激活失败", message)
|
|
logging.error(f"激活失败: {message}")
|
|
|
|
except Exception as e:
|
|
logging.error(f"激活过程中发生错误: {str(e)}")
|
|
QMessageBox.critical(self, "错误", f"激活过程发生错误: {str(e)}")
|
|
finally:
|
|
# 恢复按钮状态
|
|
self.activate_input.setEnabled(True)
|
|
self.update_button.setEnabled(True)
|
|
|
|
def disable_cursor_update(self):
|
|
"""禁用Cursor版本更新"""
|
|
try:
|
|
# TODO: 实现禁用更新逻辑
|
|
logging.info("正在禁用Cursor版本更新...")
|
|
QMessageBox.information(self, "提示", "禁用更新功能即将实现")
|
|
except Exception as e:
|
|
logging.error(f"禁用更新时发生错误: {str(e)}")
|
|
QMessageBox.warning(self, "错误", f"禁用失败: {str(e)}")
|
|
|
|
def main():
|
|
try:
|
|
logging.info("程序启动...")
|
|
app = QApplication(sys.argv)
|
|
|
|
# 设置应用程序图标
|
|
if getattr(sys, 'frozen', False):
|
|
# 如果是打包后的应用
|
|
application_path = sys._MEIPASS
|
|
logging.info(f"运行于打包环境: {application_path}")
|
|
else:
|
|
# 如果是开发环境
|
|
application_path = os.path.dirname(os.path.abspath(__file__))
|
|
logging.info(f"运行于开发环境: {application_path}")
|
|
|
|
# 创建并显示主窗口
|
|
window = MainWindow()
|
|
window.show()
|
|
logging.info("主窗口已显示")
|
|
|
|
sys.exit(app.exec_())
|
|
except Exception as e:
|
|
logging.error(f"程序运行时发生错误: {str(e)}")
|
|
QMessageBox.critical(None, "错误", f"程序启动失败: {str(e)}")
|
|
sys.exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
main() |