diff --git a/README.md b/README.md index 08bf090..5611771 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ ```plaintext project/ # 项目根目录 ├── src/ # ✅ 源代码包 -│ └── __init__.py # 包标识文件(可定义公共API)[9,10](@ref) +│ └── __init__.py # 包标识文件(可定义公共API) ├── tests/ # ✅ 测试目录(pytest自动发现用例) -│ └── test_example.py # 测试示例(需以`test_`前缀命名)[2,9](@ref) +│ └── test_example.py # 测试示例(需以`test_`前缀命名) ├── .python-version # ✅ 指定Python版本(如3.11.4) ├── main.py # ✅ 主程序入口(CLI执行入口) -├── pyproject.toml # ✅ 全局配置(依赖/元数据/构建设置)[10](@ref) -├── README.md # ✅ 项目说明(必含安装/使用/发布指南)[6,7](@ref) -└── uv.lock # ✅ UV依赖快照(锁定版本号,可选但推荐)[8](@ref) \ No newline at end of file +├── pyproject.toml # ✅ 全局配置(依赖/元数据/构建设置) +├── README.md # ✅ 项目说明(必含安装/使用/发布指南) +└── uv.lock # ✅ UV依赖快照(锁定版本号,可选但推荐) \ No newline at end of file diff --git a/main.py b/main.py index e0797ac..cb0bd25 100644 --- a/main.py +++ b/main.py @@ -1,29 +1,11 @@ -from fastapi import FastAPI import uvicorn -from src.utils.Log import Log - -app = FastAPI(title="yin_home文档") - - -@app.get('/weather', tags=["获取天气数据"], summary="返回实时天气数据") -def weather(): - return { - "code": 200, - "data": { - "city": "上海", - "weather": "多云", - "temperature": "25", - "wind": "南风", - "humidity": "60", - "pressure": "1010", - "update_time": "2021-09-01 12:00:00" - } - } - - - +from fastapi import FastAPI +from src.routers import routers # 导入路由集合 +app = FastAPI(title="FastAPI文档") +for router in routers: + app.include_router(router) # 自动注册所有路由 if __name__ == '__main__': uvicorn.run(app='main:app', host='0.0.0.0', port=1314, reload=True) diff --git a/src/common/Date_Time.py b/src/common/Date_Time.py new file mode 100644 index 0000000..21308b9 --- /dev/null +++ b/src/common/Date_Time.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import datetime +import time + + +def get_period(time_stamp): + time_array = time.localtime(int(time_stamp)/1000) + return time_array.tm_hour + + +def get_date_time(date_str): + date_ = datetime.datetime.strptime(date_str, '%Y-%m-%d') + return date_ + + +def get_hour_diff(datetime1, datetime2): + td = datetime2 - datetime1 + return td.days * 24 + td.seconds / 3600 + + +def get_day_diff(datetime1, datetime2): + td = datetime2 - datetime1 + return td.days + td.seconds / 3600 / 24 + + +def get_current_day(): + today = datetime.date.today() + return today + + +def get_now_datetime(): + return datetime.datetime.now() + + +def get_current_yesterday(): + today = get_current_day() + oneday = datetime.timedelta(days=1) + yesterday = today - oneday + return yesterday.strftime("%Y-%m-%d") + + +def get_yesterday(date_str): + date_ = get_date_time(date_str) + yesterday = date_ + datetime.timedelta(days=-1) + return yesterday + + +def get_lastyesterday(date_str): + date_ = get_date_time(date_str) + lastyesterday = date_ + datetime.timedelta(days=-2) + return lastyesterday + + +def get_lastweek_date(date_str): + date_ = get_date_time(date_str) + lastweek_date = date_ + datetime.timedelta(days=-7) + return lastweek_date + + +def get_lastlastweek_date(date_str): + date_ = get_date_time(date_str) + lastlastweek_date = date_ + datetime.timedelta(days=-14) + return lastlastweek_date + + +def get_lastweek_start_end_date(date_str): + date_ = get_date_time(date_str) + week = date_.strftime("%w") + week = int(week) + start_lastweek_date = date_ + datetime.timedelta(days=-(week + 6)) + end_lastweek_date = date_ + datetime.timedelta(days=-week) + + return start_lastweek_date, end_lastweek_date + + +def add_day(d_date, n): + return (d_date + datetime.timedelta(days=n)).strftime("%Y-%m-%d") + + +def get_lastmonth_start_end_date(date_str): + date_ = get_date_time(date_str) + start_lastmonth_date = datetime.date(date_.year, date_.month - 1, 1) + end_lastmonth_date = datetime.date(date_.year, date_.month, 1) - datetime.timedelta(1) + return start_lastmonth_date, end_lastmonth_date + + +def get_date_time_by_timestamp(time_stamp): + time_array = time.localtime(int(time_stamp)/1000) + otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", time_array) + return otherStyleTime + +# 获取当前时间字符%Y-%m-%d %H:%M:%S +def new_date_str(): + return datetime.date.today().strftime("%Y-%m-%d") + +# 获取指定时间-day天的日期 +def schedule_date(day): + # 获取当前日期 + current_date = datetime.date.today() + # 计算前一天的日期 + previous_date = current_date - datetime.timedelta(days=day) + # 格式化日期输出 + return previous_date.strftime('%Y-%m-%d') + +if __name__ == "__main__": + startDate = '2020-04-10' diff --git a/src/common/FastResponse.py b/src/common/FastResponse.py new file mode 100644 index 0000000..e69de29 diff --git a/src/common/Get_Host_Name.py b/src/common/Get_Host_Name.py new file mode 100644 index 0000000..d820281 --- /dev/null +++ b/src/common/Get_Host_Name.py @@ -0,0 +1,5 @@ +import socket + +hostname = socket.gethostname() +print("本机的主机名是:", hostname) +print(hostname.split('-')[0]) diff --git a/src/routers/__init__.py b/src/routers/__init__.py new file mode 100644 index 0000000..6957f7a --- /dev/null +++ b/src/routers/__init__.py @@ -0,0 +1,4 @@ +from .weather import router as weather_router +from .order import router as order_router + +routers = [weather_router,order_router] # 统一存放所有路由 \ No newline at end of file diff --git a/src/routers/order.py b/src/routers/order.py new file mode 100644 index 0000000..08506bd --- /dev/null +++ b/src/routers/order.py @@ -0,0 +1,9 @@ +from fastapi import APIRouter +from fastapi.responses import JSONResponse + +router = APIRouter(tags=["订单"], prefix="/order") # prefix为统一前缀(可选) + + +@router.get('/list', summary="获取订单数据") +def weather(): + return JSONResponse(content={"code": 200, "msg": "success", "data": "hello world"}) diff --git a/src/routers/weather.py b/src/routers/weather.py new file mode 100644 index 0000000..be8de38 --- /dev/null +++ b/src/routers/weather.py @@ -0,0 +1,8 @@ +from fastapi import APIRouter +from fastapi.responses import JSONResponse + +router = APIRouter(tags=["天气"], prefix="/weather") # prefix为统一前缀(可选) + +@router.get('/details', summary="返回实时天气数据") +def weather(): + return JSONResponse(content={"code": 200, "msg": "success", "data": "hello world"})