1+ # authorization.py
2+ import datetime
3+ import os
4+ import re
5+
6+ import pyodbc
7+ import pyperclip
8+ from PyQt5 .QtCore import Qt
9+ from PyQt5 .QtGui import QFont
10+ from PyQt5 .QtWidgets import (QApplication , QDialog , QVBoxLayout , QLabel , QPushButton , QTextEdit , QMessageBox )
11+ import clr
12+
13+ # 添加dll引用路径
14+ try :
15+ dll_path = os .path .join (os .path .dirname (os .path .abspath (__file__ )), "AuthorizeManage.dll" )
16+ if os .path .exists (dll_path ):
17+ clr .AddReference (dll_path )
18+ clr .AddReference ("mscorlib" )
19+ clr .AddReference ("AuthorizeManage" )
20+ from AuthorizeManage import AuthorizeX
21+ except Exception as e :
22+ print (f"加载授权DLL失败: { e } " )
23+ AuthorizeX = None
24+
25+
26+ class AuthorizationDialog (QDialog ):
27+ def __init__ (self , machine_code , parent = None ):
28+ super ().__init__ (parent )
29+ self .machine_code = machine_code
30+ self .setWindowTitle ("授權驗證" )
31+ self .setFixedSize (400 , 300 )
32+ self .setWindowFlags (Qt .Dialog | Qt .FramelessWindowHint )
33+ self .initUI ()
34+ self .center ()
35+
36+ def initUI (self ):
37+ layout = QVBoxLayout ()
38+ layout .setContentsMargins (20 , 20 , 20 , 20 )
39+ layout .setSpacing (15 )
40+
41+ # 标题
42+ title_label = QLabel ("授權驗證" )
43+ title_label .setFont (QFont ("微軟雅黑" , 14 , QFont .Bold ))
44+ title_label .setAlignment (Qt .AlignCenter )
45+ title_label .setStyleSheet ("color: #ff6b6b;" )
46+ layout .addWidget (title_label )
47+
48+ # 提示信息
49+ info_label = QLabel ("當前軟件未授權或授權已經過期" )
50+ info_label .setFont (QFont ("微軟雅黑" , 10 ))
51+ info_label .setAlignment (Qt .AlignCenter )
52+ layout .addWidget (info_label )
53+
54+ # 机器码显示
55+ machine_label = QLabel ("請複製以下機器碼聯繫管理員授權:" )
56+ machine_label .setFont (QFont ("微軟雅黑" , 9 ))
57+ layout .addWidget (machine_label )
58+
59+ self .code_text = QTextEdit ()
60+ self .code_text .setText (self .machine_code )
61+ self .code_text .setReadOnly (True )
62+ self .code_text .setFixedHeight (80 )
63+ self .code_text .setStyleSheet ("""
64+ QTextEdit {
65+ background-color: #f8f9fa;
66+ border: 1px solid #ddd;
67+ border-radius: 5px;
68+ padding: 10px;
69+ font-family: Consolas, monospace;
70+ }
71+ """ )
72+ layout .addWidget (self .code_text )
73+
74+ # 复制按钮
75+ copy_btn = QPushButton ("複製機器碼" )
76+ copy_btn .setFixedHeight (35 )
77+ copy_btn .clicked .connect (self .copy_code )
78+ copy_btn .setStyleSheet ("""
79+ QPushButton {
80+ background-color: #1890ff;
81+ color: white;
82+ border: none;
83+ border-radius: 5px;
84+ font-weight: bold;
85+ }
86+ QPushButton:hover {
87+ background-color: #40a9ff;
88+ }
89+ """ )
90+ layout .addWidget (copy_btn )
91+
92+ # 退出按钮
93+ exit_btn = QPushButton ("退出程序" )
94+ exit_btn .setFixedHeight (35 )
95+ exit_btn .clicked .connect (self .close_application )
96+ exit_btn .setStyleSheet ("""
97+ QPushButton {
98+ background-color: #ff4d4f;
99+ color: white;
100+ border: none;
101+ border-radius: 5px;
102+ font-weight: bold;
103+ }
104+ QPushButton:hover {
105+ background-color: #ff7875;
106+ }
107+ """ )
108+ layout .addWidget (exit_btn )
109+
110+ self .setLayout (layout )
111+
112+ def center (self ):
113+ frame_gm = self .frameGeometry ()
114+ screen = QApplication .desktop ().screenNumber (QApplication .desktop ().cursor ().pos ())
115+ center_point = QApplication .desktop ().screenGeometry (screen ).center ()
116+ frame_gm .moveCenter (center_point )
117+ self .move (frame_gm .topLeft ())
118+
119+ def copy_code (self ):
120+ # clipboard = QApplication.clipboard()
121+ # clipboard.setText(self.machine_code)
122+ pyperclip .copy (self .machine_code )
123+ QMessageBox .information (self , "提示" , "機器碼已複製到剪切板" )
124+
125+ def close_application (self ):
126+ QApplication .quit ()
127+
128+ def mousePressEvent (self , event ):
129+ if event .button () == Qt .LeftButton :
130+ self .dragPosition = event .globalPos () - self .frameGeometry ().topLeft ()
131+ event .accept ()
132+
133+ def mouseMoveEvent (self , event ):
134+ if event .buttons () == Qt .LeftButton :
135+ self .move (event .globalPos () - self .dragPosition )
136+ event .accept ()
137+
138+
139+ def check_authorization ():
140+ """检查授权状态"""
141+ if AuthorizeX is None :
142+ return False , "授权模块加载失败" , "未知"
143+
144+ try :
145+ # 获取机器码
146+ machine_code = AuthorizeX .GetAuthorize ("FBCJ" )
147+ print (f"机器码: { machine_code } " )
148+
149+ # 数据库连接字符串
150+ connection_string = (
151+ r"Driver={SQL Server};"
152+ r"Server=dbs.kydb.vip;"
153+ r"Database=DeviceAuthData;"
154+ r"UID=sa;"
155+ r"PWD=Yunsin@#861123823_shp4;"
156+ r"timeout=35;"
157+ )
158+
159+ # 连接数据库验证授权
160+ conn = pyodbc .connect (connection_string , timeout = 35 , pooling = True )
161+ cursor = conn .cursor ()
162+
163+ query = """
164+ SELECT PCCoded, installDate, ExpiryDate
165+ FROM UserData
166+ WHERE PCCoded = ? \
167+ """
168+ cursor .execute (query , (machine_code ,))
169+ results = cursor .fetchall ()
170+
171+ if results :
172+ for row in results :
173+ PCCoded = row [0 ]
174+ installDate = row [1 ]
175+ ExpiryDate = row [2 ]
176+
177+ current_date = datetime .datetime .now ().date ()
178+ installDate = installDate .date ()
179+ ExpiryDate = ExpiryDate .date ()
180+
181+ if installDate <= current_date <= ExpiryDate :
182+ days_remaining = (ExpiryDate - current_date ).days
183+ print (f"当前日期在有效期内。剩余天数: { days_remaining } " )
184+ return True , f"授權有效,剩餘{ days_remaining } 天" , machine_code
185+ else :
186+ print ("当前日期不在有效期内。" )
187+ return False , "授權已經過期" , machine_code
188+ else :
189+ print ("没有找到授权信息!" )
190+ return False , "未找到授權信息" , machine_code
191+
192+ except Exception as e :
193+ print (f"授权检查失败: { e } " )
194+ return False , f"授權檢測失敗: { str (e )} " , "獲取失敗"
195+
196+
197+ def show_authorization_dialog (machine_code ):
198+ """显示授权对话框"""
199+ app = QApplication .instance ()
200+ if not app :
201+ app = QApplication ([])
202+
203+ dialog = AuthorizationDialog (machine_code )
204+ dialog .exec_ ()
205+
206+
207+ def verify_and_run (version , day ):
208+ """验证授权并运行主程序"""
209+ is_authorized , message , machine_code = check_authorization ()
210+
211+ if is_authorized :
212+ print ("当前日期在有效期内。" )
213+ # 导入并运行主程序
214+ from FB_win import win_main
215+ day_match = re .search (r'\d+' , message )
216+ if day_match :
217+ win_main (version , day_match .group ())
218+ else :
219+ win_main (version , day )
220+ else :
221+ print (f"授权失败: { message } " )
222+ # 直接显示授权对话框,阻塞直到关闭
223+ app = QApplication .instance ()
224+ if not app :
225+ app = QApplication ([])
226+
227+ dialog = AuthorizationDialog (machine_code )
228+ dialog .exec_ () # 使用 exec_() 阻塞直到对话框关闭
229+
230+ # 对话框关闭后退出程序
231+ QApplication .quit ()
0 commit comments