From e533424de50510a1cef0c44588f2fc263734bf04 Mon Sep 17 00:00:00 2001 From: XingQue Date: Wed, 1 Jul 2026 12:28:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=A2=E6=9C=8D=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E8=AF=9D=E6=9C=AF=E9=85=8D=E7=BD=AE=E6=8E=A5=E5=8F=A3=E4=B8=8E?= =?UTF-8?q?=E4=BC=9A=E8=AF=9D=E7=AE=A1=E7=90=86=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/urls.py | 3 + config/views.py | 237 ++++++++++++++++-- .../commands/seed_gvsdsdk_permissions.py | 6 + jituan/services/kefu_menu_definitions.py | 5 +- 4 files changed, 236 insertions(+), 15 deletions(-) diff --git a/backend/urls.py b/backend/urls.py index 4fa641e..40fb570 100644 --- a/backend/urls.py +++ b/backend/urls.py @@ -15,6 +15,7 @@ from .view import (GetRolePermissionView, FaKuanTongJiView, FaKuanLieBiaoView, FaKuanChuLiView, FaKuanChuangJianView, HqbkxxView,BkxgView,CaiwuView,CwhybkhqView, HybkjtsjView, SzxxView, CwddhqlxView, CwhqjtddsjView, CwqtczhqView, KhpzhqView, ChzsgcView,KhgglView, ShgxgsjView, KhjlglView, KhjlczView, ZxkfghdsView) +from config.views import MiniappScriptHoutaiListView, MiniappScriptHoutaiModifyView urlpatterns = [ path('gljshq', GetRolePermissionView.as_view(), name='获取就角色信息'), @@ -63,6 +64,8 @@ urlpatterns = [ path('htxgfl', ModifyRateView.as_view(), name='后台修改费率'), path('hthqtcxx', PopupNoticeListAPIView.as_view(), name='后台获取弹窗配置'), path('htxgtcxx', PopupNoticeModifyAPIView.as_view(), name='后台更新创建弹窗配置'), + path('hthqhs', MiniappScriptHoutaiListView.as_view(), name='后台获取话术配置'), + path('htxghs', MiniappScriptHoutaiModifyView.as_view(), name='后台更新话术配置'), path('hqdplbsj', ShopListView.as_view(), name='获取店铺列表数据'), path('xgdpxx', ShopModifyView.as_view(), name='修改或添加店铺数据'), diff --git a/config/views.py b/config/views.py index 00234b0..33d15b9 100644 --- a/config/views.py +++ b/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': ''}}) \ No newline at end of file + 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': '记录不存在'}) \ No newline at end of file diff --git a/jituan/management/commands/seed_gvsdsdk_permissions.py b/jituan/management/commands/seed_gvsdsdk_permissions.py index 162fc8e..32354ab 100644 --- a/jituan/management/commands/seed_gvsdsdk_permissions.py +++ b/jituan/management/commands/seed_gvsdsdk_permissions.py @@ -14,6 +14,12 @@ EXTRA_PERMISSIONS = [ '配置各俱乐部考试开关、抽题规则与题库(小程序配置-打手接单考试)', '小程序配置', ), + ( + 'kfhs666', + '客服聊天配置', + '配置客服欢迎语、发图确认弹窗、关键词自动回复(会话管理-客服聊天配置)', + '会话管理', + ), ( 'sfbq666', '身份装饰标签管理', diff --git a/jituan/services/kefu_menu_definitions.py b/jituan/services/kefu_menu_definitions.py index 6ea837e..52bc32f 100644 --- a/jituan/services/kefu_menu_definitions.py +++ b/jituan/services/kefu_menu_definitions.py @@ -96,6 +96,9 @@ KEFU_MENU_ROW_DEFS = [ 'perm_codes': ['999a', '999b', '999c', '999d', '999e']}, {'page_id': 'shop.products', 'name': '店铺商品管理', 'path': '/shop/products', 'parent_id': 'shop', 'sort_order': 123, 'perm_codes': ['999a', '999b', '999c', '999d', '999e']}, - {'page_id': 'chat.agent', 'name': '会话管理', 'path': '/chat/agent', 'parent_id': '', 'sort_order': 130, + {'page_id': 'chat', 'name': '会话管理', 'path': '', 'parent_id': '', 'sort_order': 130}, + {'page_id': 'chat.agent', 'name': '会话接入', 'path': '/chat/agent', 'parent_id': 'chat', 'sort_order': 131, 'perm_codes': ['abca1', 'baac2', 'cabc3', 'cb3a2', 'bcaa4']}, + {'page_id': 'chat.script-config', 'name': '客服聊天配置', 'path': '/chat/script-config', 'parent_id': 'chat', 'sort_order': 132, + 'perm_codes': ['kfhs666', '8080a']}, ]