jiateng_ws/apis/tary_api.py

86 lines
3.1 KiB
Python
Raw Permalink 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 utils.api_utils import ApiUtils
import logging
class TaryApi:
def __init__(self):
"""初始化托盘API工具类"""
self.api_utils = ApiUtils()
def get_tary_info(self, tary_code):
"""
获取托盘信息
Args:
tary_code: 托盘编号
Returns:
dict: 托盘信息
"""
try:
# API 配置中的键名
api_key = "get_tray_info"
# 将托盘号作为参数传递
response = self.api_utils.get(api_key, params={"tp_note": tary_code})
# 记录API响应
logging.info(f"托盘API响应: {response}")
# 请求失败时返回空数据
if not response.get("status", False):
return {
"success": False,
"message": "未找到托盘信息",
"data": None
}
# 成功时格式化数据
if response.get("data"):
# 记录data的类型
logging.info(f"数据类型: {type(response['data'])}, 数据内容: {response['data']}")
# 如果data直接是对象则使用该对象
if isinstance(response["data"], dict):
logging.info("处理data为字典的情况")
tray_info = response["data"]
# 如果data是数组并且有元素则使用第一个元素
elif isinstance(response["data"], list) and len(response["data"]) > 0:
logging.info("处理data为数组的情况")
tray_info = response["data"][0]
else:
logging.warning(f"数据格式不支持: {response['data']}")
return {
"success": False,
"message": "托盘数据格式不正确",
"data": None
}
# 构建返回数据
formatted_data = {
"tp_note": tray_info.get("tp_note", ""), # 托盘号
"product_name": tray_info.get("zx_name", ""), # 产品名称
"axis_type": tray_info.get("zx", ""), # 轴型
"tier": str(tray_info.get("cs", "")), # 托盘料
"size": str(tray_info.get("cc", "")), # 尺寸
}
return {
"success": True,
"message": "获取托盘信息成功",
"data": formatted_data
}
# 数据为空
return {
"success": False,
"message": "未找到托盘信息",
"data": None
}
except Exception as e:
logging.error(f"获取托盘信息异常: {str(e)}")
return {
"success": False,
"message": f"获取托盘信息异常: {str(e)}",
"data": None
}