import json
import time
from abc import ABC, abstractmethod
from typing import Union
from fastapi import Depends, Request
from fastapi_pagination.bases import AbstractPage
from fastapi_pagination.ext.databases import paginate
from sqlalchemy import select
from like.admin.config import AdminConfig
from like.admin.schemas.system import (
SystemAuthAdminCreateIn, SystemAuthAdminEditIn, SystemAuthAdminUpdateIn, SystemAuthAdminListIn,
SystemAuthAdminOut, SystemAuthAdminSelfOneOut, SystemAuthAdminSelfOut, SystemAuthAdminDetailOut)
from like.dependencies.database import db
from like.exceptions.base import AppException
from like.http_base import HttpResp
from like.models import (
system_auth_admin, system_auth_menu, system_auth_role, system_auth_dept, SystemAuthAdmin)
from like.utils.redis import RedisUtil
from like.utils.tools import ToolsUtil
from like.utils.urls import UrlUtil
from .auth_perm import ISystemAuthPermService, SystemAuthPermService
from .auth_role import ISystemAuthRoleService, SystemAuthRoleService
class ISystemAuthAdminService(ABC):
"""系统管理员服务抽象类"""
@abstractmethod
async def find_by_username(self, username: str) -> Union[SystemAuthAdmin, None]:
pass
@abstractmethod
async def self(self, admin_id: int) -> SystemAuthAdminSelfOut:
pass
@abstractmethod
async def list(self, list_in: SystemAuthAdminListIn) -> AbstractPage[SystemAuthAdminOut]:
pass
@abstractmethod
async def detail(self, id_: int) -> SystemAuthAdminOut:
pass
@abstractmethod
async def add(self, admin_create_in: SystemAuthAdminCreateIn):
pass
@abstractmethod
async def edit(self, admin_edit_in: SystemAuthAdminEditIn):
pass
@abstractmethod
async def update(self, admin_update_in: SystemAuthAdminUpdateIn, admin_id: int):
pass
@abstractmethod
async def delete(self, id_: int):
pass
@abstractmethod
async def disable(self, id_: int):
pass
@classmethod
@abstractmethod
async def cache_admin_user_by_uid(cls, id_: int):
pass
class SystemAuthAdminService(ISystemAuthAdminService):
"""系统管理员服务实现类"""
async def find_by_username(self, username: str) -> Union[SystemAuthAdmin, None]:
"""根据账号查找管理员"""
row = await db.fetch_one(
system_auth_admin.select().where(system_auth_admin.c.username == username).limit(1))
return SystemAuthAdmin(**row) if row else None
async def self(self, admin_id: int) -> SystemAuthAdminSelfOut:
"""当前管理员"""
# 管理员信息
sys_admin = await db.fetch_one(
system_auth_admin.select().where(
system_auth_admin.c.id == admin_id, system_auth_admin.c.is_delete == 0).limit(1))
# 角色权限
auths = []
if admin_id > 1:
menu_ids = await self.auth_perm_service.select_menu_ids_by_role_id(
[int(i) for i in sys_admin.role_ids.split(',')])
if menu_ids:
menus = await db.fetch_all(
system_auth_menu.select()
.where(system_auth_menu.c.id.in_(menu_ids), system_auth_menu.c.is_disable == 0,
system_auth_menu.c.menu_type.in_(['C', 'A']))
.order_by(system_auth_menu.c.menu_sort, system_auth_menu.c.id))
if menus:
auths.extend((i.perms.strip() for i in menus if i))
if not auths:
auths.append('')
else:
auths.append('*')
admin = SystemAuthAdminSelfOneOut.from_orm(sys_admin)
admin.avatar = await UrlUtil.to_absolute_url(admin.avatar)
return SystemAuthAdminSelfOut(user=admin, permissions=auths)
async def list(self, list_in: SystemAuthAdminListIn) -> AbstractPage[SystemAuthAdminOut]:
"""管理员列表"""
columns = [system_auth_admin.c.id,
system_auth_admin.c.dept_ids.label('dept'), system_auth_admin.c.role_ids.label('role'),
system_auth_admin.c.username, system_auth_admin.c.nickname, system_auth_admin.c.avatar,
system_auth_admin.c.is_multipoint, system_auth_admin.c.is_disable,
system_auth_admin.c.last_login_ip, system_auth_admin.c.last_login_time,
system_auth_admin.c.create_time, system_auth_admin.c.update_time]
# 查询条件
where = [system_auth_admin.c.is_delete == 0]
if list_in.username:
where.append(system_auth_admin.c.username.like(f'%{list_in.username}%'))
if list_in.nickname:
where.append(system_auth_admin.c.nickname.like(f'%{list_in.nickname}%'))
if list_in.role is not None:
where.append(system_auth_admin.c.role.in_(list_in.role))
query = select(columns).where(*where) \
.select_from(system_auth_admin) \
.order_by(system_auth_admin.c.id.desc(), system_auth_admin.c.sort.desc())
pager = await paginate(db, query)
# 处理返回结果
for obj in pager.lists:
obj.avatar = await UrlUtil.to_absolute_url(obj.avatar)
if obj.id == 1:
obj.role = '系统管理员'
else:
role_ids = [int(i) for i in obj.role.split(',') if i.isdigit()]
roles = await db.fetch_all(system_auth_role.select().where(system_auth_role.c.id.in_(role_ids)))
obj.role = '/'.join([i.name for i in roles])
if not obj.dept:
obj.dept = ''
else:
dept_ids = [int(i) for i in obj.dept.split(',') if i.isdigit()]
depts = await db.fetch_all(system_auth_dept.select().where(
system_auth_dept.c.id.in_(dept_ids), system_auth_dept.c.is_delete == 0))
obj.dept = '/'.join([i.name for i in depts])
return pager
async def detail(self, id_: int) -> SystemAuthAdminDetailOut:
"""管理员详细"""
sys_admin = await db.fetch_one(
system_auth_admin.select().where(
system_auth_admin.c.id == id_, system_auth_admin.c.is_delete == 0).limit(1))
assert sys_admin, '账号已不存在!'
sys_admin_out = SystemAuthAdminDetailOut.from_orm(sys_admin)
sys_admin_out.avatar = await UrlUtil.to_absolute_url(sys_admin_out.avatar)
sys_admin_out.roleIds = [int(i) for i in sys_admin_out.roleIds.split(',') if i.isdigit()]
sys_admin_out.deptIds = [int(i) for i in sys_admin_out.deptIds.split(',') if i.isdigit()]
sys_admin_out.postIds = [int(i) for i in sys_admin_out.postIds.split(',') if i.isdigit()]
return sys_admin_out
async def add(self, admin_create_in: SystemAuthAdminCreateIn):
"""管理员新增"""
assert not await db.fetch_one(
system_auth_admin.select()
.where(system_auth_admin.c.username == admin_create_in.username,
system_auth_admin.c.is_delete == 0).limit(1)), '账号已存在换一个吧!'
assert not await db.fetch_one(
system_auth_admin.select()
.where(system_auth_admin.c.nickname == admin_create_in.nickname,
system_auth_admin.c.is_delete == 0).limit(1)), '昵称已存在换一个吧!'
if not (6 <= len(admin_create_in.password) <= 20):
raise AppException(HttpResp.FAILED, msg='密码必须在6~20位')
create_dict = dict(admin_create_in)
salt = ToolsUtil.random_string(5)
create_dict['role_ids'] = ','.join([str(i) for i in admin_create_in.role_ids])
create_dict['dept_ids'] = ','.join([str(i) for i in admin_create_in.dept_ids])
create_dict['post_ids'] = ','.join([str(i) for i in admin_create_in.post_ids])
create_dict['salt'] = salt
create_dict['password'] = ToolsUtil.make_md5(f'{admin_create_in.password.strip()}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
这是一个基于FastAPI和Vue3的Python管理后台设计,使用JavaScript、Vue、Python、CSS、TypeScript、HTML和Shell语言开发,包含1511个文件。主要文件类型包括487个JavaScript文件、248个Vue文件、228个SVG文件、126个Python源文件、125个CSS文件、123个TypeScript文件、100个PNG图片文件、17个JSON文件和15个SCSS文件。该项目是一个快速开发的管理后台,采用了流行的技术栈,包括Python3、FastAPI、Vue3、vite2和Element Plus1.2(ElementUI)。适合用于个人学习和实践Python、JavaScript、Vue、CSS、TypeScript和HTML的开发技术。
资源推荐
资源详情
资源评论
收起资源包目录
基于FastAPI和Vue3的Python管理后台设计源码 (1545个子文件)
.eslintrc.cjs 1KB
mysqld.cnf 1KB
likeadmin_python.code-workspace 104B
element-plus.41b212f5.css 205KB
element-plus.41b212f5.css 205KB
iconfont.css 79KB
index.d7cfcc94.css 58KB
index.d7cfcc94.css 58KB
u-icon.81bcc25e.css 47KB
u-icon.81bcc25e.css 47KB
vue3-video-play.74881f83.css 27KB
vue3-video-play.74881f83.css 27KB
index.184539bc.css 24KB
index.184539bc.css 24KB
uni.da0b3986.css 15KB
uni.da0b3986.css 15KB
@wangeditor.501cf061.css 14KB
@wangeditor.501cf061.css 14KB
z-paging.7a94f2f9.css 8KB
z-paging.7a94f2f9.css 8KB
u-button.48af928a.css 4KB
u-button.48af928a.css 4KB
dark.css 2KB
tabbar.7dfafcaa.css 2KB
tabbar.7dfafcaa.css 2KB
u-form-item.e55e5c90.css 2KB
u-form-item.e55e5c90.css 2KB
var.css 2KB
index.118ecd42.css 2KB
index.118ecd42.css 2KB
u-popup.3b64afbe.css 2KB
u-popup.3b64afbe.css 2KB
user_data.d05d0eea.css 1KB
user_data.d05d0eea.css 1KB
picker.5357a91f.css 1KB
picker.5357a91f.css 1KB
highlight.5f5db245.css 1KB
highlight.5f5db245.css 1KB
news.cf577e19.css 1KB
news.cf577e19.css 1KB
u-checkbox.c93b1db2.css 1KB
u-checkbox.c93b1db2.css 1KB
oa-phone.8b9170e2.css 931B
oa-phone.8b9170e2.css 931B
u-avatar.dfe1b6cb.css 890B
u-avatar.dfe1b6cb.css 890B
u-avatar-cropper.91a54bb1.css 849B
u-avatar-cropper.91a54bb1.css 849B
nprogress.f5128a35.css 833B
nprogress.f5128a35.css 833B
login.01e52ca6.css 779B
login.01e52ca6.css 779B
u-search.35cfd0ff.css 769B
u-search.35cfd0ff.css 769B
index.2a55a6b3.css 645B
index.2a55a6b3.css 645B
user_set.e61ca628.css 627B
user_set.e61ca628.css 627B
picker.05436930.css 573B
picker.05436930.css 573B
tabbar.75f7a8d8.css 427B
tabbar.75f7a8d8.css 427B
index.be29c95e.css 409B
index.be29c95e.css 409B
collection.c4157529.css 404B
collection.c4157529.css 404B
news-card.9d2e9b2e.css 403B
news-card.9d2e9b2e.css 403B
news_detail.3a6411e3.css 401B
news_detail.3a6411e3.css 401B
index.2ace3fcf.css 397B
index.2ace3fcf.css 397B
u-image.a7813cd9.css 388B
u-image.a7813cd9.css 388B
index.9bce7fe7.css 330B
index.9bce7fe7.css 330B
search.5426327c.css 325B
search.5426327c.css 325B
index.0d25a475.css 284B
index.0d25a475.css 284B
error.be93caf8.css 240B
error.be93caf8.css 240B
menu.a050bec6.css 231B
menu.a050bec6.css 231B
login.343b5475.css 220B
login.343b5475.css 220B
decoration-img.a64d82f7.css 192B
decoration-img.a64d82f7.css 192B
preview.6e931073.css 190B
preview.6e931073.css 190B
content.efb8af20.css 188B
content.9f244b19.css 188B
content.efb8af20.css 188B
content.9f244b19.css 188B
content.75595e09.css 175B
content.75595e09.css 175B
user.4b21793f.css 144B
user.4b21793f.css 144B
content.c9a2d497.css 141B
content.c9a2d497.css 141B
共 1545 条
- 1
- 2
- 3
- 4
- 5
- 6
- 16
资源评论
沐知全栈开发
- 粉丝: 5705
- 资源: 5216
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功