This commit is contained in:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user