This commit is contained in:
jeremygan2021
2026-02-11 00:19:33 +08:00
parent 0b3b81915b
commit 5232ab9960
10 changed files with 183 additions and 39 deletions

View File

@@ -5,7 +5,7 @@ from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiParameter, OpenApiExample
from .models import ESP32Config, Order, WeChatPayConfig, Service, ARService, ServiceOrder
from .models import ESP32Config, Order, WeChatPayConfig, Service, ARService, ServiceOrder, Salesperson, CommissionLog
from .serializers import ESP32ConfigSerializer, OrderSerializer, ServiceSerializer, ARServiceSerializer, ServiceOrderSerializer
from wechatpayv3 import WeChatPay, WeChatPayType
from wechatpayv3.core import Core
@@ -20,7 +20,9 @@ from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from django.conf import settings
import requests
import random
import threading
from django.core.cache import cache
from time import sleep
# 猴子补丁:绕过微信支付响应签名验证
# 原因是在开发环境或证书未能正确下载时SDK 会因为无法验证微信返回的签名而抛出异常。
@@ -141,39 +143,30 @@ def send_sms_code(request):
cache_key = f"sms_code_{phone}"
cache.set(cache_key, code, timeout=300)
# 调用外部短信API
try:
api_url = "https://data.tangledup-ai.com/api/send-sms"
payload = {
"phone_number": phone,
"code": code,
"template_code": "SMS_493295002",
"sign_name": "叠加态科技云南"
}
headers = {
"Content-Type": "application/json",
"accept": "application/json"
}
response = requests.post(api_url, json=payload, headers=headers, timeout=15)
if response.status_code == 200:
print(f"短信发送成功: {phone} -> {code}")
return Response({'message': '验证码已发送'})
else:
print(f"短信发送失败: {response.text}")
return Response({'error': '短信发送失败,请稍后重试'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# 异步发送短信
def _send_async():
try:
api_url = "https://data.tangledup-ai.com/api/send-sms"
payload = {
"phone_number": phone,
"code": code,
"template_code": "SMS_493295002",
"sign_name": "叠加态科技云南"
}
headers = {
"Content-Type": "application/json",
"accept": "application/json"
}
requests.post(api_url, json=payload, headers=headers, timeout=15)
print(f"短信异步发送请求已发出: {phone} -> {code}")
except Exception as e:
print(f"异步发送短信异常: {str(e)}")
except requests.exceptions.Timeout:
print(f"短信发送超时: {phone}")
# 超时并不一定代表失败,可能是对方响应慢。但为了安全起见,提示用户稍后重试或检查手机。
# 考虑到用户反馈短信实际已收到,这里返回一个较为温和的错误或成功提示(视业务逻辑而定)。
# 这里我们选择返回一个特定的错误,前端可以据此提示用户。
return Response({'message': '短信请求已发送,请留意查收(如未收到请重试)'}, status=status.HTTP_200_OK)
except Exception as e:
print(f"发送短信异常: {str(e)}")
return Response({'error': '短信服务异常'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
threading.Thread(target=_send_async).start()
sleep(2)
# 立即返回成功无需等待外部API响应
return Response({'message': '验证码已发送'})
@extend_schema(
summary="微信支付 V3 Native 下单",
@@ -463,6 +456,44 @@ def payment_finish(request):
order.wechat_trade_no = transaction_id
order.save()
print(f"订单 {order.id} 状态已更新")
# 计算佣金
try:
salesperson = order.salesperson
if salesperson:
# 1. 计算直接佣金 (一级)
# 优先级: 产品独立分润比例 > 销售员个人分润比例
rate_1 = order.config.commission_rate if order.config.commission_rate > 0 else salesperson.commission_rate
amount_1 = order.total_price * rate_1
if amount_1 > 0:
CommissionLog.objects.create(
order=order,
salesperson=salesperson,
amount=amount_1,
level=1,
status='pending'
)
print(f"生成一级佣金: {salesperson.name} - {amount_1}")
# 2. 计算上级佣金 (二级)
parent = salesperson.parent
if parent:
rate_2 = parent.second_level_rate
amount_2 = order.total_price * rate_2
if amount_2 > 0:
CommissionLog.objects.create(
order=order,
salesperson=parent,
amount=amount_2,
level=2,
status='pending'
)
print(f"生成二级佣金: {parent.name} - {amount_2}")
except Exception as e:
print(f"佣金计算失败: {str(e)}")
except Exception as e:
print(f"订单更新失败: {str(e)}")