This commit is contained in:
@@ -147,6 +147,12 @@ class ServiceOrderSerializer(serializers.ModelSerializer):
|
||||
validated_data['salesperson'] = salesperson
|
||||
except Salesperson.DoesNotExist:
|
||||
pass
|
||||
|
||||
try:
|
||||
distributor = Distributor.objects.get(invite_code=ref_code)
|
||||
validated_data['distributor'] = distributor
|
||||
except Distributor.DoesNotExist:
|
||||
pass
|
||||
|
||||
return super().create(validated_data)
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user