Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +91 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import requests
|
| 4 |
+
from fastapi import FastAPI, HTTPException
|
| 5 |
+
from fastapi.staticfiles import StaticFiles
|
| 6 |
+
from fastapi.responses import FileResponse
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
import pytz
|
| 10 |
+
|
| 11 |
+
# --- 配置区域 ---
|
| 12 |
+
# 在这里更新您的 Token 和其他固定参数
|
| 13 |
+
CONFIG = {
|
| 14 |
+
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3OTEwMjE4MjgsInVzZXJuYW1lIjoiMTgwNjc2Mzk1ODgifQ.M5tFVgNKq2HTjClRQYzeGMBKjU9jIrEXXP8mS7yPPU0",
|
| 15 |
+
"ptype": "appid_wx8317b90ed5e4fba3",
|
| 16 |
+
"weixinType": "0",
|
| 17 |
+
"aliId": "2021003196691127",
|
| 18 |
+
"target_api_url": "https://parking.youboyun.com.cn/parkWeb/common/preferential/qrcodes/getQrCode"
|
| 19 |
+
}
|
| 20 |
+
# --- 配置区域结束 ---
|
| 21 |
+
|
| 22 |
+
app = FastAPI()
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# 定义请求体模型,确保前端传来正确的数据
|
| 26 |
+
class QrCodeRequest(BaseModel):
|
| 27 |
+
qrCodeId: str
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# 创建一个API端点,前端将请求这个地址
|
| 31 |
+
@app.post("/api/get_qrcode")
|
| 32 |
+
async def get_qrcode_proxy(request_data: QrCodeRequest):
|
| 33 |
+
"""
|
| 34 |
+
这个函数作为代理,向停车场的API发起请求。
|
| 35 |
+
"""
|
| 36 |
+
# 准备请求头
|
| 37 |
+
headers = {
|
| 38 |
+
'Content-Type': 'application/json',
|
| 39 |
+
'token': CONFIG["token"],
|
| 40 |
+
'ptype': CONFIG["ptype"],
|
| 41 |
+
'weixinType': CONFIG["weixinType"],
|
| 42 |
+
'aliId': CONFIG["aliId"],
|
| 43 |
+
'User-Agent': 'Python/Requests', # 模拟一个User-Agent
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
# 准备请求体
|
| 47 |
+
payload = {
|
| 48 |
+
"qrCodeId": request_data.qrCodeId
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
# 准备带时间戳的URL参数
|
| 52 |
+
# 使用中国时区 (Asia/Shanghai)
|
| 53 |
+
tz = pytz.timezone('Asia/Shanghai')
|
| 54 |
+
current_time = datetime.now(tz).strftime('%a %b %d %Y %H:%M:%S GMT%z (%Z)')
|
| 55 |
+
# 格式化成API需要的样子,例如 'Fri Oct 03 2025 18:16:11 GMT+0800 (中国标准时间)'
|
| 56 |
+
# Python的 %Z 有时不会输出中文,我们手动替换
|
| 57 |
+
current_time_str = current_time.replace('CST', '中国标准时间')
|
| 58 |
+
|
| 59 |
+
params = {
|
| 60 |
+
't': current_time_str
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
# 发起POST请求
|
| 65 |
+
response = requests.post(
|
| 66 |
+
CONFIG["target_api_url"],
|
| 67 |
+
headers=headers,
|
| 68 |
+
params=params,
|
| 69 |
+
json=payload,
|
| 70 |
+
timeout=10 # 10秒超时
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# 检查响应状态码
|
| 74 |
+
response.raise_for_status()
|
| 75 |
+
|
| 76 |
+
# 将从目标API收到的JSON数据直接返回给前端
|
| 77 |
+
return response.json()
|
| 78 |
+
|
| 79 |
+
except requests.exceptions.RequestException as e:
|
| 80 |
+
# 如果请求失败,返回一个详细的错误信息
|
| 81 |
+
raise HTTPException(status_code=502, detail=f"Failed to fetch from target API: {e}")
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# 托管 static 文件夹中的所有文件(如 video)
|
| 85 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# 将根路径 ("/") 指向我们的 index.html 文件
|
| 89 |
+
@app.get("/")
|
| 90 |
+
async def read_index():
|
| 91 |
+
return FileResponse('static/index.html')
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# requirements.txt
|
| 2 |
+
fastapi
|
| 3 |
+
uvicorn
|
| 4 |
+
requests
|
| 5 |
+
pydantic
|
| 6 |
+
python-multipart
|
| 7 |
+
pytz
|