fix: 订单分页缓存、统计与列表一致、admin-assignments 解析

This commit is contained in:
XingQue
2026-06-24 06:18:47 +08:00
parent c42f979082
commit b33bd3eb18
3 changed files with 123 additions and 96 deletions

View File

@@ -93,6 +93,23 @@ def orders_for_request(request):
return filter_queryset_by_club(Order.query.all(), request, club_field='ClubID')
def paginate_fluent_query(fluent_qs, page, page_size):
"""
FluentQuery 分页。禁用会话缓存,避免 count 后 slice 导致列表为空。
返回 (total, items)。
"""
page = max(1, int(page))
page_size = max(1, min(int(page_size), 100))
counter = fluent_qs._clone()
counter._cache_enabled = False
total = counter.count()
start = (page - 1) * page_size
page_q = fluent_qs._clone()
page_q._cache_enabled = False
page_q._qs = page_q._qs[start:start + page_size]
return total, page_q.to_list()
def filter_user_related_by_club(qs, request, user_club_path='user__ClubID'):
"""按 User.ClubID 过滤打手/管事/商家等关联查询。"""
scope = resolve_club_scope(request)

View File

@@ -3,6 +3,7 @@ import logging
from django.utils import timezone
from rest_framework import status
from rest_framework.parsers import JSONParser
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
@@ -197,59 +198,65 @@ class ClubMeContextView(APIView):
class ClubAdminAssignmentListView(APIView):
"""POST /jituan/houtai/admin-assignments — 查看数据范围任职(非 gvsdsdk 功能权限)。"""
permission_classes = [IsAuthenticated]
parser_classes = [JSONParser]
def post(self, request):
username = (request.data.get('phone') or request.data.get('username') or '').strip()
if not username:
return Response({'code': 401, 'msg': '缺少账号'}, status=401)
kefu_obj, permissions = verify_kefu_permission(request, username)
if kefu_obj is None:
return permissions
try:
username = (request.data.get('phone') or request.data.get('username') or '').strip()
if not username:
return Response({'code': 401, 'msg': '缺少账号'}, status=401)
kefu_obj, permissions = verify_kefu_permission(request, username)
if kefu_obj is None:
return permissions
user = request.user
is_super = bool(user.IsSuperuser) or user.UserType == 'admin'
if is_super:
qs = AdminAssignment.query.filter(status=1).order_by('yonghuid', '-is_primary', 'club_id')
else:
qs = AdminAssignment.query.filter(yonghuid=user.UserUID, status=1).order_by('-is_primary')
user = request.user
is_super = bool(user.IsSuperuser) or user.UserType == 'admin'
if is_super:
qs = AdminAssignment.query.filter(status=1).order_by('yonghuid', '-is_primary', 'club_id')
else:
qs = AdminAssignment.query.filter(yonghuid=user.UserUID, status=1).order_by('-is_primary')
club_name_map = {
c.club_id: c.name for c in Club.query.filter(status=1)
}
uids = list({a.yonghuid for a in qs})
user_map = {}
if uids:
for u in User.query.filter(UserUID__in=uids).only('UserUID', 'Phone', 'UserName'):
user_map[u.UserUID] = u.Phone or u.UserName or u.UserUID
assignments = qs.to_list()
club_name_map = {
c.club_id: c.name for c in Club.query.filter(status=1)
}
uids = list({a.yonghuid for a in assignments})
user_map = {}
if uids:
for u in User.query.filter(UserUID__in=uids).only('UserUID', 'Phone', 'UserName'):
user_map[u.UserUID] = u.Phone or u.UserName or u.UserUID
rows = []
for a in qs:
club_label = '集团(全部子公司)'
if a.club_id:
club_label = club_name_map.get(a.club_id, a.club_id)
rows.append({
'yonghuid': a.yonghuid,
'phone': user_map.get(a.yonghuid, ''),
'club_id': a.club_id,
'club_label': club_label,
'role_code': a.role_code,
'role_name': ADMIN_ROLE_LABELS.get(a.role_code, a.role_code),
'data_scope': a.data_scope,
'is_primary': a.is_primary,
rows = []
for a in assignments:
club_label = '集团(全部子公司)'
if a.club_id:
club_label = club_name_map.get(a.club_id, a.club_id)
rows.append({
'yonghuid': a.yonghuid,
'phone': user_map.get(a.yonghuid, ''),
'club_id': a.club_id,
'club_label': club_label,
'role_code': a.role_code,
'role_name': ADMIN_ROLE_LABELS.get(a.role_code, a.role_code),
'data_scope': a.data_scope,
'is_primary': a.is_primary,
})
return Response({
'code': 0,
'msg': 'ok',
'data': {
'list': rows,
'perm_note': (
'功能菜单权限(能进哪些页面)在「角色管理」配置;'
'本表 admin_assignment 仅控制集团/子公司数据范围。'
),
'current_context': build_admin_club_context(user),
},
})
return Response({
'code': 0,
'msg': 'ok',
'data': {
'list': rows,
'perm_note': (
'功能菜单权限(能进哪些页面)在「角色管理」配置;'
'本表 admin_assignment 仅控制集团/子公司数据范围。'
),
'current_context': build_admin_club_context(user),
},
})
except Exception:
logger.exception('ClubAdminAssignmentListView 异常')
return Response({'code': 99, 'msg': '加载任职数据失败,请确认已执行 migrate jituan'}, status=500)
class ClubCaiwuView(APIView):