From db401a7103d6949049152aa1748e6c797014e33d Mon Sep 17 00:00:00 2001 From: jeremygan2021 Date: Tue, 17 Feb 2026 11:32:16 +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/config/settings.py | 4 ++++ backend/config/urls.py | 1 + backend/shop/utils.py | 9 +++++++-- backend/shop/views.py | 18 +++++++++++++++++- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/backend/config/settings.py b/backend/config/settings.py index 2274ce0..b44719b 100644 --- a/backend/config/settings.py +++ b/backend/config/settings.py @@ -156,6 +156,10 @@ STATICFILES_DIRS = [ BASE_DIR / 'static', ] +# 媒体文件配置 +MEDIA_URL = '/media/' +MEDIA_ROOT = BASE_DIR / 'media' + # Django REST Framework配置 REST_FRAMEWORK = { 'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema', diff --git a/backend/config/urls.py b/backend/config/urls.py index 9fc6851..e451c91 100644 --- a/backend/config/urls.py +++ b/backend/config/urls.py @@ -18,3 +18,4 @@ urlpatterns = [ # 静态文件配置(开发环境) if settings.DEBUG: urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/backend/shop/utils.py b/backend/shop/utils.py index 7bf8209..175e816 100644 --- a/backend/shop/utils.py +++ b/backend/shop/utils.py @@ -2,6 +2,10 @@ import requests from django.core.cache import cache from .models import WeChatPayConfig +import logging + +logger = logging.getLogger(__name__) + def get_access_token(config=None): """ 获取微信接口调用凭证 (client_credential) @@ -22,6 +26,7 @@ def get_access_token(config=None): config = WeChatPayConfig.objects.filter(is_active=True).first() if not config or not config.app_id or not config.app_secret: + logger.error("No active WeChatPayConfig found or missing app_id/app_secret") return None url = f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={config.app_id}&secret={config.app_secret}" @@ -36,8 +41,8 @@ def get_access_token(config=None): cache.set(cache_key, token, expires_in - 200) return token else: - print(f"获取 AccessToken 失败: {data}") + logger.error(f"获取 AccessToken 失败: {data}") except Exception as e: - print(f"获取 AccessToken 异常: {str(e)}") + logger.error(f"获取 AccessToken 异常: {str(e)}", exc_info=True) return None diff --git a/backend/shop/views.py b/backend/shop/views.py index fd45751..28638d7 100644 --- a/backend/shop/views.py +++ b/backend/shop/views.py @@ -30,7 +30,11 @@ from django.conf import settings import requests import random import threading +import logging +import string from django.core.cache import cache + +logger = logging.getLogger(__name__) from time import sleep # 猴子补丁:绕过微信支付响应签名验证 @@ -1463,9 +1467,15 @@ class DistributorViewSet(viewsets.GenericViewSet): distributor = user.distributor if distributor.qr_code_url: return Response({'qr_code_url': distributor.qr_code_url}) + + # 确保有邀请码 + if not distributor.invite_code: + distributor.invite_code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=8)) + distributor.save() access_token = get_access_token() if not access_token: + logger.error("Failed to get access token for invite generation") return Response({'error': 'Failed to get access token'}, status=500) # 微信小程序码接口 B:适用于需要的码数量极多的业务场景 @@ -1490,8 +1500,14 @@ class DistributorViewSet(viewsets.GenericViewSet): return Response({'qr_code_url': qr_url}) else: # 如果是 JSON 错误信息 - return Response({'error': 'WeChat API error', 'detail': res.json()}, status=500) + logger.error(f"WeChat API error in invite: {res.status_code} - {res.text}") + try: + detail = res.json() + except: + detail = res.text + return Response({'error': 'WeChat API error', 'detail': detail}, status=500) except Exception as e: + logger.error("Exception in invite view: %s", str(e), exc_info=True) import traceback traceback.print_exc() return Response({'error': str(e), 'traceback': traceback.format_exc()}, status=500)