""" 登录日志模型模块 ============================================================= 提供登录日志管理的数据模型定义,用于记录用户登录、退出等操作日志。 """ from application.models.base_db import BaseDB from application.models.base_model import BaseModel from config import DB_PREFIX from extensions import db from utils.case_converter import convert_camel_to_snake # ============================================================ # 登录日志模型 # ============================================================ class LoginLog(BaseModel, BaseDB): """ 登录日志模型 用于记录用户登录和退出操作的详细信息,包括:登录账号、日志类型、 来源、请求信息、IP地址、归属地、设备信息、请求耗时、操作状态等。 Attributes: id: 主键ID(继承自 BaseModel) username: 登录账号(String(50),必填) log_type: 日志类型(Integer,1-登录,2-退出,必填) source: 登录来源(Integer,0-后台,1-前台,必填) method: 请求方法(String(10),必填) request_method: 请求方式(String(255),必填) url: 请求URL(String(500),必填) param: 请求参数(Text,可选) result: 返回结果(Text,必填) ip: IP地址(String(50),必填) location: IP归属地(String(100),必填) os: 操作系统(String(100),必填) browser: 浏览器(String(100),必填) consume_time: 请求耗时(Integer,必填) status: 操作状态(Integer,0-正常,1-异常,必填) error: 错误信息(Text,可选) """ # ============================================================ # 表配置 # ============================================================ # 设置表名 __tablename__ = DB_PREFIX + "login_log" # ============================================================ # 字段定义 # ============================================================ # 登录账号 username = db.Column( db.String(50), nullable=False, comment="登录账号" ) # 日志类型:1-登录,2-退出 log_type = db.Column( db.Integer, nullable=False, comment="日志类型:1-登录,2-退出" ) # 登录来源:0-后台,1-前台 source = db.Column( db.Integer, nullable=False, comment="登录来源:0-后台,1-前台" ) # 请求方法(HTTP方法,如POST/GET) method = db.Column( db.String(10), nullable=False, comment="请求方法" ) # 请求方式(视图方法路径,如application.views.login.logout) request_method = db.Column( db.String(255), nullable=False, comment="请求方式" ) # 请求URL url = db.Column( db.String(500), nullable=False, comment="请求URL" ) # 请求参数 param = db.Column( db.Text, nullable=True, comment="请求参数" ) # 返回结果 result = db.Column( db.Text, nullable=False, comment="返回结果" ) # IP地址 ip = db.Column( db.String(50), nullable=False, comment="IP地址" ) # IP归属地 location = db.Column( db.String(100), nullable=False, comment="IP归属地" ) # 操作系统 os = db.Column( db.String(100), nullable=False, comment="操作系统" ) # 浏览器 browser = db.Column( db.String(100), nullable=False, comment="浏览器" ) # 请求耗时(毫秒) consume_time = db.Column( db.Integer, nullable=False, comment="请求耗时" ) # 操作状态:0-正常,1-异常 status = db.Column( db.Integer, nullable=False, comment="操作状态:0-正常,1-异常" ) # 错误信息 error = db.Column( db.Text, nullable=True, comment="错误信息" ) # ============================================================ # 常量定义 # ============================================================ class LogType: """日志类型常量""" LOGIN = 1 # 登录 LOGOUT = 2 # 退出 class Source: """登录来源常量""" ADMIN = 0 # 后台 FRONT = 1 # 前台 class Status: """操作状态常量""" NORMAL = 0 # 正常 ERROR = 1 # 异常 # ============================================================ # 魔法方法 # ============================================================ def __repr__(self): """ 返回模型的字符串表示 Returns: str: 格式为 "登录日志:{username} - {log_type}" 的字符串 """ log_type_name = "登录" if self.log_type == self.LogType.LOGIN else "退出" return '登录日志:{} - {}'.format(self.username, log_type_name) def __str__(self): """ 返回模型的字符串表示 Returns: str: 格式为 "登录日志:{username} - {log_type}" 的字符串 """ return self.__repr__()