fix: wx_username 改为基于昵称哈希生成,避免槽位复用导致不同客户消息混合
This commit is contained in:
+27
-5
@@ -680,10 +680,27 @@ def get_variant(name: str):
|
||||
raise KeyError(f"variant not found: {name}")
|
||||
|
||||
|
||||
def make_wx_username(sp_id: int, nick: str, seen: set, is_group: bool = False) -> str:
|
||||
"""基于销售ID和昵称哈希生成稳定的 wx_username,避免槽位复用导致消息混合。"""
|
||||
sp_wx = SALESPERSONS[sp_id]["wx_account"]
|
||||
h = hashlib.md5(f"{sp_wx}_{nick}".encode()).hexdigest()[:12]
|
||||
base = f"{sp_wx}_{h}"
|
||||
if is_group:
|
||||
base += "@chatroom"
|
||||
# 若哈希冲突,追加序号
|
||||
if base in seen:
|
||||
idx = 2
|
||||
while f"{base}_{idx}" in seen:
|
||||
idx += 1
|
||||
base = f"{base}_{idx}"
|
||||
return base
|
||||
|
||||
|
||||
def generate_contacts_for_salesperson(sp_id: int):
|
||||
"""返回 [(nickname, remark, is_group, wx_username, scenario_or_template)]"""
|
||||
assignments = SALESPERSON_ASSIGNMENTS.get(sp_id, [])
|
||||
result = []
|
||||
seen_wx = set()
|
||||
for i, (kind, nick_override, remark_override) in enumerate(assignments):
|
||||
if kind.startswith("scenario_"):
|
||||
idx = int(kind.split("_")[1])
|
||||
@@ -691,14 +708,16 @@ def generate_contacts_for_salesperson(sp_id: int):
|
||||
nick = nick_override or tpl["contact"]["nickname"]
|
||||
remark = remark_override or tpl["contact"].get("remark", "")
|
||||
is_group = False
|
||||
wx_username = f"{SALESPERSONS[sp_id]['wx_account']}_contact_{i+1}"
|
||||
wx_username = make_wx_username(sp_id, nick, seen_wx)
|
||||
seen_wx.add(wx_username)
|
||||
result.append((nick, remark, is_group, wx_username, tpl))
|
||||
elif kind.startswith("variant_"):
|
||||
tpl = get_variant(kind[8:])
|
||||
nick = nick_override or tpl["contact"]["nickname"]
|
||||
remark = remark_override or tpl["contact"].get("remark", "")
|
||||
is_group = False
|
||||
wx_username = f"{SALESPERSONS[sp_id]['wx_account']}_contact_{i+1}"
|
||||
wx_username = make_wx_username(sp_id, nick, seen_wx)
|
||||
seen_wx.add(wx_username)
|
||||
result.append((nick, remark, is_group, wx_username, tpl))
|
||||
elif kind.startswith("diet_"):
|
||||
idx = int(kind.split("_")[1])
|
||||
@@ -706,7 +725,8 @@ def generate_contacts_for_salesperson(sp_id: int):
|
||||
nick = nick_override or tpl["contact"]["nickname"]
|
||||
remark = remark_override or tpl["contact"].get("remark", "")
|
||||
is_group = False
|
||||
wx_username = f"{SALESPERSONS[sp_id]['wx_account']}_contact_{i+1}"
|
||||
wx_username = make_wx_username(sp_id, nick, seen_wx)
|
||||
seen_wx.add(wx_username)
|
||||
result.append((nick, remark, is_group, wx_username, tpl))
|
||||
elif kind == "non_customer":
|
||||
nick = nick_override
|
||||
@@ -721,7 +741,8 @@ def generate_contacts_for_salesperson(sp_id: int):
|
||||
tpl = NON_CUSTOMER_TEMPLATES[i % len(NON_CUSTOMER_TEMPLATES)]
|
||||
nick = tpl["nickname"]
|
||||
is_group = False
|
||||
wx_username = f"{SALESPERSONS[sp_id]['wx_account']}_contact_{i+1}"
|
||||
wx_username = make_wx_username(sp_id, nick, seen_wx)
|
||||
seen_wx.add(wx_username)
|
||||
result.append((nick, remark, is_group, wx_username, tpl))
|
||||
elif kind == "group":
|
||||
nick = nick_override
|
||||
@@ -735,7 +756,8 @@ def generate_contacts_for_salesperson(sp_id: int):
|
||||
tpl = GROUP_TEMPLATES[i % len(GROUP_TEMPLATES)]
|
||||
nick = tpl["nickname"]
|
||||
is_group = True
|
||||
wx_username = f"{SALESPERSONS[sp_id]['wx_account']}_group_{i+1}@chatroom"
|
||||
wx_username = make_wx_username(sp_id, nick, seen_wx, is_group=True)
|
||||
seen_wx.add(wx_username)
|
||||
result.append((nick, remark, is_group, wx_username, tpl))
|
||||
return result
|
||||
|
||||
|
||||
Reference in New Issue
Block a user