Skip to content

Commit b37f68b

Browse files
committed
解决 服务器 时区不对 问题
1 parent 4bf964f commit b37f68b

File tree

8 files changed

+35
-13
lines changed

8 files changed

+35
-13
lines changed

backend/apis/sys_log.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
@router.get("/list")
16-
async def root(db: GetDB, page: PageQuery) -> ResultSchema[SysLogOut]:
16+
async def root(db: GetDB, page: PageQuery) -> ResultSchema[list[SysLogOut]]:
1717
data = sys_log_crud.get_all(db, page)
1818
total = sys_log_crud.get_count(db)
1919
return Result.success(data=data, total=total)

backend/common/route_log.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from models import RequestIp, SysLog
2020
from utils.custom_log import my_logger
2121
from utils.custom_exc import DuplicateRequests
22+
from utils.handle_date import get_current_time
2223
from utils.ip_utils import IPUtils
2324

2425

@@ -49,7 +50,7 @@ def prevent_duplicate_requests(request: Request):
4950
key = f"{request.client.host}+{request.get('path')}"
5051
try:
5152
request_obj = RequestIp.get(key)
52-
if request_obj.num >= 5:
53+
if request_obj.num >= 10:
5354
raise DuplicateRequests()
5455
else:
5556
RequestIp(num=request_obj.num + 1, pk=key).update()
@@ -82,11 +83,13 @@ async def get_request_params(request: Request) -> dict:
8283
async def save_log(request: Request, duration: float):
8384
""" 保存日志 """
8485
request_params = await get_request_params(request)
85-
current_time = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
8686
sys_log = SysLog(url=request.get("path"), method=request.method, ip=IPUtils.get_ip(request), params=request_params,
87-
spend_time=duration, create_time=current_time)
87+
spend_time=duration, create_time=get_current_time())
8888

8989
my_logger.info(f"访问记录: {jsonable_encoder(sys_log)}.")
9090

91+
if request.get("path") == "/api/log/list": # 排除日志接口
92+
return
93+
9194
with SessionLocal() as db:
9295
sys_log_crud.create(db, sys_log)

backend/crud/role.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from sqlalchemy.orm import Session
88

99
from crud.base import CRUDBase
10-
from models import Role, UserRole, User
10+
from models import Role, UserRole
1111
from schemas import RoleIn
1212

1313

backend/main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
# register_middleware(app) # 注册请求响应拦截
2424

25-
# init_table() # 初始化表
26-
# init_data() # 初始化表数据
25+
init_table() # 初始化表
26+
init_data() # 初始化表数据
2727

2828
my_logger.info("项目启动成功!!!")
2929

backend/schemas/gmt.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
class GMT(BaseModel):
1212
""" 时间字段处理 """
13-
create_time: datetime
14-
update_time: datetime
13+
create_time: datetime | None
14+
update_time: datetime | None
1515

1616
@validator("create_time", "update_time")
1717
def format_time(cls, value: datetime) -> str:

backend/utils/handle_date.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
# @Time : 2023/4/26 15:18
4+
# @Author : zxiaosi
5+
# @desc : 时间处理工具类
6+
import datetime
7+
import time
8+
9+
10+
def get_current_time():
11+
""" 获取当前时间(Asia/Shanghai) """
12+
13+
timestamp = time.time() # 获取当前时间戳
14+
15+
offset = datetime.timezone(datetime.timedelta(hours=8)) # 获取时区偏移量
16+
17+
shanghai_time = datetime.datetime.fromtimestamp(timestamp, offset) # 将时间戳转换为时区时间
18+
19+
return shanghai_time.strftime("%Y-%m-%d %H:%M:%S") # 格式化时间

docker-compose.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ services:
6060
- /opt/docker/mysql/data/:/var/lib/mysql # 数据文件夹 (宿主机:容器)
6161
- /opt/docker/mysql/logs:/logs # 日志文件夹 (宿主机:容器)
6262
environment:
63-
MYSQL_ROOT_PASSWORD: 123456 # root用户密码 (自定义)
64-
TZ: Asia/Shanghai # mysql时区
63+
- MYSQL_ROOT_PASSWORD=123456 # root用户密码 (自定义)
64+
- TZ=Asia/Shanghai # mysql时区
6565
command:
6666
# MySQL8的密码验证方式默认是 caching_sha2_password,但是很多的连接工具还不支持该方式
6767
# 就需要手动设置下mysql的密码认证方式为以前的 mysql_native_password 方式

frontend/src/components/MyTable.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ const props = withDefaults(defineProps<Props>(), {});
9292
:total="props.total"
9393
:current-page="props.page"
9494
:page-size="props.pageSize"
95-
v-on:current-change="props?.onPageChange"
96-
v-on:size-change="props?.onSizeChange"
95+
@current-change="props?.onPageChange"
96+
@size-change="props?.onSizeChange"
9797
>
9898
第 {{ (props.page - 1) * props.pageSize + 1 }}-{{ props.page * props.pageSize }} 条 / 共 {{ props.total }} 条
9999
</el-pagination>

0 commit comments

Comments
 (0)