This repository has been archived by the owner on Apr 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
193 lines (159 loc) · 7.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import ctypes
import json
import os
import sys
from PyQt5.QtCore import Qt, QCoreApplication, QThread
from PyQt5.QtWidgets import QMainWindow, QApplication
from win32com import client
import interface
from CampusNetwork import campusNetwork, LoginError, FileError, Operators, UserInfo
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
class LoginInfo:
def __init__(self, user_info: UserInfo, self_start: bool, save_passwd: bool, auto_login: bool):
self.user_info = user_info
self.self_start = self_start
self.save_passwd = save_passwd
self.auto_login = auto_login
class loginThread(QThread):
def __init__(self, login_info: LoginInfo):
super(loginThread, self).__init__()
self.login_info = login_info
def run(self):
login(self.login_info.user_info)
class loginWindow(QMainWindow, interface.Ui_Login):
def __init__(self, login_info: LoginInfo, parent=None):
super(loginWindow, self).__init__(parent)
self.setupUi(self)
self.login_info = login_info
if self.login_info.user_info.operator == Operators.CMCC:
self.CMCC.setChecked(Qt.Checked)
elif self.login_info.user_info.operator == Operators.ChinaNet:
self.ChinaNet.setChecked(Qt.Checked)
elif self.login_info.user_info.operator == Operators.School:
self.School.setChecked(Qt.Checked)
else:
raise LoginError
self.lineEdit_username.setText(self.login_info.user_info.username)
if self.login_info.self_start:
self.checkBox_selfstart.setCheckState(Qt.Checked)
if self.login_info.save_passwd:
self.checkBox_savepasswd.setCheckState(Qt.Checked)
self.lineEdit_passed.setText(self.login_info.user_info.passwd)
if self.login_info.auto_login:
self.checkBox_autologin.setCheckState(Qt.Checked)
if not is_admin():
self.checkBox_selfstart.setText('开机自启(请以管\n理员身份运行)')
self.checkBox_selfstart.setEnabled(False)
self.checkBox_selfstart.stateChanged.connect(self.self_start_changed)
self.checkBox_savepasswd.stateChanged.connect(self.save_passed_changed)
self.checkBox_autologin.stateChanged.connect(self.auto_login_changed)
self.CMCC.clicked.connect(lambda: self.chose_operator(Operators.CMCC))
self.ChinaNet.clicked.connect(lambda: self.chose_operator(Operators.ChinaNet))
self.School.clicked.connect(lambda: self.chose_operator(Operators.School))
self.pushButton_yes.clicked.connect(self.passwd_changed)
self.pushButton_yes.clicked.connect(self.username_changed)
self.pushButton_yes.clicked.connect(lambda: save_data(self.login_info))
self.login_thread = loginThread(self.login_info)
self.pushButton_yes.clicked.connect(self.login_thread.start)
self.pushButton_yes.clicked.connect(lambda: self.setVisible(False))
self.login_thread.finished.connect(QCoreApplication.instance().quit)
self.pushButton_cancle.clicked.connect(QCoreApplication.instance().quit)
def start_thread(self):
self.login_thread.start()
def auto_login_changed(self):
self.login_info.auto_login = not self.login_info.auto_login
def self_start_changed(self):
self.login_info.self_start = not self.login_info.self_start
if self.login_info.self_start:
try:
file_name = sys.argv[0]
link_name = r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\CampusNetworkAutoLogin.lnk'
shortcut = client.Dispatch("WScript.Shell").CreateShortCut(link_name)
shortcut.TargetPath = file_name
shortcut.save()
except Exception as e:
print(e.args)
else:
if os.path.exists(r'C:\ProgramData\Microsoft\Windows\Start '
r'Menu\Programs\StartUp\CampusNetworkAutoLogin.lnk'):
os.remove(r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\CampusNetworkAutoLogin.lnk')
def save_passed_changed(self):
self.login_info.save_passwd = not self.login_info.save_passwd
def passwd_changed(self):
self.login_info.user_info.passwd = self.lineEdit_passed.text()
def username_changed(self):
self.login_info.user_info.username = self.lineEdit_username.text()
def chose_operator(self, operator: Operators):
if operator == Operators.School:
self.login_info.user_info.operator = Operators.School
elif operator == Operators.CMCC:
self.login_info.user_info.operator = Operators.CMCC
elif operator == Operators.ChinaNet:
self.login_info.user_info.operator = Operators.ChinaNet
else:
raise LoginError
def save_data(login_info: LoginInfo):
user_data = {
'username': login_info.user_info.username,
'passwd': login_info.user_info.passwd,
'operator': str(login_info.user_info.operator.name),
'self_start': login_info.self_start,
'save_passwd': login_info.save_passwd,
'auto_login': login_info.auto_login
}
with open('./setting.json', 'w') as file:
json.dump(user_data, file, indent=4)
def login(user_info: UserInfo):
c = campusNetwork()
try:
if user_info.operator == Operators.ChinaNet:
c.login(user_info.username, user_info.passwd, Operators.ChinaNet)
elif user_info.operator == Operators.CMCC:
c.login(user_info.username, user_info.passwd, Operators.CMCC)
elif user_info.operator == Operators.School:
c.login(user_info.username, user_info.passwd, Operators.School)
else:
print('setting.json error')
c.showtoast()
except LoginError:
c.showtoast()
def showWindow(login_info: LoginInfo):
main_window = loginWindow(login_info)
main_window.setVisible(True)
main_window.show()
def str_to_operators(s: str):
if s == 'ChinaNet':
return Operators.ChinaNet
elif s == 'CMCC':
return Operators.CMCC
elif s == 'School':
return Operators.School
else:
raise FileError
if __name__ == '__main__':
app = QApplication(sys.argv)
if not os.path.exists('./setting.json'):
user = {'username': '', 'passwd': '', 'operator': '', 'self_start: ': False, 'save_passwd': False,
'auto_login': False}
with open('./setting.json', 'w') as f:
json.dump(user, f, indent=4)
showWindow(LoginInfo(UserInfo('', '', Operators.School), False, False, False))
else:
with open('./setting.json', 'r') as f:
user = json.load(f)
try:
if user['auto_login']:
login(UserInfo(user['username'], user['passwd'], str_to_operators(user['operator'])))
sys.exit(0)
else:
showWindow(LoginInfo(UserInfo(user['username'], user['passwd'], str_to_operators(user['operator'])),
user['self_start'], user['save_passwd'], False))
except LoginError:
pass
except FileError:
showWindow(LoginInfo(UserInfo('', '', Operators.School), False, False, False))
sys.exit(app.exec_())