chore(release): v0.5.0-beta 发版准备
主要改动: backend 业务: - feat(error-codes): 统一错误码表 E1011/E1012 拆码 - E1011 AUTH_PASSWORD_WRONG: 本地密码错误 - E1012 AUTH_FIRST_LOGIN_PASSWORD_REQUIRED: 首次登录请先设置密码 - E1015 AUTH_OLD_PASSWORD_REQUIRED: 改密需要旧密码 - E1016 AUTH_OLD_PASSWORD_WRONG: 旧密码错误 - fix(agents): P0 降级放行时,如坐席已注册但未设密码,正确 raise 1012 (修复前会撞 1011 本地密码错误,与场景不符) - feat(approval): 审批模块 (T审批/A审批) - feat(config): approval_template_resource / approval_template_device 配置 - feat(main): /ready, /metrics, /version 端点(K8s 友好) backend 测试: - test(agents): 新增 test_agents.py — 3 个 Fix-4 降级登录测试 - 错误密码拒绝 - 缺密码拒绝 - 正确密码通过 pytest tests/test_agents.py → 3/3 通过 - test(conftest): 模块级 mock + slowapi 限流重置 + UTF-8 patch 解决 Windows pytest GBK 读 .env 失败 + 降级路径无法测试 仓库治理: - chore(gitignore): 排除 .workbuddy/memory/(workbuddy 本地记忆) - chore(docs): 重命名两份 IT 文档(前缀加智能区分版本) 部署与文档: - docs: RELEASE_NOTES_v0.5.0-beta.md / dashboard.html / 需求-发版预览页面 - docs: 部署、架构、PRD、安全、评审报告等同步 v0.5.0-beta - deploy-server: 打包脚本、nginx、docker-compose 版本号 bump 前端 (frontend-h5 / frontend-agent / frontend-admin / frontend-portal): - index.html / package.json 版本号与构建号 bump 自动验收(RELEASE_NOTES L100-104): - [x] pytest tests/test_agents.py -v → 3 passed - [x] grep Bs7ucT backend frontend-h5 frontend-agent → 无输出 - [x] grep AppException(101[123]) backend → 仅 1 处(登录场景 1012) - [ ] npm run build (frontend-h5 / frontend-agent) → 合并后跑 后续: 合并 feature/t-1-t4-merge → main,tag v0.5.0-beta
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
# =============================================================================
|
||||
# IT智能服务台 — 错误码定义
|
||||
# =============================================================================
|
||||
# 说明:统一管理系统错误码,便于前端解析和国际化
|
||||
# 格式:E{模块}{序号}
|
||||
# =============================================================================
|
||||
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class ErrorCode(str, Enum):
|
||||
"""系统错误码枚举"""
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 通用错误 (0xxx)
|
||||
# --------------------------------------------------------------------------
|
||||
SUCCESS = "E0000" # 成功
|
||||
UNKNOWN_ERROR = "E0001" # 未知错误
|
||||
INVALID_PARAMETER = "E0002" # 参数错误
|
||||
MISSING_PARAMETER = "E0003" # 缺少参数
|
||||
NOT_FOUND = "E0004" # 资源不存在
|
||||
UNAUTHORIZED = "E0005" # 未授权
|
||||
FORBIDDEN = "E0006" # 禁止访问
|
||||
INTERNAL_ERROR = "E0007" # 内部错误
|
||||
SERVICE_UNAVAILABLE = "E0008" # 服务不可用
|
||||
TIMEOUT = "E0009" # 请求超时
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 认证相关 (1xxx)
|
||||
# --------------------------------------------------------------------------
|
||||
AUTH_FAILED = "E1001" # 认证失败
|
||||
AUTH_TOKEN_EXPIRED = "E1002" # Token过期
|
||||
AUTH_TOKEN_INVALID = "E1003" # Token无效
|
||||
AUTH_PASSWORD_REQUIRED = "E1012" # 登录:首次登录请先设置密码
|
||||
AUTH_PASSWORD_WRONG = "E1011" # 登录:本地密码错误
|
||||
AUTH_OLD_PASSWORD_REQUIRED = "E1015" # 改密:请输入旧密码(2026-06-15 WB反馈 1012 上下文冲突后拆分)
|
||||
AUTH_OLD_PASSWORD_WRONG = "E1016" # 改密:旧密码错误(2026-06-15 拆分)
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 企微API错误 (2xxx)
|
||||
# --------------------------------------------------------------------------
|
||||
WECOM_API_ERROR = "E2001" # 企微API调用失败
|
||||
WECOM_API_TIMEOUT = "E2002" # 企微API超时
|
||||
WECOM_TOKEN_INVALID = "E2003" # 企微token无效
|
||||
WECOM_USER_NOT_FOUND = "E2004" # 企微用户不存在
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 会话/消息错误 (3xxx)
|
||||
# --------------------------------------------------------------------------
|
||||
CONVERSATION_NOT_FOUND = "E3001" # 会话不存在
|
||||
MESSAGE_NOT_FOUND = "E3002" # 消息不存在
|
||||
MESSAGE_TOO_LONG = "E3003" # 消息过长
|
||||
CONVERSATION_CLOSED = "E3004" # 会话已关闭
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 坐席错误 (4xxx)
|
||||
# --------------------------------------------------------------------------
|
||||
AGENT_NOT_FOUND = "E4001" # 坐席不存在
|
||||
AGENT_OFFLINE = "E4002" # 坐席不在线
|
||||
AGENT_BUSY = "E4003" # 坐席忙碌
|
||||
AGENT_MAX_LOAD = "E4004" # 坐席已达最大接待量
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 审批错误 (5xxx)
|
||||
# --------------------------------------------------------------------------
|
||||
APPROVAL_TEMPLATE_NOT_FOUND = "E5001" # 审批模板不存在
|
||||
APPROVAL_FAILED = "E5002" # 审批提交失败
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 文件上传错误 (6xxx)
|
||||
# --------------------------------------------------------------------------
|
||||
FILE_TOO_LARGE = "E6001" # 文件过大
|
||||
FILE_TYPE_NOT_ALLOWED = "E6002" # 文件类型不允许
|
||||
FILE_UPLOAD_FAILED = "E6003" # 文件上传失败
|
||||
|
||||
|
||||
# 错误码到 HTTP 状态码的映射
|
||||
ERROR_CODE_TO_STATUS = {
|
||||
ErrorCode.SUCCESS: 200,
|
||||
ErrorCode.INVALID_PARAMETER: 400,
|
||||
ErrorCode.MISSING_PARAMETER: 400,
|
||||
ErrorCode.NOT_FOUND: 404,
|
||||
ErrorCode.UNAUTHORIZED: 401,
|
||||
ErrorCode.FORBIDDEN: 403,
|
||||
ErrorCode.INTERNAL_ERROR: 500,
|
||||
ErrorCode.SERVICE_UNAVAILABLE: 503,
|
||||
# 认证
|
||||
ErrorCode.AUTH_FAILED: 401,
|
||||
ErrorCode.AUTH_TOKEN_EXPIRED: 401,
|
||||
ErrorCode.AUTH_TOKEN_INVALID: 401,
|
||||
ErrorCode.AUTH_PASSWORD_REQUIRED: 401,
|
||||
ErrorCode.AUTH_PASSWORD_WRONG: 401,
|
||||
ErrorCode.AUTH_OLD_PASSWORD_REQUIRED: 400,
|
||||
ErrorCode.AUTH_OLD_PASSWORD_WRONG: 400,
|
||||
# 企微
|
||||
ErrorCode.WECOM_API_ERROR: 502,
|
||||
ErrorCode.WECOM_API_TIMEOUT: 504,
|
||||
ErrorCode.WECOM_TOKEN_INVALID: 401,
|
||||
ErrorCode.WECOM_USER_NOT_FOUND: 404,
|
||||
# 会话
|
||||
ErrorCode.CONVERSATION_NOT_FOUND: 404,
|
||||
ErrorCode.MESSAGE_NOT_FOUND: 404,
|
||||
ErrorCode.MESSAGE_TOO_LONG: 400,
|
||||
ErrorCode.CONVERSATION_CLOSED: 400,
|
||||
# 坐席
|
||||
ErrorCode.AGENT_NOT_FOUND: 404,
|
||||
ErrorCode.AGENT_OFFLINE: 400,
|
||||
ErrorCode.AGENT_BUSY: 400,
|
||||
ErrorCode.AGENT_MAX_LOAD: 400,
|
||||
# 审批
|
||||
ErrorCode.APPROVAL_TEMPLATE_NOT_FOUND: 404,
|
||||
ErrorCode.APPROVAL_FAILED: 502,
|
||||
# 文件
|
||||
ErrorCode.FILE_TOO_LARGE: 413,
|
||||
ErrorCode.FILE_TYPE_NOT_ALLOWED: 400,
|
||||
ErrorCode.FILE_UPLOAD_FAILED: 500,
|
||||
}
|
||||
|
||||
|
||||
def get_error_message(code: ErrorCode) -> str:
|
||||
"""获取错误码对应的默认消息"""
|
||||
messages = {
|
||||
ErrorCode.SUCCESS: "操作成功",
|
||||
ErrorCode.UNKNOWN_ERROR: "未知错误,请稍后重试",
|
||||
ErrorCode.INVALID_PARAMETER: "参数错误",
|
||||
ErrorCode.MISSING_PARAMETER: "缺少必要参数",
|
||||
ErrorCode.NOT_FOUND: "资源不存在",
|
||||
ErrorCode.UNAUTHORIZED: "未授权,请先登录",
|
||||
ErrorCode.FORBIDDEN: "禁止访问",
|
||||
ErrorCode.INTERNAL_ERROR: "服务器内部错误",
|
||||
ErrorCode.SERVICE_UNAVAILABLE: "服务暂时不可用",
|
||||
ErrorCode.TIMEOUT: "请求超时",
|
||||
ErrorCode.AUTH_FAILED: "认证失败",
|
||||
ErrorCode.AUTH_TOKEN_EXPIRED: "登录已过期,请重新登录",
|
||||
ErrorCode.AUTH_TOKEN_INVALID: "无效的登录凭证",
|
||||
ErrorCode.AUTH_PASSWORD_REQUIRED: "首次登录请先设置密码",
|
||||
ErrorCode.AUTH_PASSWORD_WRONG: "密码错误",
|
||||
ErrorCode.AUTH_OLD_PASSWORD_REQUIRED: "请输入旧密码",
|
||||
ErrorCode.AUTH_OLD_PASSWORD_WRONG: "旧密码错误",
|
||||
ErrorCode.WECOM_API_ERROR: "企业微信服务异常",
|
||||
ErrorCode.WECOM_API_TIMEOUT: "企业微信服务响应超时",
|
||||
ErrorCode.WECOM_TOKEN_INVALID: "企业微信凭证无效",
|
||||
ErrorCode.WECOM_USER_NOT_FOUND: "企业微信用户不存在",
|
||||
ErrorCode.CONVERSATION_NOT_FOUND: "会话不存在",
|
||||
ErrorCode.MESSAGE_NOT_FOUND: "消息不存在",
|
||||
ErrorCode.MESSAGE_TOO_LONG: "消息内容过长",
|
||||
ErrorCode.CONVERSATION_CLOSED: "会话已结束",
|
||||
ErrorCode.AGENT_NOT_FOUND: "坐席不存在",
|
||||
ErrorCode.AGENT_OFFLINE: "坐席不在线",
|
||||
ErrorCode.AGENT_BUSY: "坐席忙碌中",
|
||||
ErrorCode.AGENT_MAX_LOAD: "坐席已达到最大接待量",
|
||||
ErrorCode.APPROVAL_TEMPLATE_NOT_FOUND: "审批模板不存在",
|
||||
ErrorCode.APPROVAL_FAILED: "审批提交失败",
|
||||
ErrorCode.FILE_TOO_LARGE: "文件过大",
|
||||
ErrorCode.FILE_TYPE_NOT_ALLOWED: "不支持的文件类型",
|
||||
ErrorCode.FILE_UPLOAD_FAILED: "文件上传失败",
|
||||
}
|
||||
return messages.get(code, "未知错误")
|
||||
@@ -0,0 +1,99 @@
|
||||
# =============================================================================
|
||||
# IT智能服务台 — 日志配置
|
||||
# =============================================================================
|
||||
# 说明:统一日志格式,支持 JSON 输出便于日志收集
|
||||
# =============================================================================
|
||||
|
||||
import json
|
||||
import logging
|
||||
import sys
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
|
||||
class JSONFormatter(logging.Formatter):
|
||||
"""JSON 格式日志 formatter"""
|
||||
|
||||
def format(self, record: logging.LogRecord) -> str:
|
||||
"""将日志记录格式化为 JSON"""
|
||||
log_data: dict[str, Any] = {
|
||||
"timestamp": datetime.utcnow().isoformat() + "Z",
|
||||
"level": record.levelname,
|
||||
"logger": record.name,
|
||||
"message": record.getMessage(),
|
||||
"module": record.module,
|
||||
"function": record.funcName,
|
||||
"line": record.lineno,
|
||||
}
|
||||
|
||||
# 添加异常信息
|
||||
if record.exc_info:
|
||||
log_data["exception"] = self.formatException(record.exc_info)
|
||||
|
||||
# 添加额外字段
|
||||
if hasattr(record, "request_id"):
|
||||
log_data["request_id"] = record.request_id
|
||||
if hasattr(record, "user_id"):
|
||||
log_data["user_id"] = record.user_id
|
||||
if hasattr(record, "extra"):
|
||||
log_data.update(record.extra)
|
||||
|
||||
return json.dumps(log_data, ensure_ascii=False)
|
||||
|
||||
|
||||
class PlainFormatter(logging.Formatter):
|
||||
"""普通格式日志 formatter(开发环境使用)"""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
fmt="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
|
||||
|
||||
def setup_logging(level: str = "INFO", json_format: bool = False) -> None:
|
||||
"""配置日志系统
|
||||
|
||||
Args:
|
||||
level: 日志级别 (DEBUG, INFO, WARNING, ERROR, CRITICAL)
|
||||
json_format: 是否使用 JSON 格式输出
|
||||
"""
|
||||
log_level = getattr(logging, level.upper(), logging.INFO)
|
||||
|
||||
# 获取 root logger
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.setLevel(log_level)
|
||||
|
||||
# 清除现有 handlers
|
||||
for handler in root_logger.handlers[:]:
|
||||
root_logger.removeHandler(handler)
|
||||
|
||||
# 创建 console handler
|
||||
console_handler = logging.StreamHandler(sys.stdout)
|
||||
console_handler.setLevel(log_level)
|
||||
|
||||
# 设置 formatter
|
||||
if json_format:
|
||||
formatter = JSONFormatter()
|
||||
else:
|
||||
formatter = PlainFormatter()
|
||||
|
||||
console_handler.setFormatter(formatter)
|
||||
root_logger.addHandler(console_handler)
|
||||
|
||||
# 设置第三方库日志级别
|
||||
logging.getLogger("uvicorn").setLevel(logging.WARNING)
|
||||
logging.getLogger("fastapi").setLevel(logging.WARNING)
|
||||
logging.getLogger("sqlalchemy.engine").setLevel(logging.WARNING)
|
||||
|
||||
|
||||
def get_logger(name: str) -> logging.Logger:
|
||||
"""获取 logger 实例
|
||||
|
||||
Args:
|
||||
name: logger 名称,通常使用 __name__
|
||||
|
||||
Returns:
|
||||
Logger 实例
|
||||
"""
|
||||
return logging.getLogger(name)
|
||||
Reference in New Issue
Block a user