feat: 客服后台话术配置接口与会话管理菜单
This commit is contained in:
237
config/views.py
237
config/views.py
@@ -3396,6 +3396,12 @@ MINIAPP_SCRIPT_DEFAULTS = {
|
||||
'confirm_text': '确认',
|
||||
'cancel_text': '取消',
|
||||
},
|
||||
'merchant_dispatch_confirm': {
|
||||
'title': '确认派单',
|
||||
'content': '确认向该打手派发此订单?',
|
||||
'confirm_text': '确认',
|
||||
'cancel_text': '取消',
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -3462,18 +3468,221 @@ class HuashuMatchView(APIView):
|
||||
).order_by('-sort_order', 'id')
|
||||
text_lower = user_text.lower()
|
||||
for rule in rules:
|
||||
kw = (rule.keyword or '').strip()
|
||||
if not kw:
|
||||
kw_raw = (rule.keyword or '').strip()
|
||||
if not kw_raw:
|
||||
continue
|
||||
if kw.lower() in text_lower or kw in user_text:
|
||||
reply_type = rule.reply_type or MiniappScriptAutoReply.REPLY_TEXT
|
||||
return Response({
|
||||
'code': 200,
|
||||
'msg': 'success',
|
||||
'data': {
|
||||
'matched': True,
|
||||
'content': rule.reply_content or '',
|
||||
'reply_type': reply_type,
|
||||
},
|
||||
})
|
||||
return Response({'code': 200, 'msg': 'success', 'data': {'matched': False, 'content': ''}})
|
||||
keywords = [k.strip() for k in kw_raw.split(',') if k.strip()]
|
||||
for kw in keywords:
|
||||
if kw.lower() in text_lower or kw in user_text:
|
||||
reply_type = rule.reply_type or MiniappScriptAutoReply.REPLY_TEXT
|
||||
return Response({
|
||||
'code': 200,
|
||||
'msg': 'success',
|
||||
'data': {
|
||||
'matched': True,
|
||||
'content': rule.reply_content or '',
|
||||
'reply_type': reply_type,
|
||||
},
|
||||
})
|
||||
return Response({'code': 200, 'msg': 'success', 'data': {'matched': False, 'content': ''}})
|
||||
|
||||
|
||||
# ---------- 后台:客服话术配置(/houtai/hthqhs、/houtai/htxghs) ----------
|
||||
|
||||
SCRIPT_CONFIG_PERMS = ('kfhs666', '8080a')
|
||||
SCRIPT_SCENE_KEYS = ('chat_image_confirm', 'cs_welcome', 'merchant_dispatch_confirm')
|
||||
|
||||
|
||||
def _has_script_config_perm(permissions):
|
||||
return any(code in permissions for code in SCRIPT_CONFIG_PERMS)
|
||||
|
||||
|
||||
def _script_scene_item_type(scene_key):
|
||||
return 'text_display' if scene_key == 'cs_welcome' else 'confirm_modal'
|
||||
|
||||
|
||||
def _serialize_script_scene(row):
|
||||
return {
|
||||
'id': row.id,
|
||||
'scene_key': row.scene_key,
|
||||
'item_type': _script_scene_item_type(row.scene_key),
|
||||
'title': row.title or '',
|
||||
'content': row.content or '',
|
||||
'confirm_text': row.confirm_text or '',
|
||||
'cancel_text': row.cancel_text or '',
|
||||
'is_active': bool(row.enabled),
|
||||
}
|
||||
|
||||
|
||||
def _serialize_script_auto_reply(row):
|
||||
keywords = [k.strip() for k in (row.keyword or '').split(',') if k.strip()]
|
||||
return {
|
||||
'id': row.id,
|
||||
'scene_key': 'cs_auto_reply',
|
||||
'item_type': 'auto_reply',
|
||||
'title': '',
|
||||
'keywords': keywords,
|
||||
'match_mode': 'contains',
|
||||
'priority': row.sort_order or 0,
|
||||
'content': row.reply_content or '',
|
||||
'is_active': bool(row.enabled),
|
||||
}
|
||||
|
||||
|
||||
class MiniappScriptHoutaiListView(APIView):
|
||||
"""POST /houtai/hthqhs 后台获取话术与自动回复配置"""
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def post(self, request):
|
||||
from backend.utils import verify_kefu_permission
|
||||
|
||||
username = request.data.get('username')
|
||||
kefu, permissions = verify_kefu_permission(request, username)
|
||||
if kefu is None:
|
||||
return Response({'code': 403, 'msg': '身份验证失败,请检查登录状态'})
|
||||
if not _has_script_config_perm(permissions):
|
||||
return Response({'code': 403, 'msg': '您没有权限访问客服聊天配置'})
|
||||
|
||||
club_id = _resolve_script_club_id(request)
|
||||
scenes = MiniappScriptScene.query.filter(
|
||||
club_id=club_id, scene_key__in=SCRIPT_SCENE_KEYS,
|
||||
).order_by('scene_key')
|
||||
auto_rules = MiniappScriptAutoReply.query.filter(club_id=club_id).order_by('-sort_order', 'id')
|
||||
|
||||
items = [_serialize_script_scene(r) for r in scenes]
|
||||
items.extend(_serialize_script_auto_reply(r) for r in auto_rules)
|
||||
return Response({'code': 0, 'data': {'items': items, 'club_id': club_id}})
|
||||
|
||||
|
||||
class MiniappScriptHoutaiModifyView(APIView):
|
||||
"""POST /houtai/htxghs 后台增删改话术与自动回复"""
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def post(self, request):
|
||||
from backend.utils import verify_kefu_permission
|
||||
from jituan.services.display_config import forbid_display_write_in_all_scope
|
||||
|
||||
username = request.data.get('username')
|
||||
kefu, permissions = verify_kefu_permission(request, username)
|
||||
if kefu is None:
|
||||
return Response({'code': 403, 'msg': '身份验证失败,请检查登录状态'})
|
||||
if not _has_script_config_perm(permissions):
|
||||
return Response({'code': 403, 'msg': '您没有权限操作客服聊天配置'})
|
||||
|
||||
action = (request.data.get('action') or '').strip()
|
||||
if not action:
|
||||
return Response({'code': 400, 'msg': '缺少 action 参数'})
|
||||
|
||||
club_id = _resolve_script_club_id(request)
|
||||
|
||||
if action == 'delete':
|
||||
return self._delete_item(request, club_id)
|
||||
|
||||
deny = forbid_display_write_in_all_scope(request)
|
||||
if deny:
|
||||
return deny
|
||||
|
||||
if action in ('create', 'update'):
|
||||
item_type = (request.data.get('item_type') or '').strip()
|
||||
if item_type == 'auto_reply':
|
||||
return self._save_auto_reply(request, club_id, action)
|
||||
return self._save_scene(request, club_id, action)
|
||||
|
||||
return Response({'code': 400, 'msg': f'未知的 action: {action}'})
|
||||
|
||||
def _save_scene(self, request, club_id, action):
|
||||
scene_key = (request.data.get('scene_key') or '').strip()
|
||||
if scene_key not in SCRIPT_SCENE_KEYS:
|
||||
return Response({'code': 400, 'msg': '不支持的 scene_key'})
|
||||
|
||||
content = (request.data.get('content') or '').strip()
|
||||
if not content:
|
||||
return Response({'code': 400, 'msg': '请填写正文内容'})
|
||||
|
||||
fields = {
|
||||
'title': (request.data.get('title') or '').strip(),
|
||||
'content': content,
|
||||
'confirm_text': (request.data.get('confirm_text') or '').strip(),
|
||||
'cancel_text': (request.data.get('cancel_text') or '').strip(),
|
||||
'enabled': bool(request.data.get('is_active', True)),
|
||||
}
|
||||
|
||||
if action == 'update':
|
||||
row_id = request.data.get('id')
|
||||
if not row_id:
|
||||
return Response({'code': 400, 'msg': '缺少 id'})
|
||||
row = MiniappScriptScene.query.filter(id=row_id, club_id=club_id).first()
|
||||
if not row:
|
||||
return Response({'code': 404, 'msg': '配置不存在'})
|
||||
for k, v in fields.items():
|
||||
setattr(row, k, v)
|
||||
row.save()
|
||||
return Response({'code': 0, 'msg': '保存成功', 'data': _serialize_script_scene(row)})
|
||||
|
||||
existing = MiniappScriptScene.query.filter(club_id=club_id, scene_key=scene_key).first()
|
||||
if existing:
|
||||
for k, v in fields.items():
|
||||
setattr(existing, k, v)
|
||||
existing.save()
|
||||
return Response({'code': 0, 'msg': '保存成功', 'data': _serialize_script_scene(existing)})
|
||||
|
||||
row = MiniappScriptScene.query.create(club_id=club_id, scene_key=scene_key, **fields)
|
||||
return Response({'code': 0, 'msg': '创建成功', 'data': _serialize_script_scene(row)})
|
||||
|
||||
def _save_auto_reply(self, request, club_id, action):
|
||||
keywords = request.data.get('keywords') or []
|
||||
if not isinstance(keywords, list) or not keywords:
|
||||
return Response({'code': 400, 'msg': '请至少添加一个触发关键词'})
|
||||
|
||||
kw_list = [str(k).strip() for k in keywords if str(k).strip()]
|
||||
if not kw_list:
|
||||
return Response({'code': 400, 'msg': '请至少添加一个触发关键词'})
|
||||
|
||||
content = (request.data.get('content') or '').strip()
|
||||
if not content:
|
||||
return Response({'code': 400, 'msg': '请填写回复内容'})
|
||||
|
||||
fields = {
|
||||
'keyword': ','.join(kw_list),
|
||||
'reply_content': content,
|
||||
'sort_order': int(request.data.get('priority') or 0),
|
||||
'enabled': bool(request.data.get('is_active', True)),
|
||||
'reply_type': MiniappScriptAutoReply.REPLY_TEXT,
|
||||
}
|
||||
|
||||
if action == 'update':
|
||||
row_id = request.data.get('id')
|
||||
if not row_id:
|
||||
return Response({'code': 400, 'msg': '缺少 id'})
|
||||
row = MiniappScriptAutoReply.query.filter(id=row_id, club_id=club_id).first()
|
||||
if not row:
|
||||
return Response({'code': 404, 'msg': '规则不存在'})
|
||||
for k, v in fields.items():
|
||||
setattr(row, k, v)
|
||||
row.save()
|
||||
return Response({'code': 0, 'msg': '保存成功', 'data': _serialize_script_auto_reply(row)})
|
||||
|
||||
row = MiniappScriptAutoReply.query.create(club_id=club_id, **fields)
|
||||
return Response({'code': 0, 'msg': '创建成功', 'data': _serialize_script_auto_reply(row)})
|
||||
|
||||
def _delete_item(self, request, club_id):
|
||||
from jituan.services.display_config import forbid_display_write_in_all_scope
|
||||
|
||||
deny = forbid_display_write_in_all_scope(request)
|
||||
if deny:
|
||||
return deny
|
||||
|
||||
row_id = request.data.get('id')
|
||||
if not row_id:
|
||||
return Response({'code': 400, 'msg': '缺少 id'})
|
||||
|
||||
row = MiniappScriptAutoReply.query.filter(id=row_id, club_id=club_id).first()
|
||||
if row:
|
||||
row.delete()
|
||||
return Response({'code': 0, 'msg': '删除成功'})
|
||||
|
||||
row = MiniappScriptScene.query.filter(id=row_id, club_id=club_id).first()
|
||||
if row:
|
||||
return Response({'code': 400, 'msg': '场景话术不支持删除,请改为禁用'})
|
||||
|
||||
return Response({'code': 404, 'msg': '记录不存在'})
|
||||
Reference in New Issue
Block a user