Showing
1 changed file
with
47 additions
and
10 deletions
| ... | @@ -9,6 +9,27 @@ import json | ... | @@ -9,6 +9,27 @@ import json |
| 9 | from .api_config import API_CONFIG | 9 | from .api_config import API_CONFIG |
| 10 | 10 | ||
| 11 | 11 | ||
| 12 | +def _create_error_response(message: str, consignmentCode: str) -> str: | ||
| 13 | + """创建错误响应的辅助函数 | ||
| 14 | + | ||
| 15 | + Args: | ||
| 16 | + message: 错误消息 | ||
| 17 | + consignmentCode: 运单编号 | ||
| 18 | + | ||
| 19 | + Returns: | ||
| 20 | + JSON格式的错误响应字符串 | ||
| 21 | + """ | ||
| 22 | + error_data = { | ||
| 23 | + "message": message, | ||
| 24 | + "consignmentCode": consignmentCode, | ||
| 25 | + "waybill_id": "", | ||
| 26 | + "operate_unit_name": "", | ||
| 27 | + "operate_unit_code": "", | ||
| 28 | + "detailList": [] | ||
| 29 | + } | ||
| 30 | + return json.dumps(error_data, ensure_ascii=False, indent=2) | ||
| 31 | + | ||
| 32 | + | ||
| 12 | def create_waybill_d(consignmentCode: str, Authorization: str = None) -> str: | 33 | def create_waybill_d(consignmentCode: str, Authorization: str = None) -> str: |
| 13 | """根据运单号创建D类运单 | 34 | """根据运单号创建D类运单 |
| 14 | 35 | ||
| ... | @@ -18,6 +39,7 @@ def create_waybill_d(consignmentCode: str, Authorization: str = None) -> str: | ... | @@ -18,6 +39,7 @@ def create_waybill_d(consignmentCode: str, Authorization: str = None) -> str: |
| 18 | 39 | ||
| 19 | Returns: | 40 | Returns: |
| 20 | 创建结果信息(JSON字符串),包含: | 41 | 创建结果信息(JSON字符串),包含: |
| 42 | + - message: 创建结果描述(成功时:"创建运单成功",失败时:具体的失败原因) | ||
| 21 | - consignmentCode: 运单编号 | 43 | - consignmentCode: 运单编号 |
| 22 | - waybill_id: 运单ID | 44 | - waybill_id: 运单ID |
| 23 | - operate_unit_name: 操作单位名称 | 45 | - operate_unit_name: 操作单位名称 |
| ... | @@ -49,7 +71,10 @@ def create_waybill_d(consignmentCode: str, Authorization: str = None) -> str: | ... | @@ -49,7 +71,10 @@ def create_waybill_d(consignmentCode: str, Authorization: str = None) -> str: |
| 49 | 71 | ||
| 50 | # 检查响应状态 | 72 | # 检查响应状态 |
| 51 | if response.status_code != 200: | 73 | if response.status_code != 200: |
| 52 | - return f"❌ 创建失败: HTTP {response.status_code}, 错误信息: {response.text}" | 74 | + return _create_error_response( |
| 75 | + f"创建运单失败: HTTP {response.status_code}, 错误信息: {response.text}", | ||
| 76 | + consignmentCode | ||
| 77 | + ) | ||
| 53 | 78 | ||
| 54 | result = response.json() | 79 | result = response.json() |
| 55 | 80 | ||
| ... | @@ -59,7 +84,7 @@ def create_waybill_d(consignmentCode: str, Authorization: str = None) -> str: | ... | @@ -59,7 +84,7 @@ def create_waybill_d(consignmentCode: str, Authorization: str = None) -> str: |
| 59 | # 业务状态判定(保守容错) | 84 | # 业务状态判定(保守容错) |
| 60 | if result.get('success') is False: | 85 | if result.get('success') is False: |
| 61 | error_msg = result.get('message', '未知错误') | 86 | error_msg = result.get('message', '未知错误') |
| 62 | - return f"❌ 创建失败: {error_msg}" | 87 | + return _create_error_response(f"创建运单失败: {error_msg}", consignmentCode) |
| 63 | 88 | ||
| 64 | # 提取新建的运单ID与编号 | 89 | # 提取新建的运单ID与编号 |
| 65 | data = result.get('data') or {} | 90 | data = result.get('data') or {} |
| ... | @@ -78,6 +103,7 @@ def create_waybill_d(consignmentCode: str, Authorization: str = None) -> str: | ... | @@ -78,6 +103,7 @@ def create_waybill_d(consignmentCode: str, Authorization: str = None) -> str: |
| 78 | 103 | ||
| 79 | # 若拿不到ID,直接返回空数据 | 104 | # 若拿不到ID,直接返回空数据 |
| 80 | if not waybill_id: | 105 | if not waybill_id: |
| 106 | + result_data["message"] = "创建运单失败:未获取到运单ID" | ||
| 81 | return json.dumps(result_data, ensure_ascii=False, indent=2) | 107 | return json.dumps(result_data, ensure_ascii=False, indent=2) |
| 82 | 108 | ||
| 83 | # 请求表头详情获取 operate_unit_name 和 operate_unit_code | 109 | # 请求表头详情获取 operate_unit_name 和 operate_unit_code |
| ... | @@ -127,15 +153,18 @@ def create_waybill_d(consignmentCode: str, Authorization: str = None) -> str: | ... | @@ -127,15 +153,18 @@ def create_waybill_d(consignmentCode: str, Authorization: str = None) -> str: |
| 127 | except Exception: | 153 | except Exception: |
| 128 | pass | 154 | pass |
| 129 | 155 | ||
| 156 | + # 添加成功消息 | ||
| 157 | + result_data["message"] = "创建运单成功" | ||
| 158 | + | ||
| 130 | # 返回 JSON 格式数据 | 159 | # 返回 JSON 格式数据 |
| 131 | return json.dumps(result_data, ensure_ascii=False, indent=2) | 160 | return json.dumps(result_data, ensure_ascii=False, indent=2) |
| 132 | 161 | ||
| 133 | except requests.exceptions.RequestException as e: | 162 | except requests.exceptions.RequestException as e: |
| 134 | - return f"❌ 网络请求失败: {str(e)}" | 163 | + return _create_error_response(f"创建运单失败: 网络请求失败 - {str(e)}", consignmentCode) |
| 135 | except json.JSONDecodeError as e: | 164 | except json.JSONDecodeError as e: |
| 136 | - return f"❌ 响应解析失败: {str(e)}" | 165 | + return _create_error_response(f"创建运单失败: 响应解析失败 - {str(e)}", consignmentCode) |
| 137 | except Exception as e: | 166 | except Exception as e: |
| 138 | - return f"❌ 创建D类运单失败: {str(e)}" | 167 | + return _create_error_response(f"创建运单失败: {str(e)}", consignmentCode) |
| 139 | 168 | ||
| 140 | 169 | ||
| 141 | def create_waybill_d_with_id( | 170 | def create_waybill_d_with_id( |
| ... | @@ -211,6 +240,7 @@ def query_waybill_info(consignmentCode: str, Authorization: str = None) -> str: | ... | @@ -211,6 +240,7 @@ def query_waybill_info(consignmentCode: str, Authorization: str = None) -> str: |
| 211 | 240 | ||
| 212 | Returns: | 241 | Returns: |
| 213 | 运单信息(JSON字符串),包含: | 242 | 运单信息(JSON字符串),包含: |
| 243 | + - message: 查询结果描述(成功时:"查询详情成功",失败时:具体的失败原因) | ||
| 214 | - consignmentCode: 运单编号 | 244 | - consignmentCode: 运单编号 |
| 215 | - waybill_id: 运单ID | 245 | - waybill_id: 运单ID |
| 216 | - operate_unit_name: 操作单位名称 | 246 | - operate_unit_name: 操作单位名称 |
| ... | @@ -262,12 +292,16 @@ def query_waybill_info(consignmentCode: str, Authorization: str = None) -> str: | ... | @@ -262,12 +292,16 @@ def query_waybill_info(consignmentCode: str, Authorization: str = None) -> str: |
| 262 | # 提取运单类型(用于后续查询明细) | 292 | # 提取运单类型(用于后续查询明细) |
| 263 | cons_type_code = head_extra.get("consTypeDicCode") or "consType_D" | 293 | cons_type_code = head_extra.get("consTypeDicCode") or "consType_D" |
| 264 | else: | 294 | else: |
| 265 | - return f"❌ 查询运单信息失败: HTTP {head_resp.status_code}, 错误信息: {head_resp.text}" | 295 | + return _create_error_response( |
| 296 | + f"查询详情失败: HTTP {head_resp.status_code}, 错误信息: {head_resp.text}", | ||
| 297 | + consignmentCode | ||
| 298 | + ) | ||
| 266 | except Exception as e: | 299 | except Exception as e: |
| 267 | - return f"❌ 查询表头信息失败: {str(e)}" | 300 | + return _create_error_response(f"查询详情失败: 查询表头信息失败 - {str(e)}", consignmentCode) |
| 268 | 301 | ||
| 269 | # 若拿不到ID,直接返回已有数据 | 302 | # 若拿不到ID,直接返回已有数据 |
| 270 | if not waybill_id: | 303 | if not waybill_id: |
| 304 | + result_data["message"] = "查询详情失败:未获取到运单ID" | ||
| 271 | return json.dumps(result_data, ensure_ascii=False, indent=2) | 305 | return json.dumps(result_data, ensure_ascii=False, indent=2) |
| 272 | 306 | ||
| 273 | # 请求表体明细获取所有商品明细(sku_code 和 sku_name) | 307 | # 请求表体明细获取所有商品明细(sku_code 和 sku_name) |
| ... | @@ -300,15 +334,18 @@ def query_waybill_info(consignmentCode: str, Authorization: str = None) -> str: | ... | @@ -300,15 +334,18 @@ def query_waybill_info(consignmentCode: str, Authorization: str = None) -> str: |
| 300 | except Exception: | 334 | except Exception: |
| 301 | pass | 335 | pass |
| 302 | 336 | ||
| 337 | + # 添加成功消息 | ||
| 338 | + result_data["message"] = "查询详情成功" | ||
| 339 | + | ||
| 303 | # 返回 JSON 格式数据 | 340 | # 返回 JSON 格式数据 |
| 304 | return json.dumps(result_data, ensure_ascii=False, indent=2) | 341 | return json.dumps(result_data, ensure_ascii=False, indent=2) |
| 305 | 342 | ||
| 306 | except requests.exceptions.RequestException as e: | 343 | except requests.exceptions.RequestException as e: |
| 307 | - return f"❌ 网络请求失败: {str(e)}" | 344 | + return _create_error_response(f"查询详情失败: 网络请求失败 - {str(e)}", consignmentCode) |
| 308 | except json.JSONDecodeError as e: | 345 | except json.JSONDecodeError as e: |
| 309 | - return f"❌ 响应解析失败: {str(e)}" | 346 | + return _create_error_response(f"查询详情失败: 响应解析失败 - {str(e)}", consignmentCode) |
| 310 | except Exception as e: | 347 | except Exception as e: |
| 311 | - return f"❌ 查询运单信息失败: {str(e)}" | 348 | + return _create_error_response(f"查询详情失败: {str(e)}", consignmentCode) |
| 312 | 349 | ||
| 313 | 350 | ||
| 314 | def query_waybill_list(initialize: int = 1, consStatusName: str = "等待录入", Authorization: str = None) -> str: | 351 | def query_waybill_list(initialize: int = 1, consStatusName: str = "等待录入", Authorization: str = None) -> str: | ... | ... |
-
Please register or login to post a comment