Forráskód Böngészése

完成文档下载接口功能

yangzeyu 11 hónapja
szülő
commit
65931b6e57
3 módosított fájl, 53 hozzáadás és 10 törlés
  1. 39 6
      api.py
  2. 13 3
      api_test.py
  3. 1 1
      config/config.py

+ 39 - 6
api.py

@@ -1,5 +1,5 @@
-from config
-from fastapi import FastAPI, Request, status, BackgroundTasks
+from config import load_service_config
+from fastapi import FastAPI, Request, status, BackgroundTasks, HTTPException
 from fastapi.exceptions import RequestValidationError
 from fastapi.responses import JSONResponse
 from database.dao.mysql_dao import MySqlDao
@@ -14,6 +14,7 @@ from typing import List, Dict
 app = FastAPI()
 dao = MySqlDao()
 
+cfgs = load_service_config()
 # 添加全局异常处理器
 @app.exception_handler(RequestValidationError)
 async def validation_exception_handler(request: Request, exc: RequestValidationError):
@@ -83,12 +84,12 @@ def generate_report(city_uuid, product_id, recall_count, delivery_count):
     """生成报告"""
     report_util = ReportUtils(city_uuid, product_id)
     report_util.generate_all_data(recall_count, delivery_count)
-    repots_dir = os.path.join('./data/reports', city_uuid, product_id)
-    upload_file(repots_dir)
+    # repots_dir = os.path.join('./data/reports', city_uuid, product_id)
+    # upload_file(repots_dir)
     
 def upload_file(reports_dir):
     """上传报告文件"""
-    base_url = "https://10-79-117-86-8p1kxyomtjwgt3.ztna-dingtalk.com/screen/mapi/file/fileUpload"
+    base_url = cfgs["aliyun"]["upload_url"]
     files = [
         "卷烟信息表.xlsx",
         "品规商户特征关系表.xlsx",
@@ -133,12 +134,44 @@ def upload_file(reports_dir):
     files_id_str = ""
     if files_id:
         for filename, file_id in files_id.items():
-            files_id_str += f"{filename}, {file_id}\n"
+            files_id_str += f"{filename},{file_id}\n"
     else:
         files_id_str = "failed"
     with open(os.path.join(reports_dir, "files_id.txt"), 'w', encoding="utf-8") as file:
         file.write(files_id_str)
 
+@app.post("/brandcultivation/api/v1/report")
+async def get_file_id(request: ReportRequest):
+    files_id_path = os.path.join("./data/reports", request.city_uuid, request.product_code, "files_id.txt")
+    if not os.path.exists(files_id_path):
+        raise HTTPException(
+            status_code=status.HTTP_404_NOT_FOUND,
+            detail="Reports not found"
+        )
+    
+    with open(files_id_path) as file:
+        lines = file.readlines()
+        
+    if lines[0].strip() == "failed":
+        raise HTTPException(
+            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
+            detail="reports upload failed"
+        )
+    
+    request_data = []
+    for index, line in enumerate(lines):
+        filename, file_id = line.strip().split(",")
+        request_data.append(
+            {
+                "id": index+1,
+                "filename": filename,
+                "file_id": file_id
+            }
+        )
+    
+    return {"code": 200, "msg": "success", "data": {"reportInfo": request_data}}
+        
+    
 if __name__ == "__main__":
     uvicorn.run(app, host="0.0.0.0", port=7960)
     # report_dir = "./data/reports/00000000000000000000000011445301/440298"

+ 13 - 3
api_test.py

@@ -1,12 +1,22 @@
 import requests
 import json
 
-url = "http://127.0.0.1:7960/brandcultivation/api/v1/recommend"
+# url = "http://127.0.0.1:7960/brandcultivation/api/v1/recommend"
+# payload = {
+#     "city_uuid": "00000000000000000000000011445301",
+#     "product_code": "440298",
+#     "recall_cust_count": 500,
+#     "delivery_count": 5000
+# }
+# headers = {'Content-Type': 'application/json'}
+
+# response = requests.post(url, data=json.dumps(payload), headers=headers)
+# print(response.json())
+
+url = "http://127.0.0.1:7960/brandcultivation/api/v1/report"
 payload = {
     "city_uuid": "00000000000000000000000011445301",
     "product_code": "440298",
-    "recall_cust_count": 500,
-    "delivery_count": 5000
 }
 headers = {'Content-Type': 'application/json'}
 

+ 1 - 1
config/config.py

@@ -12,5 +12,5 @@ def load_model_config():
 
 def load_service_config():
     with open("./config/model_config.yaml") as file:
-        config = yaml,safe_load(file)
+        config = yaml.safe_load(file)
     return config