Showing
4 changed files
with
141 additions
and
1 deletions
| ... | @@ -13,7 +13,7 @@ API_CONFIG = { | ... | @@ -13,7 +13,7 @@ API_CONFIG = { |
| 13 | "headers": { | 13 | "headers": { |
| 14 | "accept": "*/*", | 14 | "accept": "*/*", |
| 15 | "Content-Type": "application/json", | 15 | "Content-Type": "application/json", |
| 16 | - "Authorization": os.getenv("API_AUTHORIZATION", "Bearer 2.21e3deecca904c7697ab891b8276cf36"), | 16 | + "Authorization": os.getenv("API_AUTHORIZATION", "Bearer 2.bd593d8f36c4468fbf02ee95c473cbb6"), |
| 17 | "Ver": os.getenv("API_VER", "033BD94B1168D7E4F0D644C3C95E35BF.D73E33B659AD1D6B7D181D1DF8D05760"), | 17 | "Ver": os.getenv("API_VER", "033BD94B1168D7E4F0D644C3C95E35BF.D73E33B659AD1D6B7D181D1DF8D05760"), |
| 18 | "Referer": os.getenv("API_REFERER", "http://192.168.1.251/") | 18 | "Referer": os.getenv("API_REFERER", "http://192.168.1.251/") |
| 19 | } | 19 | } | ... | ... |
| ... | @@ -103,3 +103,79 @@ def upload_clearance_file( | ... | @@ -103,3 +103,79 @@ def upload_clearance_file( |
| 103 | return f"上传清关文件失败: {str(e)}" | 103 | return f"上传清关文件失败: {str(e)}" |
| 104 | 104 | ||
| 105 | 105 | ||
| 106 | +def upload_file_for_ocr( | ||
| 107 | + pdf_path: str, | ||
| 108 | + Authorization: Optional[str] = None, | ||
| 109 | +) -> str: | ||
| 110 | + """上传文件进行OCR识别 | ||
| 111 | + | ||
| 112 | + Args: | ||
| 113 | + pdf_path: 本地 PDF 文件路径(必填) | ||
| 114 | + Authorization: 授权令牌,如果提供则使用该值,否则使用 API_CONFIG 中的默认值 | ||
| 115 | + | ||
| 116 | + Returns: | ||
| 117 | + 文本:成功/失败信息 | ||
| 118 | + """ | ||
| 119 | + if not os.path.isfile(pdf_path): | ||
| 120 | + return f"参数错误: 文件不存在 - {pdf_path}" | ||
| 121 | + | ||
| 122 | + # 从文件路径提取文件名 | ||
| 123 | + file_name = os.path.basename(pdf_path) | ||
| 124 | + | ||
| 125 | + # 构建URL(不包含查询参数) | ||
| 126 | + url = f"{API_CONFIG['base_url']}/API/ocrDemo/pushDemo" | ||
| 127 | + | ||
| 128 | + # 封装参数到 params_payload | ||
| 129 | + params_payload = { | ||
| 130 | + "fileName01": file_name, | ||
| 131 | + "fileNumber": 1 | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + # form-data: params 是 JSON 字符串,fileUpload 是文件 | ||
| 135 | + data = { | ||
| 136 | + "params": json.dumps(params_payload, ensure_ascii=False), | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + files = { | ||
| 140 | + "file": (file_name, open(pdf_path, "rb"), "application/pdf"), | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + # 构建 headers,优先使用传入的 Authorization | ||
| 144 | + headers = dict(API_CONFIG.get("headers", {})) | ||
| 145 | + if Authorization: | ||
| 146 | + headers['Authorization'] = Authorization | ||
| 147 | + # 移除 Content-Type(由 requests 根据 multipart 自动设置) | ||
| 148 | + headers.pop("Content-Type", None) | ||
| 149 | + | ||
| 150 | + try: | ||
| 151 | + resp = requests.post(url, headers=headers, data=data, files=files, timeout=60) | ||
| 152 | + # 确保文件句柄尽快关闭 | ||
| 153 | + try: | ||
| 154 | + files["file"][1].close() | ||
| 155 | + except Exception: | ||
| 156 | + pass | ||
| 157 | + | ||
| 158 | + if resp.status_code == 200: | ||
| 159 | + # 解析响应JSON | ||
| 160 | + result = resp.json() | ||
| 161 | + | ||
| 162 | + # 提取指定字段,只返回 status、message、chmCode、chmId | ||
| 163 | + filtered_result = {} | ||
| 164 | + if "status" in result: | ||
| 165 | + filtered_result["status"] = result["status"] | ||
| 166 | + if "message" in result: | ||
| 167 | + filtered_result["message"] = result["message"] | ||
| 168 | + if "chmCode" in result: | ||
| 169 | + filtered_result["chmCode"] = result["chmCode"] | ||
| 170 | + if "chmId" in result: | ||
| 171 | + filtered_result["chmId"] = result["chmId"] | ||
| 172 | + | ||
| 173 | + # 返回过滤后的JSON字符串 | ||
| 174 | + return json.dumps(filtered_result, ensure_ascii=False) | ||
| 175 | + return f"上传失败: HTTP {resp.status_code}, 错误信息: {resp.text}" | ||
| 176 | + | ||
| 177 | + except requests.exceptions.RequestException as e: | ||
| 178 | + return f"网络请求失败: {str(e)}" | ||
| 179 | + except Exception as e: | ||
| 180 | + return f"上传文件进行OCR识别失败: {str(e)}" | ||
| 181 | + | ... | ... |
| ... | @@ -133,6 +133,70 @@ def create_waybill_d(consignmentCode: str, Authorization: str = None) -> str: | ... | @@ -133,6 +133,70 @@ def create_waybill_d(consignmentCode: str, Authorization: str = None) -> str: |
| 133 | return f"❌ 创建D类运单失败: {str(e)}" | 133 | return f"❌ 创建D类运单失败: {str(e)}" |
| 134 | 134 | ||
| 135 | 135 | ||
| 136 | +def create_waybill_d_with_id( | ||
| 137 | + consignmentCode: str, | ||
| 138 | + consignmentId: str, | ||
| 139 | + loginName: str, | ||
| 140 | + userId: int, | ||
| 141 | + Authorization: str = None, | ||
| 142 | +) -> str: | ||
| 143 | + """根据运单ID及运单号创建D类运单 | ||
| 144 | + | ||
| 145 | + Args: | ||
| 146 | + consignmentCode: 运单号(必填) | ||
| 147 | + consignmentId: 运单ID(必填) | ||
| 148 | + loginName: 登录名(必填) | ||
| 149 | + userId: 用户ID(必填) | ||
| 150 | + Authorization: 授权令牌,如果提供则使用该值,否则使用 API_CONFIG 中的默认值 | ||
| 151 | + | ||
| 152 | + Returns: | ||
| 153 | + 创建结果,成功时返回序列化的JSON,失败时返回错误信息 | ||
| 154 | + """ | ||
| 155 | + try: | ||
| 156 | + url = f"{API_CONFIG['base_url']}/Exp/bus-customer/consignment/createChmConsignmentD/demo" | ||
| 157 | + | ||
| 158 | + # 构建请求数据 | ||
| 159 | + data = { | ||
| 160 | + "consignmentCode": consignmentCode, | ||
| 161 | + "consignmentId": consignmentId, | ||
| 162 | + "isAIRecognition": 1, # 写死为1 | ||
| 163 | + "loginName": loginName, | ||
| 164 | + "userId": userId | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + # 构建 headers,优先使用传入的 Authorization | ||
| 168 | + headers = dict(API_CONFIG['headers']) | ||
| 169 | + if Authorization: | ||
| 170 | + headers['Authorization'] = Authorization | ||
| 171 | + | ||
| 172 | + # 发送POST请求 | ||
| 173 | + response = requests.post( | ||
| 174 | + url, | ||
| 175 | + headers=headers, | ||
| 176 | + json=data, | ||
| 177 | + timeout=30 | ||
| 178 | + ) | ||
| 179 | + | ||
| 180 | + # 检查响应状态 | ||
| 181 | + if response.status_code == 200: | ||
| 182 | + result = response.json() | ||
| 183 | + # 提取运单ID | ||
| 184 | + data = result.get('data') or {} | ||
| 185 | + extra = data.get('extra') or {} | ||
| 186 | + waybill_id = extra.get('id') | ||
| 187 | + if waybill_id is not None: | ||
| 188 | + return waybill_id # 返回数字格式的运单ID | ||
| 189 | + return f"❌ 创建失败: 响应中未找到运单ID" | ||
| 190 | + return f"❌ 创建失败: HTTP {response.status_code}, 错误信息: {response.text}" | ||
| 191 | + | ||
| 192 | + except requests.exceptions.RequestException as e: | ||
| 193 | + return f"❌ 网络请求失败: {str(e)}" | ||
| 194 | + except json.JSONDecodeError as e: | ||
| 195 | + return f"❌ 响应解析失败: {str(e)}" | ||
| 196 | + except Exception as e: | ||
| 197 | + return f"❌ 创建D类运单失败: {str(e)}" | ||
| 198 | + | ||
| 199 | + | ||
| 136 | def query_waybill_info(consignmentCode: str, Authorization: str = None) -> str: | 200 | def query_waybill_info(consignmentCode: str, Authorization: str = None) -> str: |
| 137 | """根据运单编号查询运单信息 | 201 | """根据运单编号查询运单信息 |
| 138 | 202 | ... | ... |
This diff is collapsed. Click to expand it.
-
Please register or login to post a comment