2025-06-07 10:45:09 +08:00
|
|
|
|
from PySide6.QtWidgets import QMessageBox
|
|
|
|
|
|
from PySide6.QtCore import Qt
|
|
|
|
|
|
from ui.login_ui import LoginUI
|
|
|
|
|
|
from utils.version_manager import VersionManager
|
|
|
|
|
|
from widgets.main_window import MainWindow
|
|
|
|
|
|
from utils.sql_utils import SQLUtils
|
|
|
|
|
|
import logging
|
|
|
|
|
|
import threading
|
|
|
|
|
|
|
|
|
|
|
|
def check_user_login(user_id, password):
|
|
|
|
|
|
"""验证用户登录"""
|
|
|
|
|
|
try:
|
2025-06-13 17:14:03 +08:00
|
|
|
|
# 始终使用SQLite数据源验证登录
|
|
|
|
|
|
db = SQLUtils(source_name='sqlite')
|
2025-06-16 11:55:19 +08:00
|
|
|
|
db.execute_query("SELECT id FROM wsbz_user WHERE username = ? AND password = ? AND is_deleted = 0", (user_id, password))
|
2025-06-07 10:45:09 +08:00
|
|
|
|
result = db.fetchone()
|
|
|
|
|
|
db.close()
|
|
|
|
|
|
return result is not None
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logging.error(f"登录验证出错: {e}")
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_info(user_id):
|
|
|
|
|
|
"""获取用户信息"""
|
|
|
|
|
|
try:
|
2025-06-13 17:14:03 +08:00
|
|
|
|
# 始终使用SQLite数据源获取用户信息
|
|
|
|
|
|
db = SQLUtils(source_name='sqlite')
|
2025-06-18 15:48:13 +08:00
|
|
|
|
db.execute_query("SELECT username, corp_id as corp_name, corp_id FROM wsbz_user WHERE username = ?", (user_id,))
|
2025-06-07 10:45:09 +08:00
|
|
|
|
result = db.fetchone()
|
|
|
|
|
|
db.close()
|
|
|
|
|
|
if result:
|
|
|
|
|
|
return result
|
|
|
|
|
|
return user_id, "未知公司", 0, 0
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logging.error(f"获取用户信息出错: {e}")
|
|
|
|
|
|
return user_id, "未知公司", 0, 0
|
|
|
|
|
|
|
|
|
|
|
|
class LoginWidget(LoginUI):
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
|
|
super().__init__(parent)
|
|
|
|
|
|
self.btn_login.clicked.connect(self.handle_login)
|
|
|
|
|
|
self.btn_close.clicked.connect(self.close)
|
|
|
|
|
|
|
|
|
|
|
|
# 添加回车键支持
|
|
|
|
|
|
self.edit_user.returnPressed.connect(self.handle_login)
|
|
|
|
|
|
self.edit_pwd.returnPressed.connect(self.handle_login)
|
|
|
|
|
|
|
|
|
|
|
|
# 显示版本号
|
|
|
|
|
|
self.version_label.setText(f"版本: {VersionManager.get_current_version()}")
|
|
|
|
|
|
|
|
|
|
|
|
# 在单独的线程中检查更新,避免阻塞UI
|
|
|
|
|
|
# self.check_update_thread = threading.Thread(target=self.check_for_updates)
|
|
|
|
|
|
# self.check_update_thread.daemon = True
|
|
|
|
|
|
# self.check_update_thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
def check_for_updates(self):
|
|
|
|
|
|
"""在后台线程中检查更新"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 检查更新并提示用户
|
|
|
|
|
|
VersionManager.check_and_prompt_update(self)
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logging.error(f"检查更新时发生错误: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
# 登录按钮点击事件
|
|
|
|
|
|
def handle_login(self):
|
|
|
|
|
|
user_id = self.edit_user.text().strip()
|
|
|
|
|
|
password = self.edit_pwd.text().strip()
|
|
|
|
|
|
if not user_id or not password:
|
|
|
|
|
|
QMessageBox.warning(self, "提示", "请输入工号和密码!")
|
|
|
|
|
|
return
|
|
|
|
|
|
if check_user_login(user_id, password):
|
|
|
|
|
|
try:
|
2025-06-18 15:48:13 +08:00
|
|
|
|
user_name, corp_name, corp_id = get_user_info(user_id)
|
2025-06-07 10:45:09 +08:00
|
|
|
|
if not corp_name:
|
|
|
|
|
|
corp_name = "未知公司"
|
|
|
|
|
|
# 移除登录成功的弹框
|
|
|
|
|
|
# QMessageBox.information(self, "登录成功", f"欢迎 {user_name}!")
|
|
|
|
|
|
self.hide()
|
|
|
|
|
|
|
|
|
|
|
|
# 使用异常处理确保主窗口创建失败不会导致整个应用程序崩溃
|
|
|
|
|
|
try:
|
|
|
|
|
|
import logging
|
|
|
|
|
|
logging.info(f"正在创建主窗口,用户ID: {user_id}, 姓名: {user_name}, 公司: {corp_name}")
|
2025-06-18 15:48:13 +08:00
|
|
|
|
self.main_window = MainWindow(user_id, user_name, corp_name, corp_id)
|
2025-06-07 10:45:09 +08:00
|
|
|
|
self.main_window.showMaximized() # 窗口最大化显示
|
|
|
|
|
|
logging.info("主窗口已显示(最大化)")
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logging.critical(f"创建或显示主窗口时发生错误: {e}", exc_info=True)
|
|
|
|
|
|
QMessageBox.critical(self, "系统错误", f"打开主窗口时发生错误: {e}\n请联系管理员")
|
|
|
|
|
|
self.show() # 如果主窗口创建失败,重新显示登录窗口
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
import logging
|
|
|
|
|
|
logging.critical(f"处理登录信息时发生错误: {e}", exc_info=True)
|
|
|
|
|
|
QMessageBox.critical(self, "系统错误", f"处理登录信息时发生错误: {e}\n请联系管理员")
|
|
|
|
|
|
self.show() # 确保用户可以重新尝试登录
|
|
|
|
|
|
else:
|
|
|
|
|
|
QMessageBox.warning(self, "登录失败", "工号或密码错误!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|