fastAPI/main.py

132 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import FileResponse
import os
app = FastAPI()
@app.get("/")
async def root():
return "welcome to fastAPI!"
@app.post("/user/login")
async def login(request: Request):
print(request)
data = await request.json()
username = data.get("username")
password = data.get("password")
print(f"username: {username},password: {password}")
if username == "1" and password == "1":
data1 = None
return {"code": "0", "msg": "ok", "data": data1}
else:
data2 = None
return {"code": "-1", "msg": "账号或密码错误", "data": data2}
@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", "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:
data.append({
"id": item[id],
"menu_name": item['menu_name'],
"url": item['url'],
"image_url": base_url + item['icon_name']
})
return {
"code": 0,
"msg": "ok",
"data": data
}
@app.get("/api/get-swiper-list")
async def get_swiper_list():
# 设置图片所在的目录
directory = "assets/images/pictures"
base_url = "http://127.0.0.1:8000/api/swiper-image/"
# 列出目录下所有文件
files = os.listdir(directory)
for i in files:
if i == '.DS_Store':
files.remove(i)
print(files)
# 生成包含完整路径的图片列表
swiper_list = [{"id": i + 1, "path": base_url + file} for i, file in enumerate(files)]
return {
"code": 0,
"msg": "ok",
"data": swiper_list
}
@app.get("/api/swiper-image/{image_id}.png")
async def get_swiper_image(image_id: str):
base_image_path = "assets/images/pictures"
# 构造图片文件的完整路径
image_path = f"{base_image_path}/{image_id}.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/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-03"},
{"title": "政策标题4", "date": "2023-01-14"},
{"title": "政策标题5", "date": "2022-01-14"},
{"title": "政策标题6", "date": "2023-01-15"},
{"title": "政策标题7", "date": "2021-01-16"},
{"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": "2023-01-24"},
{"title": "政策标题14", "date": "2022-01-02"},
{"title": "政策标题15", "date": "2022-12-02"},
{"title": "政策标题16", "date": "2021-04-02"},
{"title": "政策标题17", "date": "2020-02-02"}
]
return {
"code": 0,
"msg": "ok",
"data": policy_list
}