更新 main.py
parent
c81cde9aee
commit
8051d4a027
75
main.py
75
main.py
|
|
@ -8,12 +8,7 @@ app = FastAPI()
|
|||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "Hello World"}
|
||||
|
||||
|
||||
@app.get("/hello/{name}")
|
||||
async def say_hello(name: str):
|
||||
return {"message": f"Hello {name}"}
|
||||
return "welcome to fastAPI!"
|
||||
|
||||
|
||||
@app.post("/user/login")
|
||||
|
|
@ -34,22 +29,22 @@ async def login(request: Request):
|
|||
|
||||
@app.get("/api/get-menu")
|
||||
async def get_menu():
|
||||
base_url = "http://127.0.0.1:8000/api/menu-image/"
|
||||
directory = "assets/images/icon"
|
||||
pic_list = [
|
||||
{id: 1, "menu_name": "政策查询", "url": "/pages/policy/policy", "path": "assets/images/icon/政策解读.png"},
|
||||
{id: 2, "menu_name": "问答", "url": "/pages/qa/qa", "path": "assets/images/icon/问答.png"},
|
||||
{id: 3, "menu_name": "信息管理", "url": "/pages/assist/assist", "path": "assets/images/icon/群众信息管理.png"},
|
||||
{id: 4, "menu_name": "招聘", "url": "/pages/recruit/recruit", "path": "assets/images/icon/招聘.png"}
|
||||
{id: 1, "menu_name": "政策查询", "url": "/pages/policy/policy", "icon_name": "政策解读.png"},
|
||||
{id: 2, "menu_name": "问答", "url": "/pages/qa/qa", "icon_name": "问答.png"},
|
||||
{id: 3, "menu_name": "信息管理", "url": "/pages/assist/assist", "icon_name": "群众信息管理.png"},
|
||||
{id: 4, "menu_name": "招聘", "url": "/pages/recruit/recruit", "icon_name": "招聘.png"}
|
||||
]
|
||||
data = []
|
||||
for item in pic_list:
|
||||
with open(item['path'], "rb") as image_file:
|
||||
encoded_string = base64.b64encode(image_file.read()).decode()
|
||||
data.append({
|
||||
"id": item[id],
|
||||
"menu_name": item['menu_name'],
|
||||
"url": item['url'],
|
||||
"base64EncodedImage": encoded_string
|
||||
})
|
||||
data.append({
|
||||
"id": item[id],
|
||||
"menu_name": item['menu_name'],
|
||||
"url": item['url'],
|
||||
"image_url": base_url + item['icon_name']
|
||||
})
|
||||
|
||||
return {
|
||||
"code": 0,
|
||||
|
|
@ -91,4 +86,46 @@ async def get_swiper_image(image_id: str):
|
|||
return FileResponse(image_path)
|
||||
except FileNotFoundError:
|
||||
# 如果文件没有找到,返回404错误
|
||||
raise HTTPException(status_code=404, detail="Image not found")
|
||||
raise HTTPException(status_code=404, detail="Image not found")
|
||||
|
||||
|
||||
@app.get("/api/menu-image/{image_name}.png")
|
||||
async def get_menu_image(image_name: str):
|
||||
base_image_path = "assets/images/icon"
|
||||
# 构造图片文件的完整路径
|
||||
image_path = f"{base_image_path}/{image_name}.png" # 假设图片格式为png,根据需要修改
|
||||
print(image_path)
|
||||
try:
|
||||
# 返回图片文件的响应
|
||||
return FileResponse(image_path)
|
||||
except FileNotFoundError:
|
||||
# 如果文件没有找到,返回404错误
|
||||
raise HTTPException(status_code=404, detail="Image not found")
|
||||
|
||||
|
||||
@app.get("/api/policy-list")
|
||||
async def get_policy_list():
|
||||
policy_list = [
|
||||
{"title": "政策标题1", "date": "2024-01-01"},
|
||||
{"title": "政策标题2", "date": "2023-01-02"},
|
||||
{"title": "政策标题3", "date": "2022-01-02"},
|
||||
{"title": "政策标题4", "date": "2023-01-02"},
|
||||
{"title": "政策标题5", "date": "2022-01-02"},
|
||||
{"title": "政策标题6", "date": "2023-01-02"},
|
||||
{"title": "政策标题7", "date": "2021-01-02"},
|
||||
{"title": "政策标题8", "date": "2021-01-02"},
|
||||
{"title": "政策标题9", "date": "2024-02-02"},
|
||||
{"title": "政策标题10", "date": "2024-03-02"},
|
||||
{"title": "政策标题11", "date": "2024-04-02"},
|
||||
{"title": "政策标题12", "date": "2024-01-02"},
|
||||
{"title": "政策标题13", "date": "2024-01-02"},
|
||||
{"title": "政策标题14", "date": "2024-01-02"},
|
||||
{"title": "政策标题15", "date": "2024-01-02"},
|
||||
{"title": "政策标题16", "date": "2024-01-02"},
|
||||
{"title": "政策标题17", "date": "2024-01-02"}
|
||||
]
|
||||
return {
|
||||
"code": 0,
|
||||
"msg": "ok",
|
||||
"data": policy_list
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue