Skip to content

Commit

Permalink
Merge pull request #34 from pylover7/develop-1.0.1
Browse files Browse the repository at this point in the history
✨ develop-1.01
  • Loading branch information
pylover7 authored Dec 4, 2024
2 parents 76146d6 + 71ba0c0 commit ace1ef6
Show file tree
Hide file tree
Showing 83 changed files with 2,011 additions and 822 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.local
.eslintcache
report.html
*/logs

yarn.lock
npm-debug.log*
Expand Down
49 changes: 42 additions & 7 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
name: Docker Image CI
name: 创建并发布Docker镜像

on:
push:
branches: [ "master" ]
release:
types: [published]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
#
steps:
- uses: actions/checkout@v3
- name: Build the Docker image
run: docker build . --file Dockerfile --tag my-image-name:$(date +%s)
- name: Checkout repository
uses: actions/checkout@v4
- name: 登录docker仓库
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: 提取Docker的元数据:tags,labels
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: 构建并发布Docker镜像
id: push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}, ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
labels: ${{ steps.meta.outputs.labels }}

- name: 生成工件证明
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.sqlite*
.venv/
*/migrations
*/logs

# frontend
node_modules
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
构建镜像

```bash
docker build -t material-manager:0.0.8 .
docker build -t material:1.0.1 .
```

启动容器
Expand Down
1 change: 0 additions & 1 deletion backend/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
register_routers,
)
from app.core.init_db import init_db
from app.log import logger

try:
from app.settings.config import settings
Expand Down
6 changes: 3 additions & 3 deletions backend/app/api/v1/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
# @Author :dayezi
from fastapi import APIRouter

from .depart import departRouter
from .system import sysRouter
from .apis import apiRouter
from .menus import menuRouter
from .roles import roleRouter
from .users import userRouter
from .area import areaRouter

adminRouter = APIRouter()
adminRouter.include_router(departRouter, tags=["系统部门模块"], prefix="/depart")
adminRouter.include_router(sysRouter, tags=["系统数据库模块"], prefix="/system")
adminRouter.include_router(sysRouter, tags=["系统模块"], prefix="/system")
adminRouter.include_router(apiRouter, tags=["系统API模块"], prefix="/api")
adminRouter.include_router(menuRouter, tags=["系统菜单模块"], prefix="/menu")
adminRouter.include_router(roleRouter, tags=["系统角色模块"], prefix="/role")
adminRouter.include_router(userRouter, tags=["系统用户模块"], prefix="/user")
adminRouter.include_router(areaRouter, tags=["系统区域模块"], prefix="/area")

__all__ = ["adminRouter"]
2 changes: 1 addition & 1 deletion backend/app/api/v1/admin/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from tortoise.expressions import Q

from app.controllers.api import api_controller
from app.log import logger
from app.utils.log import logger
from app.models.users import Api
from app.schemas import Success, SuccessExtra
from app.schemas.apis import *
Expand Down
79 changes: 79 additions & 0 deletions backend/app/api/v1/admin/area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# coding=utf-8
# @FileName :area.py
# @Time :2024/12/3 16:00
# @Author :dayezi
from fastapi import APIRouter, Query
from tortoise.expressions import Q
from tortoise.exceptions import IntegrityError

from app.controllers.area import areaController
from app.schemas import Success, SuccessExtra, Fail
from app.schemas.area import AreaCreate, AreaUpdate
from app.utils import generate_uuid

areaRouter = APIRouter()

@areaRouter.post("/add", summary="新增区域")
async def create_area(
data: AreaCreate,
):
data.uuid = generate_uuid(data.name).__str__()
try:
area = await areaController.create(obj_in=data)
except IntegrityError:
return Fail(msg="区域名称或编码已存在")
return Success(data=await area.to_dict())


@areaRouter.delete("/delete", summary="删除区域")
async def delete_area(
id: int,
):
await areaController.remove(id)
return Success(msg="删除成功")

@areaRouter.put("/update", summary="更新区域")
async def update_area(
data: AreaUpdate,
):
area = await areaController.update(id=data.id, obj_in=data)
return Success(data=await area.to_dict())

@areaRouter.get("/updateStatus", summary="修改区域状态")
async def update_area_status(
id: int = Query(..., description="区域ID"),
status: int = Query(..., description="状态:启用/停用"),
):
area = await areaController.update(id=id, obj_in={"status": status})
return Success(data=await area.to_dict())


@areaRouter.get("/list", summary="查看区域列表")
async def list_area(
currentPage: int = Query(1, description="页码"),
pageSize: int = Query(10, description="每页数量"),
name: str = Query("", description="区域名称,用于搜索"),
code: str = Query("", description="区域编码,用于搜索"),
):
q = Q()
if name:
q &= Q(name__icontains=name)
if code:
q &= Q(code__icontains=code)
total, area_list = await areaController.list(currentPage, pageSize, q)
data = []
for area in area_list:
item = await area.to_dict()
data.append(item)
return SuccessExtra(total=total, data=data, msg="查询成功", currentPage=currentPage, pageSize=pageSize)

