小程序分销
All checks were successful
Deploy to Server / deploy (push) Successful in 24s

This commit is contained in:
jeremygan2021
2026-02-17 11:14:58 +08:00
parent 321c57bee2
commit ac61a127ae
15 changed files with 464 additions and 21 deletions

View File

@@ -1463,10 +1463,36 @@ class DistributorViewSet(viewsets.GenericViewSet):
if distributor.qr_code_url:
return Response({'qr_code_url': distributor.qr_code_url})
# 调用微信接口生成小程序码 (wxacode.getUnlimited)
# 这里简化处理返回模拟URL或需要实现具体逻辑
# 实际逻辑需要获取 AccessToken 然后调用 API
return Response({'qr_code_url': 'https://placeholder.com/qrcode.png'})
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:
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', ''):
file_name = f"distributor_qr_{distributor.invite_code}_{uuid.uuid4().hex[:6]}.png"
# 保存到 media/qr_codes 目录
path = default_storage.save(f"qr_codes/{file_name}", ContentFile(res.content))
qr_url = default_storage.url(path)
distributor.qr_code_url = qr_url
distributor.save()
return Response({'qr_code_url': qr_url})
else:
# 如果是 JSON 错误信息
return Response({'error': 'WeChat API error', 'detail': res.json()}, status=500)
except Exception as e:
return Response({'error': str(e)}, status=500)
@action(detail=False, methods=['post'])
def withdraw(self, request):