Login.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import inspect
  2. import shutil
  3. import os
  4. import time
  5. from typing import Optional
  6. import bcrypt
  7. from PyQt5.QtCore import Qt, QUrl, QSize
  8. from PyQt5.QtGui import QIcon
  9. from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTreeWidget, QTreeWidgetItem, QMenu, QFileDialog, \
  10. QMessageBox, QInputDialog, QLineEdit, QPushButton, QHBoxLayout, QFormLayout, QToolBar
  11. from PyQt5.QtWebEngineWidgets import *
  12. import ftplib
  13. import sys
  14. from PostgreSQL import PostgreSQL
  15. from Redis import Redis
  16. import socket
  17. from qgis.utils import iface
  18. class Login(QWidget):
  19. def __init__(self, parent=None):
  20. super().__init__(parent)
  21. self.setupUi()
  22. def setupUi(self):
  23. self.setWindowTitle("用户登录")
  24. self.resize(250, 100)
  25. self.username_edit = QLineEdit()
  26. self.password_edit = QLineEdit()
  27. self.password_edit.setEchoMode(QLineEdit.Password)
  28. current_directory = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
  29. self.setWindowIcon(QIcon(os.path.join(current_directory, "login.png")))
  30. login_button = QPushButton("登录")
  31. login_button.clicked.connect(self.login)
  32. layout = QFormLayout()
  33. layout.addRow("用户名", self.username_edit)
  34. layout.addRow("密码", self.password_edit)
  35. layout.addRow(login_button)
  36. self.setLayout(layout)
  37. def checklogin(self):
  38. redis = Redis()
  39. s = redis.getUsername()
  40. redis.close()
  41. if s is not None:
  42. app.quit()
  43. def login(self):
  44. username = self.username_edit.text()
  45. password = self.password_edit.text()
  46. if username is not None and password is not None:
  47. pgconn = PostgreSQL(schema='base')
  48. rows = pgconn.getPasswordByUsername(username)
  49. pgconn.close()
  50. if len(rows) == 0:
  51. QMessageBox.critical(self, "提示信息", "用户名错误!")
  52. else:
  53. userpass = rows[0][0]
  54. checked = bcrypt.checkpw(password.encode('utf-8'), userpass.encode('utf-8'))
  55. print(checked)
  56. if checked:
  57. QMessageBox.information(self, "提示信息", "登录成功")
  58. # 存储用户名到Redis
  59. redis = Redis()
  60. redis.setUsername(username)
  61. s = redis.getUsername()
  62. print(s)
  63. redis.close()
  64. app.quit()
  65. else:
  66. QMessageBox.critical(self, "提示信息", "密码错误!")
  67. else:
  68. QMessageBox.critical(self, "提示信息", "请输入用户名、密码!")
  69. if __name__ == '__main__':
  70. app = QApplication(sys.argv)
  71. login = Login()
  72. login.show()
  73. # login.checklogin()
  74. sys.exit(app.exec_())
  75. # time.sleep(0.5)
  76. # redis = Redis()
  77. # s = redis.getUsername()
  78. # redis.close()
  79. # if s is not None:
  80. # app.quit()