@areaRouter.get("/all", summary="查看所有区域")
async def list_area():
area_list = await areaController.all()
data = []
for area in area_list:
if area.status == 0:
continue
item = await area.to_dict()
data.append(item)
return Success(data=data, msg="查询成功")
43 changes: 0 additions & 43 deletions backend/app/api/v1/admin/depart.py

This file was deleted.

2 changes: 1 addition & 1 deletion backend/app/api/v1/admin/menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from app.controllers.menu import menu_controller
from app.schemas.base import Fail, Success
from app.schemas.menus import *
from app.log import logger
from app.utils.log import logger

menuRouter = APIRouter()

Expand Down
8 changes: 5 additions & 3 deletions backend/app/api/v1/admin/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from app.controllers import role_controller
from app.schemas.base import Success, SuccessExtra
from app.schemas.roles import *
from app.log import logger
from app.utils.log import logger


roleRouter = APIRouter()
Expand Down Expand Up @@ -64,9 +64,11 @@ async def get_role_menu_id(id: int = Query(..., description="角色ID")):
role_obj = await role_controller.get(id=id)
menuID = await role_obj.menus.all().values_list("id", flat=True)
apiId = await role_obj.apis.all().values_list("id", flat=True)
areaID = await role_obj.areas.all().values_list("id", flat=True)
data = {
"menus": menuID,
"apis": apiId
"apis": apiId,
"areas": areaID,
}
return Success(data=data)

Expand All @@ -81,6 +83,6 @@ async def update_role(role_in: RoleUpdate):
@roleRouter.post("/updateRoleAuth", summary="更新角色权限")
async def update_role_menu_id(data: RoleUpdateMenusApis):
role_obj = await role_controller.get(id=data.id)
await role_controller.update_roles(role=role_obj, menu_ids=data.menus, api_ids=data.apis)
await role_controller.update_roles(role=role_obj, menu_ids=data.menus, api_ids=data.apis, area_ids=data.areas)
return Success(msg="权限更新成功")

65 changes: 62 additions & 3 deletions backend/app/api/v1/admin/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
# @FileName :system.py
# @Time :2024/7/6 上午3:49
# @Author :dayezi
from fastapi import APIRouter, Request
from datetime import datetime
from pathlib import Path
from fastapi import APIRouter, Request, Query

from app.settings import settings
from app.core.init_db import test_db, set_db
from app.schemas import Success, Fail
from app.schemas import Success, Fail, SuccessExtra
from app.schemas.admin import DbInfo
from app.log import logger
from app.utils.log import logger

sysRouter = APIRouter()

Expand All @@ -35,3 +37,60 @@ async def set_db_conn(data: DbInfo, request: Request):
except Exception as e:
logger.error(e)
return Fail(msg="数据库设置失败!")


@sysRouter.post("/getLoginLogs", summary="获取登录日志")
async def get_login_logs(
data: dict,
currentPage: int = Query(1, description="页码"),
pageSize: int = Query(15, description="每页数量"),
):
loginLog = Path(__file__).parent.parent.parent.parent.parent.joinpath("logs", "login.log")
# 读取 loginLog 下的文件,按照逆序读取,根据currentPage 和 pageSize 分页,并计算总数量total
logList = []
with open(loginLog, "r", encoding="utf-8") as f:
lines = f.readlines()
# 对读取的日志先经过username和status,以及startTime和endTime 过滤
if data["username"]:
lines = [line for line in lines if data["username"] in line]
if data["status"]:
newLines = []
for line in lines:
if data["status"] == "1" and "SUCCESS" in line:
newLines.append(line)
elif data["status"] == "0" and "ERROR" in line:
newLines.append(line)
lines = newLines
if len(data["loginTime"]) > 1:
lines = [line for line in lines if
datetime.strptime(data["loginTime"][0], "%Y-%m-%d %H:%M:%S")
<= datetime.strptime(line.split("|")[0].strip(), "%Y-%m-%d %H:%M:%S.%f")
<= datetime.strptime(data["loginTime"][1], "%Y-%m-%d %H:%M:%S")]

total = len(lines)
if total == 0:
return SuccessExtra(data=logList, total=total, currentPage=currentPage, pageSize=pageSize)
start_index = total - pageSize * (currentPage - 1)
end_index = start_index - pageSize
if end_index < 0:
end_index = -1
for i in range(start_index, end_index, -1):
# 分割日志行
parts = lines[i - 1].strip().split('|')
# 构建字典
log_dict = {
'username': parts[3].strip(),
'status': 1 if parts[1].strip() == "SUCCESS" else 0,
'ip': parts[2].strip(),
'loginTime': parts[0].strip()
}
logList.append(log_dict)
return SuccessExtra(data=logList, total=total, currentPage=currentPage, pageSize=pageSize)


@sysRouter.get("/clearLoginLogs", summary="清除登录日志")
async def clear_login_logs():
loginLog = Path(__file__).parent.parent.parent.parent.parent.joinpath("logs", "login.log")
with open(loginLog, "w", encoding="utf-8") as f:
f.write("")
return Success(msg="清除成功!")
Loading

0 comments on commit ace1ef6

Please sign in to comment.