jiateng_ws/widgets/login_widget.py

100 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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:
# 始终使用SQLite数据源验证登录
db = SQLUtils(source_name='sqlite')
db.execute_query("SELECT id FROM wsbz_user WHERE username = ? AND password = ? AND is_deleted = 0", (user_id, password))
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:
# 始终使用SQLite数据源获取用户信息
db = SQLUtils(source_name='sqlite')
db.execute_query("SELECT username, 'Default Corp', 1, 1 FROM user WHERE username = ?", (user_id,))
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:
user_name, corp_name, corp_id, position_id = get_user_info(user_id)
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}")
self.main_window = MainWindow(user_id, user_name, corp_name, corp_id, position_id)
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, "登录失败", "工号或密码错误!")