import os
import shutil
from threading import Thread
import hashlib
from PyQt5.QtCore import pyqtSignal, QObject, Qt, QRegExp
from PyQt5.QtWidgets import QWidget, QApplication, QMessageBox, QMainWindow, QFileDialog, QLabel, QTableWidgetItem, \
QMenu
from PyQt5.uic import loadUi
from PyQt5.QtGui import QIcon, QPixmap, QRegExpValidator
import pymysql # 数据库控制
from resource import gas_predict
# 自定义信号源对象类型,一定要继承自 QObject
class MySignals(QObject):
# 定义一种信号,两个参数 类型分别是: QTextBrowser 和 字符串
# 调用 emit方法 发信号时,传入参数 必须是这里指定的 参数类型
text_print = pyqtSignal(str)
# 子线程给主线程发送聚类个数
type_return = pyqtSignal(int)
# 子线程给主线程发送进度条完成进度
progress_return = pyqtSignal(int)
progress_return2 = pyqtSignal(int)
# 每当预测文件图片生成,则向主线程发射信号,让主线程将图片显示出来
predict_return = pyqtSignal(str)
# 登录界面
class Login(QWidget):
def __init__(self):
super(Login, self).__init__()
# 加载ui界面
loadUi('resource/ui/login.ui', self)
# self.setupUi(self) # 设置写好的ui转py
# 设置背景图片
self.Background_png = QPixmap('resource/picture/background.png')
self.Background.setPixmap(self.Background_png)
# 图片自适应
self.Background.setScaledContents(True)
# 设置图标
self.gas_ico_png = QPixmap('resource/picture/燃气.png')
self.gas_ico.setPixmap(self.gas_ico_png)
self.gas_ico.setScaledContents(True)
# 登录按钮关联
self.btn_login.clicked.connect(self.click_login)
# 注册按钮关联
self.btn_register.clicked.connect(self.click_register)
# 设置输入框只能输入非中文字符
self.text_user.setValidator(QRegExpValidator(QRegExp("[^\u4e00-\u9fa5]{16}"), self))
self.text_password.setValidator(QRegExpValidator(QRegExp("[^\u4e00-\u9fa5]{16}"), self))
# 登录事件触发
def click_login(self):
# 获取账号和密码文本框内容
username = self.text_user.text().strip()
password = self.text_password.text().strip()
# 判断输入情况是否合法
if username == '' or password == '': # 账号或密码为空
QMessageBox.information(self,
"提示",
"账号或密码为空!")
else:
# 如果是管理员账户则登陆管理员账户
if username == 'root' and password == 'root':
staff.show()
login.close()
else:
# 创建md5对象
hl = hashlib.md5()
# Tips
# 此处必须声明encode
# 若写法为hl.update(str) 报错为: Unicode-objects must be encoded before hashing
# 对密码进行md5加密
hl.update(password.encode(encoding='utf-8'))
# 获取加密密码
password = hl.hexdigest()
# 打开数据库连接
db = pymysql.connect(host="localhost", user="root", password="root", database="userinfo")
cursor = db.cursor()
# 查询是否有该用户
sql = f"select * from user where username =\'{username}\'"
cursor.execute(sql)
result = cursor.fetchall()
# 判断数据库中是否存在当前用户
if len(result) == 0:
QMessageBox.information(self,
"提示",
"登录失败,系统无该用户,请先注册!")
else:
sql = f"select * from user where username =\'{username}\' and password = \'{password}\'"
cursor.execute(sql)
result = cursor.fetchall()
if len(result) == 0:
QMessageBox.information(self,
"提示",
'密码错误,请重新输入!')
else:
QMessageBox.information(self,
"提示",
'登录成功!')
cursor.close()
db.close()
# 登录成功后显示主界面
main.show()
self.close()
# 注册事件触发,进入注册界面
def click_register(self):
# 展示注册窗口
register.show()
# 隐藏登录窗口
self.hide()
# 注册界面
class Register(QWidget):
def __init__(self):
super(Register, self).__init__()
# 加载ui界面
loadUi('resource/ui/register.ui', self)
# 返回按钮关联
self.btn_return.clicked.connect(self.click_return)
# 注册按钮关联
self.btn_register.clicked.connect(self.click_register)
# 设置输入框只能输入非中文字符
self.text_user.setValidator(QRegExpValidator(QRegExp("[^\u4e00-\u9fa5]{16}"), self))
self.text_password.setValidator(QRegExpValidator(QRegExp("[^\u4e00-\u9fa5]{16}"), self))
self.text_password_is.setValidator(QRegExpValidator(QRegExp("[^\u4e00-\u9fa5]{16}"), self))
self.text_phone.setValidator(QRegExpValidator(QRegExp("^[0-9]+$"), self))
# 返回按钮触发
def click_return(self):
# 展示登录窗口
login.show()
# 隐藏注册窗口
self.close()
# 注册按钮触发
def click_register(self):
# 获取输入框信息
username = self.text_user.text().strip()
password = self.text_password.text().strip()
password_is = self.text_password_is.text().strip()
staff_name = self.text_name.text().strip()
phone = self.text_phone.text().strip()
# 当账号或密码或确认密码为空,则提示为空
if username == '' or password == '' or password_is == '' or staff_name == '' and phone == '':
QMessageBox.information(self,
"提示",
"输入框不得为空!")
# 当密码和确认密码不同,提示
elif password != password_is:
QMessageBox.information(self,
"提示",
"密码与确认密码不同!")
# 用户名和密码不得少于6位
elif len(username) < 6 or len(password) < 6:
QMessageBox.information(self,
"提示",
"账号和密码都不得少于6位!")
# 输入的数据都合法
else:
# 打开数据库连接
db = pymysql.connect(host="localhost", user="root", password="root", database="userinfo")
cursor = db.cursor()
sql = f"select username from user where username =\'{username}\'"
cursor.execute(sql)
# 抓取结果
result = cursor.fetchall()
# 当查询到数据库中存在该账号,提示已存在
if len(result) != 0:
QMessageBox.information(self,
"提示",
"账号已存在,请重新输入!")
# 如果没有该账号,则查询内部人员表,是否内部人员
else:
sql = f"select stname,phone from staff where stname = \'{staff_name}\' and phone = \'{phone}\'"
cursor.execute(sql