Python/pyqt5/CODE/VIP_WMS_pyAPI/main.py
2024-06-25 22:02:02 +08:00

339 lines
13 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 multiprocessing import Process,Queue,RLock
from UI.main.mainui import Ui_MainWindow as mainUI
from UI.items.itemsui import Ui_Form as itemsUI
from UI.history.historyui import Ui_Form as historyUI
from UI.create.createui import Ui_Dialog as createUI
from UI.login.loginui import Ui_Dialog as loginUI
from UI.delete.deleteui import Ui_Dialog as deleteUI
from UI.borrow.borrowui import Ui_Dialog as borrowUI
from UI.rereturn.returnui import Ui_Dialog as returnUI
from VIP_WMS_api import Vip_api
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
import configparser
import requests
import base64
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QVBoxLayout, QPushButton, QMessageBox
from datetime import datetime
import os
def base64_to_pixmap(base64_str):
# 解码Base64字符串为图像数据
image_data = base64.b64decode(base64_str)
# 创建Qt图像对象
image = QImage.fromData(image_data)
# 将Qt图像对象转换为QPixmap对象
pixmap = QPixmap.fromImage(image)
return pixmap
class LoginSuccessSignal(QtCore.QObject):
signal = QtCore.pyqtSignal()
class DeleteUI(QtWidgets.QDialog,deleteUI):
def __init__(self):
super().__init__()
self.setupUi(self)
self.vip_api = Vip_api() # 创建 Vip_api 类的实例
self.deleteUI=deleteUI()
self.setupcombobox()
self.comboBox_category.currentIndexChanged.connect(self.comboBox_category_changed)
def setupcombobox(self):
data = self.vip_api.get_all_data("Model_em")
len_item = len(data['item'])
for i in range(len_item):
item = data['item'][i]
print(item)
self.comboBox_item.addItem(item)
def comboBox_category_changed(self):
self.comboBox_item.clear()
data=None
category = self.comboBox_category.currentText()
if category == "電料類":
data = self.vip_api.get_all_data("Model_em")
if (category == "線材類"):
data = self.vip_api.get_all_data("Model_line")
if (category == "螺絲類"):
data = self.vip_api.get_all_data("Model_rose")
if (category == "相機類"):
data = self.vip_api.get_all_data("Model_camera")
len_item = len(data['item'])
for i in range(len_item):
item = data['item'][i]
print(item)
self.comboBox_item.addItem(item)
class LoginUI(QtWidgets.QDialog,loginUI):
def __init__(self):
super().__init__()
self.setupUi(self)
self.vip_api = Vip_api() # 创建 Vip_api 类的实例
self.createUI=createUI()
self.success_signal = LoginSuccessSignal()
self.btn_login.clicked.connect(self.login)
def clearlineEdit(self):
self.lineEdit_userid.setText("")
self.lineEdit_password.setText("")
def login(self):
username=self.lineEdit_userid.text()
password=self.lineEdit_password.text()
if self.vip_api.verify(username, password):
token = self.vip_api.token
self.save_token_to_ini(token)
print(token)
QMessageBox.information(self, '成功', '登入成功!')
self.success_signal.signal.emit() # 登录成功后发射信号
else:
QMessageBox.warning(self, '錯誤', '帳號或密碼錯誤!')
def save_token_to_ini(self, token):
config = configparser.ConfigParser()
config['TokenSection'] = {'token': token}
with open('config.ini', 'w') as configfile:
config.write(configfile)
class CreateUI(QtWidgets.QDialog,createUI):
def __init__(self):
super().__init__()
self.setupUi(self)
self.buttonBox.accepted.connect(self.create)
self.buttonBox.rejected.connect(self.cancel)
def create(self):
category=self.comboBox_category.currentText()
item=self.textEdit_item.toPlainText()
account=self.textEdit_account.toPlainText()
location=self.textEdit_location.toPlainText()
itemimage=self.textEdit_image.toPlainText()
if (category=="電料類"):
self.vip_api.createitem("Model_em",item,account,location,itemimage)
if (category=="線材類"):
self.vip_api.createitem("Model_line",item,account,location,itemimage)
if (category=="螺絲類"):
self.vip_api.createitem("Model_rose",item,account,location,itemimage)
if (category=="相機類"):
self.vip_api.createitem("Model_camera",item,account,location,itemimage)
def cancel(self):
print("no")
class ItemsUI(QtWidgets.QWidget, itemsUI):
def __init__(self):
super().__init__()
self.setupUi(self)
self.comboBox.currentIndexChanged.connect(self.comboBox_changed)
self.vip_api = Vip_api() # 创建 Vip_api 类的实例
self.item_init_()
def item_init_(self):
self.data = self.vip_api.get_all_data("Model_em")
self.item_show(self.data)
def comboBox_changed(self):
# print(self.comboBox.currentText())
if (self.comboBox.currentText()=="電料類"):
data=self.vip_api.get_all_data("Model_em")
if (self.comboBox.currentText()=="線材類"):
data=self.vip_api.get_all_data("Model_line")
if (self.comboBox.currentText() == "螺絲類"):
data=self.vip_api.get_all_data("Model_rose")
if (self.comboBox.currentText() == "相機類"):
data=self.vip_api.get_all_data("Model_camera")
self.item_show(data)
def item_show(self,data):
# print(data)
if data.empty or data is None:
self.tableWidget.clearContents()
self.tableWidget.setRowCount(0)
else:
len_item = len(data['item'])
self.tableWidget.setRowCount(len_item)
for i in range(len(data['item'])):
item = self.data['item'][i]
account = str(self.data['account'][i])
location = self.data['location'][i]
self.tableWidget.setItem(i, 0, QtWidgets.QTableWidgetItem(item))
self.tableWidget.setItem(i, 1, QtWidgets.QTableWidgetItem(account))
self.tableWidget.setItem(i, 2, QtWidgets.QTableWidgetItem(location))
button = QtWidgets.QPushButton('Show Image', self)
button.clicked.connect(lambda checked, idx=i: self.show_image(idx))
self.tableWidget.setCellWidget(i, 3, button)
def show_image(self, index):
image_path = self.data['itemimage'][index]
pixmap = base64_to_pixmap(image_path)
self.label.setPixmap(pixmap)
self.label.setScaledContents(True) # 图片自适应
class HistoryUI(QtWidgets.QWidget, historyUI):
def __init__(self):
super().__init__()
self.setupUi(self)
self.vip_api = Vip_api() # 创建 Vip_api 类的实例
self.history_show()
def history_show(self):
data = self.vip_api.get_all_data("Model_rent")
len_borrow_date = len(data['borrow_date'])
borrow_dates_as_datetime = [datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S') for date_str in data['borrow_date']]
sorted_dates = sorted(borrow_dates_as_datetime, reverse=True)
data['borrow_date'] = [date.strftime('%Y-%m-%d %H:%M:%S') for date in sorted_dates]
print(data['borrow_date'])
self.tableWidget.setRowCount(len_borrow_date)
for i in range(len_borrow_date):
self.tableWidget.resizeColumnsToContents()
borrow_date = data['borrow_date'][i]
return_date = data['return_date'][i]
borrow_man = data['borrow_man'][i]
borrow_category = data['borrow_category'][i]
borrow_item = data['borrow_item'][i]
borrow_account = data['borrow_account'][i]
# 在相应单元格插入数据
self.tableWidget.setItem(i, 0, QtWidgets.QTableWidgetItem(borrow_date))
self.tableWidget.setItem(i, 1, QtWidgets.QTableWidgetItem(return_date))
self.tableWidget.setItem(i, 2, QtWidgets.QTableWidgetItem(borrow_man))
self.tableWidget.setItem(i, 3, QtWidgets.QTableWidgetItem(borrow_category))
self.tableWidget.setItem(i, 4, QtWidgets.QTableWidgetItem(borrow_item))
self.tableWidget.setItem(i, 5, QtWidgets.QTableWidgetItem(str(borrow_account)))
class BorrowUI(QtWidgets.QDialog, borrowUI):
def __init__(self):
super().__init__()
self.vip_api = Vip_api() # 创建 Vip_api 类的实例
self.setupUi(self)
self.buttonBox.accepted.connect(self.create)
self.buttonBox.rejected.connect(self.cancel)
def create(self):
pass
def cancel(self):
print("no")
class ReturnUI(QtWidgets.QDialog, returnUI):
def __init__(self):
super().__init__()
self.vip_api = Vip_api() # 创建 Vip_api 类的实例
self.setupUi(self)
self.buttonBox.accepted.connect(self.create)
self.buttonBox.rejected.connect(self.cancel)
def create(self):
pass
def cancel(self):
print("no")
class MainWindow(QtWidgets.QMainWindow, mainUI):
def __init__(self):
super().__init__()
self.setupUi(self)
self.itemsUI= ItemsUI()
self.borrowUI=BorrowUI()
self.returnUI=ReturnUI()
self.createUI=CreateUI()
self.loginUI=LoginUI()
self.loginUI.success_signal.signal.connect(self.close_login_ui) # 连接登录成功的信号到关闭登录视窗的槽函数
self.deleteUI=DeleteUI()
self.vip_api=Vip_api()
# config = configparser.ConfigParser()
self.token=self.vip_api.get_token()
print(self.token)
self.stackedWidget.addWidget(self.itemsUI)
self.btn_items.clicked.connect(lambda: self.changeIndex(0))
self.btn_history.clicked.connect(lambda: self.changeIndex(1))
self.btn_delete.clicked.connect(self.open_delete_window)
self.btn_create.clicked.connect(self.open_create_window)
self.btn_login.clicked.connect(self.open_login_window)
self.init_ini_remove() #存在ini檔的token刪除
self.btn_login_flag=0
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_data)
self.timer.start(1000) # 1000 毫秒(即 1 秒)
self.op = QtWidgets.QGraphicsOpacityEffect() #創建畫布
self.op1 = QtWidgets.QGraphicsOpacityEffect() #創建畫布
self.op2 = QtWidgets.QGraphicsOpacityEffect() #創建畫布
self.op.setOpacity(0) #設定畫布透明
self.op1.setOpacity(0) #設定畫布透明
self.btn_create.setGraphicsEffect(self.op) #創建按鈕隱藏
self.btn_create.setEnabled(False)
self.btn_delete.setGraphicsEffect(self.op1) #刪除按鈕隱藏
self.btn_delete.setEnabled(False)
def init_ini_remove(self):
file_path = 'config.ini'
if os.path.exists(file_path):
os.remove(file_path)
print(f"File {file_path} deleted successfully.")
else:
print(f"File {file_path} does not exist.")
# config['TokenSection'] = {'token': self.token}
# with open('config.ini', 'w') as configfile:
# config.write(configfile)
def update_data(self):
config = configparser.ConfigParser()
config.read('config.ini')
if 'TokenSection' in config and 'token' in config['TokenSection']:
token = config['TokenSection']['token']
print(f"从配置文件中获取的 token 为: {token}")
self.op.setOpacity(1)
self.op1.setOpacity(1)
self.op2.setOpacity(0) # 設定畫布透明
self.btn_create.setGraphicsEffect(self.op)
self.btn_create.setEnabled(True)
self.btn_delete.setGraphicsEffect(self.op1)
self.btn_delete.setEnabled(True)
self.btn_login_flag=1
else:
print("配置文件中未找到 token")
self.op.setOpacity(0)
self.op1.setOpacity(0)
self.btn_create.setGraphicsEffect(self.op)
self.btn_create.setEnabled(False)
self.btn_delete.setGraphicsEffect(self.op1)
self.btn_delete.setEnabled(False)
self.btn_login_flag=0
if self.btn_login_flag==1: #目前為登入狀態
self.btn_login.setText("倉管登出")
self.btn_login.disconnect() # 断开之前的连接
self.btn_login.clicked.connect(self.init_ini_remove)
else:
self.btn_login.setText("倉管登入")
self.btn_login.disconnect() # 断开之前的连接
self.btn_login.clicked.connect(self.open_login_window)
def open_create_window(self):
self.createUI.show()
def open_login_window(self):
self.loginUI.clearlineEdit()
self.loginUI.show()
def close_login_ui(self):
self.loginUI.close()
def open_delete_window(self):
self.deleteUI.show()
def changeIndex(self,index):
print(index)
if index==1: #index==1時歷史紀錄頁
self.historyUI = HistoryUI()
self.stackedWidget.addWidget(self.historyUI)
self.stackedWidget.setCurrentIndex(index)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())