You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
9.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import psycopg2
from login import *
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
from interface import *
from activemanager import ActivityManager # 导入活动管理类
import config
from activearrangement import Active_arrangement
from store import Store
NONE = None
class LoginWindow(QMainWindow):
def __init__(self):
super().__init__() # 调用父类的初始化方法
self.ui = Ui_LoginWindow() # 创建一个 UI 界面的实例
self.ui.setupUi(self) # 设置 UI 界面
# 去掉窗口边框
self.setWindowFlag(QtCore.Qt.FramelessWindowHint) # 设置窗口为无边框模式
self.setAttribute(QtCore.Qt.WA_TranslucentBackground) # 设置窗口背景为透明
# 创建阴影效果
self.shadow = QtWidgets.QGraphicsDropShadowEffect(self) # 创建一个阴影效果对象
self.shadow.setOffset(0, 0) # 设置阴影偏移量为 (0, 0)
self.shadow.setBlurRadius(10) # 设置阴影模糊半径为 10 像素
self.shadow.setColor(QtCore.Qt.black) # 设置阴影颜色为黑色
self.ui.frame.setGraphicsEffect(self.shadow) # 将阴影效果应用到指定的框架 (frame)
# 设置样式表,去掉边框并设置透明背景
self.setStyleSheet("background-color: rgba(255, 255, 255, 0); border: none;") # 设置窗口背景为完全透明,并去掉边框
# 强制窗口固定大小
# self.setFixedSize(self.size()) # (注释掉的代码)如果需要,可以取消注释以固定窗口大小为当前大小
self.ui.pushButton_Login.clicked.connect(lambda: self.ui.stackedWidget_2.setCurrentIndex(0))
self.ui.pushButton_Register.clicked.connect(lambda: self.ui.stackedWidget_2.setCurrentIndex(1))
self.ui.PushButtom_L_sure.clicked.connect(self.login)
self.ui.QPushButtom_R_sure.clicked.connect(self.register)
self.show() # 显窗口
def register(self):
account = self.ui.LineEdit_R_account.text()
password = self.ui.LineEdit_R_password.text()
password1 = self.ui.LineEdit_R_password1.text()
global user_now
if len(account) == 0 or len(password ) == 0 or len(password1) == 0:
self.ui.stackedWidget.setCurrentIndex(3)
elif password1 != password:
self.ui.stackedWidget.setCurrentIndex(1)
else:
conn = psycopg2.connect(database="DataMY", user="postgres", password="822520", host="127.0.0.1",
port="5432")
cur = conn.cursor()
# 查询现有账户
cur.execute("SELECT accounts FROM users")
existing_accounts = cur.fetchall() # 获取所有账户
account_list = [row[0] for row in existing_accounts] # 提取账户名列表
if account in account_list:
self.ui.stackedWidget.setCurrentIndex(6) # 账户已存在
else:
# 插入新账户并获取新用户的userid
cur.execute("INSERT INTO users (accounts, passwords) VALUES (%s, %s) RETURNING userid",
(account, password))
new_user_id = cur.fetchone()[0] # 获取新插入的用户ID
# 插入用户积分,假设初始积分为 0
initial_points = 0
cur.execute("INSERT INTO user_points (user_id, total_points) VALUES (%s, %s)",
(new_user_id, initial_points))
conn.commit() # 提交更改
self.ui.stackedWidget.setCurrentIndex(2) # 注册成功页面
def login(self):
# 获取用户输入的账户和密码
account = self.ui.LineEdit_L_account.text()
password = self.ui.LineEdit_L_password.text()
# 检查账户或密码是否为空
if len(account) == 0 or len(password) == 0:
self.ui.stackedWidget.setCurrentIndex(3) # 显示错误消息,提示账户或密码不能为空
return
try:
# 连接数据库
conn = psycopg2.connect(database="DataMY", user="postgres", password="822520", host="127.0.0.1",
port="5432")
cur = conn.cursor()
# 查询用户账户和密码
cur.execute("SELECT userid, accounts, passwords FROM users WHERE accounts = %s", (account,))
row = cur.fetchone()
if row:
user_id, db_account, db_password = row
# 验证账户和密码是否匹配
if account == db_account and password == db_password:
config.user_now = user_id # 将用户ID存储在全局变量中
self.win = MainWindow() # 打开主窗口
self.win.show() # 显示主窗口
self.close() # 关闭登录窗口
else:
self.ui.stackedWidget.setCurrentIndex(7) # 显示错误消息,提示账户或密码错误
else:
self.ui.stackedWidget.setCurrentIndex(7) # 显示错误消息,提示账户不存在
conn.commit()
except Exception as e:
print(f"Error logging in: {e}")
finally:
# 关闭数据库连接
if conn:
conn.close()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow() # 假设 Ui_MainWindow 已经定义
self.ui.setupUi(self)
# 设置窗口属性
self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.setStyleSheet("background-color: rgba(255, 255, 255, 0); border: none;")
# 创建活动管理实例
self.activity_manager = ActivityManager(self.ui)
self.activity_arrangement = Active_arrangement(self.ui)
self.Store = Store(self.ui)
# 初始化拖动相关变量
self.mouse_press_pos = None
# 连接信号和槽
self.setup_signals()
self.activity_manager.load_data() # 使用活动管理类加载数据
self.Store.setup_ui()
self.Store.load_data()
self.Store.load_user_info()
self.show() # 显示窗口
def setup_signals(self):
self.ui.pushButton_active_manage.clicked.connect(lambda: self.ui.stackedWidget.setCurrentIndex(0))
self.ui.pushButton_active_arrange.clicked.connect(lambda: self.ui.stackedWidget.setCurrentIndex(1))
self.ui.pushButton_home.clicked.connect(lambda: self.ui.stackedWidget.setCurrentIndex(2))
self.ui.pushButton_profile.clicked.connect(lambda: self.ui.stackedWidget.setCurrentIndex(4))
self.ui.pushButton_store.clicked.connect(lambda: (self.ui.stackedWidget.setCurrentIndex(3), self.Store.load_user_info()))
self.ui.pushButton_logout.clicked.connect(self.logout)
self.ui.pushButton_m_sure.clicked.connect(self.change_password)
self.ui.pushButton_creat_active.clicked.connect(self.activity_manager.create_activity) # 更新为活动管理方法
self.ui.pushButton_correct_active.clicked.connect(self.activity_manager.update_activity) # 更新为活动管理方法
self.ui.pushButton_delete_active.clicked.connect(
self.activity_manager.delete_activity) # 传递活动ID
self.ui.pushButton_baoming.clicked.connect(self.activity_manager.register_activity)
self.ui.active_table.cellClicked.connect(self.activity_manager.handle_double_click) # 连接双击事件
self.ui.pushButton_4.clicked.connect(self.activity_manager.search)
self.ui.pushButton_5.clicked.connect(self.activity_manager.view_registered_activities)
self.ui.pushButton_6.clicked.connect(self.activity_manager.load_data)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton and self.ui.frame_4.geometry().contains(event.pos()):
self.mouse_press_pos = event.globalPos() - self.pos()
event.accept()
def mouseMoveEvent(self, event):
if self.mouse_press_pos is not None:
self.move(event.globalPos() - self.mouse_press_pos)
event.accept()
def mouseReleaseEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.mouse_press_pos = NONE
event.accept()
def logout(self):
global user_now
self.close()
self.login = LoginWindow() # 假设 LoginWindow 已经定义
user_now = ''
def change_password(self):
global user_now
password = self.ui.lineEdit_password.text()
confirm_password = self.ui.lineEdit_password2.text()
if not password or not confirm_password:
self.ui.stackedWidget_2.setCurrentIndex(1) # 显示错误消息
return
if password == confirm_password:
try:
with psycopg2.connect(database="DataMY", user="postgres", password="822520", host="127.0.0.1",
port="5432") as conn:
with conn.cursor() as cur:
cur.execute("UPDATE users SET passwords = %s WHERE accounts = %s", (password, user_now))
conn.commit()
self.ui.stackedWidget_2.setCurrentIndex(2) # 显示成功消息
except Exception as e:
print(f"Error changing password: {e}")
self.ui.stackedWidget_2.setCurrentIndex(1)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = LoginWindow()
sys.exit(app.exec_())