[patch] 修正京东丢失客服列表功能
This commit is contained in:
@@ -210,6 +210,80 @@ class FixJdCookie:
|
|||||||
}
|
}
|
||||||
await ws.send(json.dumps(message))
|
await ws.send(json.dumps(message))
|
||||||
|
|
||||||
|
async def get_all_customer(self, ws, aid, pin_zj):
|
||||||
|
"""异步获取商家组织架构(客服列表)"""
|
||||||
|
id_str = hashlib.md5(str(int(time.time() * 1000) + 300).encode()).hexdigest()
|
||||||
|
message = {
|
||||||
|
"id": id_str,
|
||||||
|
"aid": aid,
|
||||||
|
"lang": "zh_CN",
|
||||||
|
"timestamp": int(time.time() * 1000),
|
||||||
|
"from": {
|
||||||
|
"app": "im.waiter", "pin": pin_zj, "art": "customerGroupMsg", "clientType": "comet"
|
||||||
|
},
|
||||||
|
"type": "org_new",
|
||||||
|
"body": {"param": pin_zj, "paramtype": "ByPin"}
|
||||||
|
}
|
||||||
|
await ws.send(json.dumps(message))
|
||||||
|
while True:
|
||||||
|
response = await ws.recv()
|
||||||
|
data = json.loads(response)
|
||||||
|
if data.get("id") == id_str and data.get("body", {}).get("groupname") == "商家组织架构":
|
||||||
|
group = data.get("body", {}).get("group")
|
||||||
|
if group:
|
||||||
|
try:
|
||||||
|
# 与旧实现保持一致的数据路径
|
||||||
|
self.customer_list = group[0].get("group")[0].get("users")
|
||||||
|
except Exception:
|
||||||
|
self.customer_list = []
|
||||||
|
return
|
||||||
|
|
||||||
|
async def send_staff_list_to_backend(self, store_id):
|
||||||
|
"""发送客服列表到后端(通过统一后端连接)"""
|
||||||
|
try:
|
||||||
|
if not hasattr(self, 'customer_list') or not self.customer_list:
|
||||||
|
self._log("⚠️ 客服列表为空", "WARNING")
|
||||||
|
return False
|
||||||
|
|
||||||
|
# 转换客服数据格式
|
||||||
|
staff_infos = []
|
||||||
|
for staff in self.customer_list:
|
||||||
|
staff_info = {
|
||||||
|
"staff_id": staff.get("pin", ""),
|
||||||
|
"name": staff.get("nickname", ""),
|
||||||
|
"status": staff.get("status", 0),
|
||||||
|
"department": staff.get("department", ""),
|
||||||
|
"online": staff.get("online", True)
|
||||||
|
}
|
||||||
|
staff_infos.append(staff_info)
|
||||||
|
|
||||||
|
# 通过后端统一连接发送
|
||||||
|
try:
|
||||||
|
from WebSocket.backend_singleton import get_backend_client
|
||||||
|
backend_client = get_backend_client()
|
||||||
|
if not backend_client or not getattr(backend_client, 'is_connected', False):
|
||||||
|
self._log("❌ 统一后端连接不可用", "ERROR")
|
||||||
|
return False
|
||||||
|
|
||||||
|
message = {
|
||||||
|
"type": "staff_list",
|
||||||
|
"content": "客服列表更新",
|
||||||
|
"data": {
|
||||||
|
"staff_list": staff_infos,
|
||||||
|
"total_count": len(staff_infos)
|
||||||
|
},
|
||||||
|
"store_id": str(store_id)
|
||||||
|
}
|
||||||
|
backend_client.send_message(message)
|
||||||
|
self._log(f"✅ 成功发送客服列表到后端,共 {len(staff_infos)} 个客服", "SUCCESS")
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
self._log(f"❌ 发送客服列表到后端失败: {e}", "ERROR")
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
self._log(f"❌ 发送客服列表到后端异常: {e}", "ERROR")
|
||||||
|
return False
|
||||||
|
|
||||||
async def transfer_customer(self, ws, aid, pin, pin_zj, chat_name):
|
async def transfer_customer(self, ws, aid, pin, pin_zj, chat_name):
|
||||||
"""异步客服转接 在发送的消息为客服转接的关键词的时候"""
|
"""异步客服转接 在发送的消息为客服转接的关键词的时候"""
|
||||||
message = {
|
message = {
|
||||||
@@ -506,6 +580,13 @@ class FixJdCookie:
|
|||||||
# print(f"✅ 连接状态: open={ws.open}, closed={ws.closed}")
|
# print(f"✅ 连接状态: open={ws.open}, closed={ws.closed}")
|
||||||
print(f"🖥️ 服务端地址: {ws.remote_address}")
|
print(f"🖥️ 服务端地址: {ws.remote_address}")
|
||||||
|
|
||||||
|
# 连接成功后,获取并上报一次客服列表(最小改动恢复)
|
||||||
|
try:
|
||||||
|
await self.get_all_customer(ws, aid, pin_zj)
|
||||||
|
await self.send_staff_list_to_backend(store.get('id', ''))
|
||||||
|
except Exception as e:
|
||||||
|
self._log(f"❌ 获取或发送客服列表失败: {e}", "ERROR")
|
||||||
|
|
||||||
# --- 注册连接信息到全局管理
|
# --- 注册连接信息到全局管理
|
||||||
shop_key = f"京东:{store['id']}"
|
shop_key = f"京东:{store['id']}"
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import json # 用于将令牌保存为 JSON 格式
|
|||||||
|
|
||||||
# 后端服务器配置
|
# 后端服务器配置
|
||||||
# BACKEND_HOST = "192.168.5.233"
|
# BACKEND_HOST = "192.168.5.233"
|
||||||
BACKEND_HOST = "192.168.5.12"
|
BACKEND_HOST = "192.168.5.106"
|
||||||
# BACKEND_HOST = "shuidrop.com"
|
# BACKEND_HOST = "shuidrop.com"
|
||||||
# BACKEND_HOST = "test.shuidrop.com"
|
# BACKEND_HOST = "test.shuidrop.com"
|
||||||
BACKEND_PORT = "8000"
|
BACKEND_PORT = "8000"
|
||||||
|
|||||||
Reference in New Issue
Block a user