99 lines
3.4 KiB
Python
99 lines
3.4 KiB
Python
from db.pgsql import PostgreSQL
|
||
from db.system_config_dao import SystemConfigDAO
|
||
import logging
|
||
|
||
class LoginDAO:
|
||
"""用户登录数据访问对象"""
|
||
|
||
def __init__(self):
|
||
"""初始化数据访问对象"""
|
||
self.db = PostgreSQL()
|
||
|
||
def __del__(self):
|
||
"""析构函数,确保数据库连接关闭"""
|
||
if hasattr(self, 'db'):
|
||
# 如果连接还打开着,确保关闭
|
||
try:
|
||
self.db.disconnect()
|
||
except:
|
||
pass
|
||
|
||
def check_user_login(self, user_id: str, password: str) -> bool:
|
||
"""检查用户登录凭证
|
||
|
||
Args:
|
||
user_id: 用户ID
|
||
password: 用户密码
|
||
|
||
Returns:
|
||
bool: 登录是否成功
|
||
"""
|
||
try:
|
||
self.db.connect()
|
||
sql = "SELECT 1 FROM sys_user WHERE user_id=%s AND password=md5(%s)"
|
||
result = self.db.execute_query(sql, (user_id, password))
|
||
return bool(len(result) > 0)
|
||
except Exception as e:
|
||
logging.error(f"检查用户登录失败: {str(e)}")
|
||
return False
|
||
finally:
|
||
self.db.disconnect()
|
||
|
||
def get_user_info(self, user_id: str):
|
||
"""获取用户信息
|
||
|
||
Args:
|
||
user_id: 用户ID
|
||
|
||
Returns:
|
||
tuple: (用户名, 公司名, 公司ID, 职位ID) 或 (None, None, None, None)
|
||
"""
|
||
try:
|
||
self.db.connect()
|
||
sql = """
|
||
SELECT user_id, nick_name AS user_name, position_id,
|
||
f_getname('GETCORPEXP', '', '', org_id) AS corp_name,
|
||
org_id AS corp_id, default_org_id
|
||
FROM sys_user
|
||
WHERE user_id = %s
|
||
"""
|
||
result = self.db.execute_query(sql, (user_id,))
|
||
if result:
|
||
user_name = result[0]['user_name']
|
||
corp_name = result[0]['corp_name']
|
||
corp_id = result[0]['corp_id']
|
||
position_id = result[0]['position_id']
|
||
default_org_id = result[0]['default_org_id']
|
||
|
||
# 如果有默认账套ID,使用默认账套ID替代登录账套ID
|
||
if default_org_id:
|
||
corp_id = default_org_id
|
||
# 查询默认账套的名称
|
||
corp_name_sql = """
|
||
SELECT corp_exp
|
||
FROM sys_org
|
||
WHERE org_id = %s
|
||
"""
|
||
corp_result = self.db.execute_query(corp_name_sql, (corp_id,))
|
||
if corp_result:
|
||
corp_name = corp_result[0]['corp_exp']
|
||
|
||
# 将用户的当前账套ID保存到内存中
|
||
SystemConfigDAO.save_user_selected_corp(user_id, corp_id)
|
||
|
||
return user_name, corp_name, corp_id, position_id
|
||
return None, None, None, None
|
||
except Exception as e:
|
||
logging.error(f"获取用户信息失败: {str(e)}")
|
||
return None, None, None, None
|
||
finally:
|
||
self.db.disconnect()
|
||
|
||
# 为保持向后兼容,添加全局函数
|
||
def check_user_login(user_id: str, password: str) -> bool:
|
||
dao = LoginDAO()
|
||
return dao.check_user_login(user_id, password)
|
||
|
||
def get_user_info(user_id: str):
|
||
dao = LoginDAO()
|
||
return dao.get_user_info(user_id) |