189 lines
7.0 KiB
Python
189 lines
7.0 KiB
Python
import sys
|
|
import requests
|
|
import json
|
|
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
|
|
QLabel, QLineEdit, QPushButton, QTextEdit, QMessageBox, QFileDialog)
|
|
from PyQt5.QtCore import Qt
|
|
|
|
|
|
class TurbineAddWindow(QMainWindow):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.setWindowTitle("新增机组")
|
|
self.setGeometry(100, 100, 500, 500)
|
|
|
|
self.init_ui()
|
|
|
|
def init_ui(self):
|
|
# 主窗口部件
|
|
central_widget = QWidget()
|
|
self.setCentralWidget(central_widget)
|
|
layout = QVBoxLayout()
|
|
|
|
# 项目ID
|
|
project_id_layout = QHBoxLayout()
|
|
project_id_layout.addWidget(QLabel("项目ID*:"))
|
|
self.project_id_input = QLineEdit()
|
|
project_id_layout.addWidget(self.project_id_input)
|
|
layout.addLayout(project_id_layout)
|
|
|
|
# 机组名称
|
|
turbine_name_layout = QHBoxLayout()
|
|
turbine_name_layout.addWidget(QLabel("机组名称:"))
|
|
self.turbine_name_input = QLineEdit()
|
|
turbine_name_layout.addWidget(self.turbine_name_input)
|
|
layout.addLayout(turbine_name_layout)
|
|
|
|
# 机组编码
|
|
turbine_code_layout = QHBoxLayout()
|
|
turbine_code_layout.addWidget(QLabel("机组编码:"))
|
|
self.turbine_code_input = QLineEdit()
|
|
turbine_code_layout.addWidget(self.turbine_code_input)
|
|
layout.addLayout(turbine_code_layout)
|
|
|
|
# 机组描述
|
|
turbine_desc_layout = QVBoxLayout()
|
|
turbine_desc_layout.addWidget(QLabel("机组描述:"))
|
|
self.turbine_desc_input = QTextEdit()
|
|
self.turbine_desc_input.setMaximumHeight(80)
|
|
turbine_desc_layout.addWidget(self.turbine_desc_input)
|
|
layout.addLayout(turbine_desc_layout)
|
|
|
|
# 机组厂商
|
|
turbine_manufacturer_layout = QHBoxLayout()
|
|
turbine_manufacturer_layout.addWidget(QLabel("机组厂商:"))
|
|
self.turbine_manufacturer_input = QLineEdit()
|
|
turbine_manufacturer_layout.addWidget(self.turbine_manufacturer_input)
|
|
layout.addLayout(turbine_manufacturer_layout)
|
|
|
|
# 机组型号
|
|
turbine_model_layout = QHBoxLayout()
|
|
turbine_model_layout.addWidget(QLabel("机组型号:"))
|
|
self.turbine_model_input = QLineEdit()
|
|
turbine_model_layout.addWidget(self.turbine_model_input)
|
|
layout.addLayout(turbine_model_layout)
|
|
|
|
# 机组封面图
|
|
self.cover_image_path = ""
|
|
cover_image_layout = QHBoxLayout()
|
|
cover_image_layout.addWidget(QLabel("机组封面图:"))
|
|
self.cover_image_label = QLabel("未选择图片")
|
|
self.cover_image_label.setStyleSheet("color: #666; font-style: italic;")
|
|
cover_image_layout.addWidget(self.cover_image_label)
|
|
self.select_image_btn = QPushButton("选择图片")
|
|
self.select_image_btn.clicked.connect(self.select_image)
|
|
cover_image_layout.addWidget(self.select_image_btn)
|
|
layout.addLayout(cover_image_layout)
|
|
|
|
# 按钮区域
|
|
button_layout = QHBoxLayout()
|
|
button_layout.addStretch()
|
|
|
|
# 新增清空按钮
|
|
self.clear_btn = QPushButton("清空表单")
|
|
self.clear_btn.setFixedWidth(100)
|
|
self.clear_btn.clicked.connect(self.clear_form)
|
|
button_layout.addWidget(self.clear_btn)
|
|
|
|
# 提交按钮
|
|
self.submit_btn = QPushButton("提交")
|
|
self.submit_btn.setFixedWidth(100)
|
|
self.submit_btn.clicked.connect(self.submit_data)
|
|
button_layout.addWidget(self.submit_btn)
|
|
|
|
layout.addLayout(button_layout)
|
|
|
|
central_widget.setLayout(layout)
|
|
|
|
def select_image(self):
|
|
options = QFileDialog.Options()
|
|
file_path, _ = QFileDialog.getOpenFileName(
|
|
self, "选择机组封面图", "",
|
|
"图片文件 (*.jpg *.jpeg *.png *.gif);;所有文件 (*.*)",
|
|
options=options
|
|
)
|
|
|
|
if file_path:
|
|
self.cover_image_path = file_path
|
|
self.cover_image_label.setText(file_path.split('/')[-1])
|
|
self.cover_image_label.setStyleSheet("color: #333; font-style: normal;")
|
|
|
|
def validate_inputs(self):
|
|
if not self.project_id_input.text().strip():
|
|
QMessageBox.warning(self, "输入错误", "项目ID不能为空!")
|
|
return False
|
|
return True
|
|
|
|
def submit_data(self):
|
|
if not self.validate_inputs():
|
|
return
|
|
|
|
# 构造请求数据
|
|
turbine_data = {
|
|
"projectId": self.project_id_input.text().strip(),
|
|
"turbineName": self.turbine_name_input.text().strip(),
|
|
"turbineCode": self.turbine_code_input.text().strip(),
|
|
"turbineDesc": self.turbine_desc_input.toPlainText().strip(),
|
|
"turbineManufacturer": self.turbine_manufacturer_input.text().strip(),
|
|
"turbineModel": self.turbine_model_input.text().strip(),
|
|
"turbineCoverUrl": self.cover_image_path if self.cover_image_path else ""
|
|
}
|
|
|
|
# 确认对话框
|
|
reply = QMessageBox.question(
|
|
self, '确认提交',
|
|
'确定要提交这些信息吗?',
|
|
QMessageBox.Yes | QMessageBox.No, QMessageBox.No
|
|
)
|
|
|
|
if reply == QMessageBox.No:
|
|
return
|
|
|
|
# 提交数据
|
|
url = "http://pms.dtyx.net:9158/turbine"
|
|
headers = {
|
|
"Authorization": "null",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
try:
|
|
response = requests.post(url, headers=headers, data=json.dumps(turbine_data))
|
|
response.raise_for_status()
|
|
|
|
result = response.json()
|
|
QMessageBox.information(
|
|
self, "提交成功",
|
|
f"数据提交成功!\n\n响应结果:\n{json.dumps(result, indent=2)}"
|
|
)
|
|
|
|
except Exception as e:
|
|
QMessageBox.critical(
|
|
self, "提交失败",
|
|
f"数据提交失败!\n\n错误信息:\n{str(e)}"
|
|
)
|
|
|
|
def clear_form(self):
|
|
"""手动清空表单的方法"""
|
|
reply = QMessageBox.question(
|
|
self, '确认清空',
|
|
'确定要清空所有已填写的信息吗?',
|
|
QMessageBox.Yes | QMessageBox.No, QMessageBox.No
|
|
)
|
|
|
|
if reply == QMessageBox.Yes:
|
|
self.project_id_input.clear()
|
|
self.turbine_name_input.clear()
|
|
self.turbine_code_input.clear()
|
|
self.turbine_desc_input.clear()
|
|
self.turbine_manufacturer_input.clear()
|
|
self.turbine_model_input.clear()
|
|
self.cover_image_path = ""
|
|
self.cover_image_label.setText("未选择图片")
|
|
self.cover_image_label.setStyleSheet("color: #666; font-style: italic;")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app = QApplication(sys.argv)
|
|
window = TurbineAddWindow()
|
|
window.show()
|
|
sys.exit(app.exec_()) |