小程序分销

This commit is contained in:
jeremygan2021
2026-02-17 11:32:16 +08:00
parent 315f461a20
commit db401a7103
4 changed files with 29 additions and 3 deletions

View File

@@ -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',

View File

@@ -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)

View File

@@ -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

View File

@@ -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)