From 315f461a209ad6fe43d643d8ee592ce4e3143608 Mon Sep 17 00:00:00 2001 From: jeremygan2021 Date: Tue, 17 Feb 2026 11:25:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E5=88=86=E9=94=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/shop/views.py | 44 ++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/backend/shop/views.py b/backend/shop/views.py index c3a10b8..fd45751 100644 --- a/backend/shop/views.py +++ b/backend/shop/views.py @@ -1455,27 +1455,27 @@ class DistributorViewSet(viewsets.GenericViewSet): @action(detail=False, methods=['post']) def invite(self, request): """生成小程序码""" - user = get_current_wechat_user(request) - if not user or not hasattr(user, 'distributor'): - return Response({'error': 'Unauthorized'}, status=401) - - distributor = user.distributor - if distributor.qr_code_url: - return Response({'qr_code_url': distributor.qr_code_url}) - - access_token = get_access_token() - if not access_token: - return Response({'error': 'Failed to get access token'}, status=500) - - # 微信小程序码接口 B:适用于需要的码数量极多的业务场景 - url = f"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={access_token}" - data = { - "scene": distributor.invite_code, - "page": "pages/index/index", # 扫码落地页 - "width": 430 - } - try: + user = get_current_wechat_user(request) + if not user or not hasattr(user, 'distributor'): + return Response({'error': 'Unauthorized'}, status=401) + + distributor = user.distributor + if distributor.qr_code_url: + return Response({'qr_code_url': distributor.qr_code_url}) + + access_token = get_access_token() + if not access_token: + return Response({'error': 'Failed to get access token'}, status=500) + + # 微信小程序码接口 B:适用于需要的码数量极多的业务场景 + url = f"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={access_token}" + data = { + "scene": distributor.invite_code, + "page": "pages/index/index", # 扫码落地页 + "width": 430 + } + res = requests.post(url, json=data) # 微信返回图片时 Content-Type 包含 image/jpeg 或 image/png if res.status_code == 200 and 'image' in res.headers.get('Content-Type', ''): @@ -1492,7 +1492,9 @@ class DistributorViewSet(viewsets.GenericViewSet): # 如果是 JSON 错误信息 return Response({'error': 'WeChat API error', 'detail': res.json()}, status=500) except Exception as e: - return Response({'error': str(e)}, status=500) + import traceback + traceback.print_exc() + return Response({'error': str(e), 'traceback': traceback.format_exc()}, status=500) @action(detail=False, methods=['post']) def withdraw(self, request):