refactor: P4c 最终批次-5 auth 模块(login/index 合并)
【改造】login(data) 入参数据/返回 dict/raise BizError(验证码/限流/密码校验); _record_login 兼容 dict result;logout 修正误导入(auth→log service) 【修复】auth router login+index 合并的 docstring/import 结构混乱 【验收】flake8 零告警;应用启动;登录表单校验运行时正常;12 passed Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
6bd4951f95
commit
b9ce9dc510
+53
-55
@@ -43,30 +43,33 @@
|
||||
登录认证控制器模块
|
||||
=============================================================
|
||||
|
||||
提供系统登录相关的API接口,包括:
|
||||
- 用户登录认证
|
||||
- 验证码生成
|
||||
|
||||
所属包: application.views
|
||||
依赖模块: application.services.logins - 登录业务逻辑层
|
||||
utils.captcha - 验证码生成工具
|
||||
提供登录、验证码、退出登录以及用户自助信息接口。
|
||||
"""
|
||||
|
||||
from flask import Blueprint
|
||||
|
||||
from sqlalchemy import and_
|
||||
from application.middleware.check_demo import check_demo
|
||||
from src.modules.auth import service as login
|
||||
|
||||
login_blue = Blueprint('login', __name__, url_prefix='/')
|
||||
from src.modules.log import service as login_log
|
||||
from src.modules.system.user.models import User
|
||||
from src.modules.system.menu import service as menu
|
||||
from src.modules.system.user import service as user
|
||||
from src.common.utils.request import parse_request_body
|
||||
from src.core import response as R
|
||||
from src.common.utils.captcha import generate_captcha
|
||||
from src.core.jwt import add_token_to_blacklist, get_access_token
|
||||
from src.common.utils.rich_text import get_file_url
|
||||
from src.core.security import get_user_id
|
||||
|
||||
|
||||
login_blue = Blueprint("login", __name__, url_prefix="/")
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 用户登录
|
||||
# ============================================================
|
||||
|
||||
@login_blue.route('/login', methods=["POST"])
|
||||
|
||||
@login_blue.route("/login", methods=["POST"])
|
||||
def user_login():
|
||||
"""
|
||||
用户登录认证
|
||||
@@ -77,15 +80,18 @@ def user_login():
|
||||
Returns:
|
||||
JSON: 登录结果,成功时返回 token,失败时返回错误信息
|
||||
"""
|
||||
result = login.login()
|
||||
return result
|
||||
json_data, err = parse_request_body()
|
||||
if err:
|
||||
return R.failed(msg=err)
|
||||
return R.ok(msg="登录成功", data=login.login(json_data))
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 验证码获取
|
||||
# ============================================================
|
||||
|
||||
@login_blue.route('/captcha', methods=["GET"])
|
||||
|
||||
@login_blue.route("/captcha", methods=["GET"])
|
||||
def captcha():
|
||||
"""
|
||||
获取图形验证码
|
||||
@@ -100,8 +106,8 @@ def captcha():
|
||||
"""
|
||||
result = generate_captcha()
|
||||
|
||||
id_key = result['idKey'] # 验证码唯一标识
|
||||
data = result['data'] # 验证码图片 Base64 数据
|
||||
id_key = result["idKey"] # 验证码唯一标识
|
||||
data = result["data"] # 验证码图片 Base64 数据
|
||||
|
||||
# 拼接完整的图片 DataURL 格式
|
||||
img_str = "data:image/png;base64," + data
|
||||
@@ -113,7 +119,8 @@ def captcha():
|
||||
# 退出登录
|
||||
# ============================================================
|
||||
|
||||
@login_blue.route('/logout', methods=["GET"])
|
||||
|
||||
@login_blue.route("/logout", methods=["GET"])
|
||||
def logout():
|
||||
"""
|
||||
用户退出登录
|
||||
@@ -134,18 +141,17 @@ def logout():
|
||||
import time
|
||||
from flask import g, request
|
||||
from src.modules.log.models import LoginLog
|
||||
from src.modules.auth import service as login_log
|
||||
from src.core.security import get_username
|
||||
from src.common.utils.ip import get_client_ip
|
||||
from src.common.utils.ip2region import get_ip_location
|
||||
from src.common.utils.user_agent import parse_user_agent
|
||||
|
||||
username = get_username() or '未知'
|
||||
username = get_username() or "未知"
|
||||
client_ip = get_client_ip()
|
||||
ua_info = parse_user_agent()
|
||||
|
||||
# 请求耗时
|
||||
start_time = getattr(g, 'request_start_time', None)
|
||||
start_time = getattr(g, "request_start_time", None)
|
||||
consume_time = int((time.time() - start_time) * 1000) if start_time else 0
|
||||
|
||||
login_log.record_login_log(
|
||||
@@ -154,25 +160,25 @@ def logout():
|
||||
status=LoginLog.Status.NORMAL,
|
||||
log_type=LoginLog.LogType.LOGOUT,
|
||||
method=request.method,
|
||||
request_method=request.endpoint or '',
|
||||
request_method=request.endpoint or "",
|
||||
url=request.path,
|
||||
param='',
|
||||
result='退出成功',
|
||||
param="",
|
||||
result="退出成功",
|
||||
ip=client_ip,
|
||||
location=get_ip_location(client_ip),
|
||||
os=ua_info.get('os', '未知'),
|
||||
browser=ua_info.get('browser', '未知'),
|
||||
os=ua_info.get("os", "未知"),
|
||||
browser=ua_info.get("browser", "未知"),
|
||||
consume_time=consume_time,
|
||||
error='',
|
||||
error="",
|
||||
)
|
||||
except Exception as e:
|
||||
import logging
|
||||
|
||||
logging.getLogger(__name__).warning(f"记录退出日志失败: {str(e)}")
|
||||
|
||||
return R.ok(msg="注销成功")
|
||||
|
||||
|
||||
|
||||
"""
|
||||
首页控制器模块
|
||||
=============================================================
|
||||
@@ -189,26 +195,17 @@ def logout():
|
||||
application.services.user - 用户业务逻辑层
|
||||
application.models.user - 用户数据模型
|
||||
"""
|
||||
from sqlalchemy import and_
|
||||
|
||||
from flask import Blueprint
|
||||
|
||||
from application.middleware.check_demo import check_demo
|
||||
from src.modules.system.user.models import User
|
||||
from src.modules.system.menu import service as menu
|
||||
from src.modules.system.user import service as user
|
||||
|
||||
index_blue = Blueprint('index', __name__, url_prefix='/index')
|
||||
from src.core import response as R
|
||||
from src.common.utils.rich_text import get_file_url
|
||||
from src.core.security import get_user_id
|
||||
index_blue = Blueprint("index", __name__, url_prefix="/index")
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 系统菜单获取
|
||||
# ============================================================
|
||||
|
||||
@index_blue.route('/getMenu', methods=["GET"])
|
||||
|
||||
@index_blue.route("/getMenu", methods=["GET"])
|
||||
def get_menus():
|
||||
"""
|
||||
获取系统菜单列表
|
||||
@@ -227,7 +224,8 @@ def get_menus():
|
||||
# 用户信息获取
|
||||
# ============================================================
|
||||
|
||||
@index_blue.route('/getUser', methods=["GET"])
|
||||
|
||||
@index_blue.route("/getUser", methods=["GET"])
|
||||
def get_user():
|
||||
"""
|
||||
获取当前用户信息
|
||||
@@ -241,9 +239,7 @@ def get_user():
|
||||
JSON: 用户详细信息
|
||||
"""
|
||||
# 查询用户信息
|
||||
current_user = User.query.filter(
|
||||
and_(User.id == get_user_id(), User.is_delete == 0)
|
||||
).first()
|
||||
current_user = User.query.filter(and_(User.id == get_user_id(), User.is_delete == 0)).first()
|
||||
|
||||
if not current_user:
|
||||
return R.failed("用户不存在")
|
||||
@@ -253,15 +249,15 @@ def get_user():
|
||||
|
||||
# 构建用户信息
|
||||
info = {
|
||||
'realname': current_user.realname,
|
||||
'avatar': get_file_url(current_user.avatar),
|
||||
'gender': current_user.gender,
|
||||
'mobile': current_user.mobile,
|
||||
'email': current_user.email,
|
||||
'intro': current_user.intro,
|
||||
'address': current_user.address,
|
||||
'roles': [],
|
||||
'permissions': permissions,
|
||||
"realname": current_user.realname,
|
||||
"avatar": get_file_url(current_user.avatar),
|
||||
"gender": current_user.gender,
|
||||
"mobile": current_user.mobile,
|
||||
"email": current_user.email,
|
||||
"intro": current_user.intro,
|
||||
"address": current_user.address,
|
||||
"roles": [],
|
||||
"permissions": permissions,
|
||||
}
|
||||
|
||||
return R.ok(data=info)
|
||||
@@ -271,7 +267,8 @@ def get_user():
|
||||
# 个人中心信息更新
|
||||
# ============================================================
|
||||
|
||||
@index_blue.route('/userInfo', methods=["PUT"])
|
||||
|
||||
@index_blue.route("/userInfo", methods=["PUT"])
|
||||
@check_demo
|
||||
def update_user_info():
|
||||
"""
|
||||
@@ -291,7 +288,8 @@ def update_user_info():
|
||||
# 密码更新
|
||||
# ============================================================
|
||||
|
||||
@index_blue.route('/updatePwd', methods=["PUT"])
|
||||
|
||||
@index_blue.route("/updatePwd", methods=["PUT"])
|
||||
@check_demo
|
||||
def update_pwd():
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user