upload
Former-commit-id: 3af1a19b5b0e0bb4dbc367468b36c62aa8762a91
This commit is contained in:
187
utils.py
187
utils.py
@ -10,6 +10,11 @@ import matplotlib.pyplot as plt
|
||||
from matplotlib.animation import FuncAnimation
|
||||
import threading
|
||||
from collections import deque
|
||||
import requests
|
||||
import json
|
||||
import tkinter as tk
|
||||
from tkinter import messagebox
|
||||
import base64
|
||||
|
||||
@dataclass
|
||||
class HistoryRecord:
|
||||
@ -377,6 +382,188 @@ class State:
|
||||
return ", " + ", ".join(status) if status else ""
|
||||
|
||||
|
||||
def login_to_platform(username, password):
|
||||
"""登录到平台获取token"""
|
||||
try:
|
||||
wwwF = {'userName': username, 'password': password}
|
||||
url = 'https://jingsai.mools.net/api/login'
|
||||
|
||||
response = requests.post(url, wwwF,timeout=2)
|
||||
if response is None:
|
||||
print("错误",'网络连接失败 ')
|
||||
return None
|
||||
request = json.loads(response.text)
|
||||
if request['code'] == 1:
|
||||
# 登陆成功
|
||||
# print("登陆成功",'登陆成功')
|
||||
# 从服务器获取到的数据中找到token
|
||||
token = request['token']
|
||||
print("成功", "登录成功!")
|
||||
return token
|
||||
elif request['code'] == 2:
|
||||
print("错误",'用户名或密码错误!')
|
||||
return None
|
||||
else:
|
||||
print("错误",'登陆失败 ')
|
||||
return None
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print("错误", f"发送数据时出错:{e}")
|
||||
return None
|
||||
|
||||
def send_data_to_platform(token, data, picture):
|
||||
"""将数据发送到平台"""
|
||||
try:
|
||||
# if 1:
|
||||
# 打开图片文件并转换为 Base64 编码
|
||||
with open(picture, "rb") as picture_file:
|
||||
picture_data = picture_file.read()
|
||||
base64_encoded_picture = base64.b64encode(picture_data).decode("utf-8")
|
||||
|
||||
print(base64_encoded_picture)
|
||||
|
||||
# 更新数据字典,添加 Base64 编码的图片
|
||||
data["final_image"] = base64_encoded_picture
|
||||
# print(data)
|
||||
|
||||
# 设置请求头
|
||||
headers = {
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
url = "https://jingsai.mools.net/api/upload-record"
|
||||
# 合并 headers 和 data 为一个字典
|
||||
datas = {**headers, **data}
|
||||
# 准备 JSON 数据
|
||||
json_data = json.dumps(data)
|
||||
# 发送 POST 请求
|
||||
response = requests.post(url, headers=headers, data=json_data)
|
||||
request = json.loads(response.text)
|
||||
# print(request['code'])
|
||||
# 检查响应
|
||||
if request['code'] == 1:
|
||||
print("提交成功", "提交成功")
|
||||
|
||||
else:
|
||||
print("错误", f"网络请求失败,状态码:{response.status_code}\n错误信息:{response.text}")
|
||||
|
||||
except Exception as e:
|
||||
raise e
|
||||
print("错误", f"发送数据时出错:{e}")
|
||||
|
||||
|
||||
class LoginApp:
|
||||
def __init__(self, root):
|
||||
self.root = root
|
||||
self.root.title("Mlabs AI Titration 1.0")
|
||||
# 设置窗口图标
|
||||
self.set_window_icon()
|
||||
|
||||
# 定义变量
|
||||
self.username = tk.StringVar()
|
||||
self.password = tk.StringVar()
|
||||
self.token = ''
|
||||
|
||||
self.un = ''
|
||||
self.pw = ''
|
||||
|
||||
# 创建登录界面
|
||||
self.create_login_interface()
|
||||
|
||||
# 设置窗口居中
|
||||
self.center_window()
|
||||
|
||||
def center_window(self, width=300, height=120):
|
||||
"""将窗口居中"""
|
||||
# 获取屏幕宽度和高度
|
||||
screen_width = self.root.winfo_screenwidth()
|
||||
screen_height = self.root.winfo_screenheight()
|
||||
|
||||
# 计算窗口位置
|
||||
x = (screen_width - width) // 2
|
||||
y = (screen_height - height) // 2
|
||||
|
||||
# 设置窗口大小和位置
|
||||
self.root.geometry(f"{width}x{height}+{x}+{y}")
|
||||
|
||||
def set_window_icon(self):
|
||||
"""设置窗口图标"""
|
||||
try:
|
||||
# 替换为你的图标文件路径
|
||||
icon_path = "logo.ico" # Windows系统使用.ico文件
|
||||
self.root.iconbitmap(icon_path)
|
||||
except Exception as e:
|
||||
print(f"设置窗口图标时出错:{e}")
|
||||
|
||||
def create_login_interface(self):
|
||||
# 用户名
|
||||
tk.Label(self.root, text="用户名:").grid(row=0, column=0, padx=10, pady=5)
|
||||
self.username_entry = tk.Entry(self.root, textvariable=self.username, width=30)
|
||||
self.username_entry.grid(row=0, column=1, padx=10, pady=5)
|
||||
|
||||
# 密码
|
||||
tk.Label(self.root, text="密码:").grid(row=1, column=0, padx=10, pady=5)
|
||||
self.password_entry = tk.Entry(self.root, textvariable=self.password, show="*", width=30)
|
||||
self.password_entry.grid(row=1, column=1, padx=10, pady=5)
|
||||
|
||||
# 登录按钮
|
||||
login_button = tk.Button(self.root, text="登录", command=self.login)
|
||||
login_button.grid(row=3, column=0, columnspan=2, pady=10)
|
||||
|
||||
# 检查info.json文件是否存在
|
||||
self.check_info_file()
|
||||
self.username_entry.focus_set()
|
||||
|
||||
def check_info_file(self):
|
||||
login_folder = "login"
|
||||
info_file = os.path.join(login_folder, "info.json")
|
||||
|
||||
if os.path.exists(info_file):
|
||||
with open(info_file, "r", encoding="utf-8") as file:
|
||||
info = json.load(file)
|
||||
self.username.set(info.get("username", ""))
|
||||
self.password.set(info.get("password", ""))
|
||||
else:
|
||||
print("提示", "未找到本地登录信息,请手动输入登录信息。")
|
||||
|
||||
def login(self):
|
||||
self.un = self.username.get()
|
||||
self.pw = self.password.get()
|
||||
|
||||
if not self.un or not self.pw:
|
||||
print("错误", "用户名、密码不能为空!")
|
||||
return
|
||||
|
||||
# 这里可以添加登录逻辑,例如发送请求到服务器验证登录信息
|
||||
self.token = login_to_platform(self.un, self.pw)
|
||||
# print(self.token)
|
||||
# 这里可以添加登录逻辑,例如发送请求到服务器验证登录信息
|
||||
|
||||
self.save_info_file()
|
||||
self.root.destroy()
|
||||
# return username, password
|
||||
|
||||
|
||||
def save_info_file(self):
|
||||
login_folder = "login"
|
||||
#如果没有login文件夹,则创建一个
|
||||
if not os.path.exists(login_folder):
|
||||
os.makedirs(login_folder)
|
||||
info_file = os.path.join(login_folder, "info.json")
|
||||
|
||||
if not os.path.exists(login_folder):
|
||||
os.makedirs(login_folder)
|
||||
|
||||
info = {
|
||||
"username": self.username.get(),
|
||||
"password": self.password.get(),
|
||||
}
|
||||
|
||||
with open(info_file, "w", encoding="utf-8") as file:
|
||||
json.dump(info, file, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
def setup_logging(log_level=logging.INFO, log_dir="logs"):
|
||||
"""
|
||||
设置logging配置,创建不同模块的logger
|
||||
|
Reference in New Issue
Block a user