- 新增员工岗位(InquiryStaffRole)实体、服务与后台页面,数据范围默认按岗位,看板由「仅本人」切换为岗位范围,补充业务负责人与角色说明迁移 - 新增企微群 webhook 通道、access_gate/staff_roles 鉴权与智价通/双 BOT 回复配置 - 报价回执增加去重防抖(quote_receipt_debounce),询价原文落库并补单测 - 同义词补陆运类型修正与地名通配 v2,更新种子生成脚本 - 空运/陆运/海运询价流程、报价单按需生成、转人工与多段处理调整 - 后台工单需求字段、BOT 显示名、站点标题与看板配套更新
135 lines
4.7 KiB
Python
135 lines
4.7 KiB
Python
"""
|
|
私聊询价销售准入门禁。
|
|
|
|
产品口径(2026-11-25):
|
|
- 只有「销售」岗位可以发起询价;非销售回复固定话术:
|
|
「您没有询价权限,如需询价,请咨询IT开通权限。」
|
|
- 员工可同时是销售 / 航线 / 产品;只要岗位集合含 sales 即放行。
|
|
- 群聊不在这里拦:群里产品/航线要报价、补字段、确认成交,
|
|
继续由 group_collab 的「本单销售 / 群成员」规则校验。
|
|
|
|
身份来源仍是入站 sender_id(企微 userid);岗位查主账 /inquiry/agent/staff/byWecom。
|
|
副作用:首次查询会打主账 HTTP;结果进程内缓存 5 分钟;被拒时写一条 outbox。
|
|
线程:在 inbox 工作线程调用,不阻塞回调线程。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import threading
|
|
import time
|
|
from typing import Any
|
|
|
|
from agent.channel.queue import get_message_store
|
|
from agent.config import get_settings
|
|
from agent.channel.wecom.models import InboundMessage
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# 命中缓存 5 分钟;员工岗位变更由后台维护,5 分钟内可接受。
|
|
_HIT_TTL_SECONDS = 300
|
|
# 未查到员工时短缓存,避免非员工连续发消息反复打主账。
|
|
_MISS_TTL_SECONDS = 60
|
|
|
|
_cache: dict[str, tuple[float, dict[str, Any]]] = {}
|
|
_cache_lock = threading.Lock()
|
|
|
|
|
|
def allow_inquiry(message: InboundMessage) -> bool:
|
|
"""
|
|
私聊入口是否允许继续询价。
|
|
|
|
返回 False 时已向发送人写入拒绝话术,调用方直接结束本次分发。
|
|
开关关闭/群聊/无 sender 时一律放行,保持既有行为。
|
|
"""
|
|
settings = get_settings()
|
|
if not bool(getattr(settings, "agent_sales_gate_enabled", False)):
|
|
return True
|
|
if (message.chat_type or "") == "group":
|
|
return True
|
|
if not message.has_sender():
|
|
return True
|
|
|
|
sender = (message.sender_id or "").strip()
|
|
if not sender:
|
|
return True
|
|
if _is_bypass(sender, getattr(settings, "agent_sales_gate_bypass", "")):
|
|
logger.info("sales gate bypass sender=%s", sender)
|
|
return True
|
|
|
|
staff = _load_staff(sender)
|
|
if staff and _has_sales_role(staff) and _is_active(staff):
|
|
return True
|
|
|
|
reason = "staff_not_found" if not staff else (
|
|
"staff_disabled" if not _is_active(staff) else "not_sales"
|
|
)
|
|
if not staff and bool(getattr(settings, "agent_sales_gate_fail_open", False)):
|
|
# 主账不可用/查不到人时按配置放行,避免一次接口故障挡住全部销售。
|
|
logger.warning("sales gate fail-open sender=%s reason=%s", sender, reason)
|
|
return True
|
|
|
|
logger.info("sales gate deny sender=%s reason=%s", sender, reason)
|
|
_reply_no_permission(message, getattr(settings, "agent_sales_gate_reply", ""))
|
|
return False
|
|
|
|
|
|
def _is_bypass(sender: str, raw: str) -> bool:
|
|
for part in str(raw or "").split(","):
|
|
if part.strip() and part.strip() == sender:
|
|
return True
|
|
return False
|
|
|
|
|
|
def _has_sales_role(staff: dict[str, Any]) -> bool:
|
|
from agent.policy.staff_roles import has_role
|
|
|
|
return has_role(staff, "sales")
|
|
|
|
|
|
def _is_active(staff: dict[str, Any]) -> bool:
|
|
status = str(staff.get("status") or "active").strip().lower()
|
|
return status in {"", "active"}
|
|
|
|
|
|
def _load_staff(sender: str) -> dict[str, Any]:
|
|
now = time.time()
|
|
with _cache_lock:
|
|
hit = _cache.get(sender)
|
|
if hit is not None:
|
|
expires_at, staff = hit
|
|
if now < expires_at:
|
|
return staff
|
|
try:
|
|
from agent.ledger.http_ledger import HttpLedger
|
|
|
|
staff = HttpLedger().get_staff_by_wecom_id(wecom_id=sender)
|
|
except Exception: # noqa: BLE001 - 门禁查询失败按未查到处理,由 fail_open 决定
|
|
logger.warning("sales gate 查询员工失败 sender=%s", sender, exc_info=True)
|
|
staff = {}
|
|
ttl = _HIT_TTL_SECONDS if staff else _MISS_TTL_SECONDS
|
|
with _cache_lock:
|
|
_cache[sender] = (now + ttl, staff)
|
|
return staff
|
|
|
|
|
|
def _reply_no_permission(message: InboundMessage, text: str) -> None:
|
|
body = str(text or "").strip() or "您没有询价权限,如需询价,请咨询IT开通权限。"
|
|
try:
|
|
store = get_message_store()
|
|
ok, info = store.enqueue_outbound(
|
|
touser=message.sender_id,
|
|
content=body,
|
|
dedupe_key=f"inq:no-sales:{message.message_id or message.sender_id}",
|
|
payload={},
|
|
)
|
|
logger.info("sales gate reply ok=%s info=%s", ok, info)
|
|
except Exception: # noqa: BLE001 - 出站失败不能把分发线程打挂
|
|
logger.warning("sales gate 回复失败 sender=%s", message.sender_id, exc_info=True)
|
|
|
|
|
|
def clear_cache() -> None:
|
|
"""单测/联调用:清空岗位缓存。"""
|
|
with _cache_lock:
|
|
_cache.clear()
|