之前保证金提现(leixing=5)在申请扣款时会封禁(zhanghaozhuangtai=0),驳回退款时会解封(zhanghaozhuangtai=1)。收款流程本身不会封禁,但若申请时已封禁,收款校验会拦掉。
This commit is contained in:
@@ -873,7 +873,6 @@ def refund_balance(user_main, leixing, jine):
|
|||||||
elif leixing == 5:
|
elif leixing == 5:
|
||||||
dashou = UserDashou.objects.select_for_update().get(user=user_main)
|
dashou = UserDashou.objects.select_for_update().get(user=user_main)
|
||||||
dashou.yajin += jine
|
dashou.yajin += jine
|
||||||
dashou.zhanghaozhuangtai = 1
|
|
||||||
dashou.save()
|
dashou.save()
|
||||||
elif leixing == 6:
|
elif leixing == 6:
|
||||||
shangjia = UserShangjia.objects.select_for_update().get(user=user_main)
|
shangjia = UserShangjia.objects.select_for_update().get(user=user_main)
|
||||||
@@ -918,7 +917,6 @@ def deduct_balance(user_main, leixing, jine):
|
|||||||
if dashou.yajin < jine:
|
if dashou.yajin < jine:
|
||||||
raise ValueError('押金余额不足')
|
raise ValueError('押金余额不足')
|
||||||
dashou.yajin -= jine
|
dashou.yajin -= jine
|
||||||
dashou.zhanghaozhuangtai = 0
|
|
||||||
dashou.save()
|
dashou.save()
|
||||||
return dashou.nicheng or ''
|
return dashou.nicheng or ''
|
||||||
if leixing == 6:
|
if leixing == 6:
|
||||||
|
|||||||
145
yonghu/views.py
145
yonghu/views.py
@@ -3,7 +3,7 @@ import time
|
|||||||
import random
|
import random
|
||||||
import hashlib
|
import hashlib
|
||||||
import requests
|
import requests
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
@@ -24,7 +24,7 @@ from rest_framework.permissions import AllowAny # 新增这行
|
|||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
@@ -71,6 +71,63 @@ def huoquYonghuDaili(request):
|
|||||||
return user_agent[:100] if user_agent else ''
|
return user_agent[:100] if user_agent else ''
|
||||||
|
|
||||||
|
|
||||||
|
def shengcheng_yonghu_id():
|
||||||
|
"""生成7位数字用户ID"""
|
||||||
|
for _ in range(10):
|
||||||
|
timestamp_part = str(int(time.time()))[-5:].zfill(5)
|
||||||
|
random_part = str(random.randint(0, 99)).zfill(2)
|
||||||
|
user_id = timestamp_part + random_part
|
||||||
|
if len(user_id) == 7 and user_id.isdigit():
|
||||||
|
if not UserMain.objects.filter(yonghuid=user_id).exists():
|
||||||
|
return user_id
|
||||||
|
raise Exception('生成用户ID失败')
|
||||||
|
|
||||||
|
|
||||||
|
def get_or_create_wechat_user(openid, user_type='normal'):
|
||||||
|
"""
|
||||||
|
按 openid 获取或创建微信用户,兼容并发注册导致的主键/唯一键冲突。
|
||||||
|
返回 (user_main, created)
|
||||||
|
"""
|
||||||
|
for attempt in range(3):
|
||||||
|
try:
|
||||||
|
with transaction.atomic():
|
||||||
|
user_main = UserMain.objects.select_for_update().filter(openid=openid).first()
|
||||||
|
if user_main:
|
||||||
|
if not user_main.yonghuid:
|
||||||
|
yonghuid = shengcheng_yonghu_id()
|
||||||
|
UserMain.objects.filter(pk=user_main.pk).update(yonghuid=yonghuid)
|
||||||
|
user_main.refresh_from_db()
|
||||||
|
return user_main, False
|
||||||
|
|
||||||
|
user_main = UserMain.objects.create(
|
||||||
|
openid=openid,
|
||||||
|
yonghuid=shengcheng_yonghu_id(),
|
||||||
|
user_type=user_type,
|
||||||
|
)
|
||||||
|
user_main.refresh_from_db()
|
||||||
|
return user_main, True
|
||||||
|
except IntegrityError:
|
||||||
|
if attempt == 2:
|
||||||
|
user_main = UserMain.objects.filter(openid=openid).first()
|
||||||
|
if user_main:
|
||||||
|
return user_main, False
|
||||||
|
raise
|
||||||
|
user_main = UserMain.objects.get(openid=openid)
|
||||||
|
return user_main, False
|
||||||
|
|
||||||
|
|
||||||
|
def update_wechat_user_login_info(user_main, ip, unionid=None):
|
||||||
|
"""更新登录信息,使用 update 避免 save() 误触发 INSERT。"""
|
||||||
|
update_fields = {
|
||||||
|
'ip': ip,
|
||||||
|
'last_login_time': timezone.now(),
|
||||||
|
}
|
||||||
|
if unionid and unionid != user_main.unionid:
|
||||||
|
update_fields['unionid'] = unionid
|
||||||
|
UserMain.objects.filter(pk=user_main.pk).update(**update_fields)
|
||||||
|
user_main.refresh_from_db()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -112,20 +169,8 @@ class WechatMiniProgramLoginView(APIView):
|
|||||||
kehuduan_ip = self.huoquKehuduanIP(request)
|
kehuduan_ip = self.huoquKehuduanIP(request)
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
# 创建或获取用户
|
user_main, created = get_or_create_wechat_user(openid, user_type='normal')
|
||||||
user_main, created = UserMain.objects.select_for_update().get_or_create(
|
update_wechat_user_login_info(user_main, kehuduan_ip, unionid=unionid or None)
|
||||||
openid=openid,
|
|
||||||
defaults={'yonghuid': self.shengchengYonghuID(), 'user_type': 'normal'}
|
|
||||||
)
|
|
||||||
|
|
||||||
# 🆕【新增】保存unionid
|
|
||||||
if unionid and unionid != user_main.unionid:
|
|
||||||
user_main.unionid = unionid
|
|
||||||
|
|
||||||
cunchu_ip = kehuduan_ip
|
|
||||||
user_main.ip = cunchu_ip
|
|
||||||
user_main.last_login_time = timezone.now()
|
|
||||||
user_main.save()
|
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
UserBoss.objects.create(user=user_main, nickname='板板大人')
|
UserBoss.objects.create(user=user_main, nickname='板板大人')
|
||||||
@@ -513,7 +558,7 @@ class WeixinOfficialCallbackView(APIView):
|
|||||||
# yonghu/views.py
|
# yonghu/views.py
|
||||||
from rest_framework.parsers import MultiPartParser, FormParser,JSONParser
|
from rest_framework.parsers import MultiPartParser, FormParser,JSONParser
|
||||||
from rest_framework import permissions
|
from rest_framework import permissions
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
@@ -1597,7 +1642,7 @@ class GuanshiYaoqingDashouListView(APIView):
|
|||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework import permissions, status
|
from rest_framework import permissions, status
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from .models import UserMain, UserDashou, UserGuanshi, Tixianjilu
|
from .models import UserMain, UserDashou, UserGuanshi, Tixianjilu
|
||||||
import os
|
import os
|
||||||
import uuid
|
import uuid
|
||||||
@@ -1811,7 +1856,7 @@ class ShoukuanXinxiShangchuanView(APIView):
|
|||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework import permissions, status
|
from rest_framework import permissions, status
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.db.models import F
|
from django.db.models import F
|
||||||
import decimal
|
import decimal
|
||||||
import traceback
|
import traceback
|
||||||
@@ -1820,7 +1865,7 @@ from .models import UserMain, UserDashou, UserGuanshi, Tixianjilu
|
|||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework import permissions, status
|
from rest_framework import permissions, status
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.db.models import F
|
from django.db.models import F
|
||||||
import decimal
|
import decimal
|
||||||
import traceback
|
import traceback
|
||||||
@@ -1979,8 +2024,6 @@ class TixianShenqingView(APIView):
|
|||||||
if profile.yajin < jine:
|
if profile.yajin < jine:
|
||||||
return Response({'code': 53, 'msg': '押金余额不足'})
|
return Response({'code': 53, 'msg': '押金余额不足'})
|
||||||
profile.yajin = F('yajin') - jine
|
profile.yajin = F('yajin') - jine
|
||||||
# 押金提现后封禁打手账号
|
|
||||||
profile.zhanghaozhuangtai = 0 # 0表示禁用
|
|
||||||
profile.save()
|
profile.save()
|
||||||
nicheng = profile.nicheng or ''
|
nicheng = profile.nicheng or ''
|
||||||
elif leixing == 6:
|
elif leixing == 6:
|
||||||
@@ -2460,7 +2503,7 @@ from rest_framework.views import APIView
|
|||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
import json
|
import json
|
||||||
|
|
||||||
@@ -2926,7 +2969,7 @@ from rest_framework.response import Response
|
|||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.parsers import JSONParser
|
from rest_framework.parsers import JSONParser
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
# 获取日志器
|
# 获取日志器
|
||||||
@@ -4323,7 +4366,7 @@ class AdTongYiChuFa(APIView):
|
|||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
@@ -5700,7 +5743,7 @@ from rest_framework.views import APIView
|
|||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from peizhi.models import Szjilu # 添加收支记录表导入
|
from peizhi.models import Szjilu # 添加收支记录表导入
|
||||||
|
|
||||||
@@ -6609,20 +6652,8 @@ class WechatLoginAndDashouRegisterView(APIView):
|
|||||||
|
|
||||||
# 4. 开始数据库事务(确保登录和注册的原子性)
|
# 4. 开始数据库事务(确保登录和注册的原子性)
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
# 4.1 创建或获取用户(登录逻辑)
|
user_main, created = get_or_create_wechat_user(openid, user_type='normal')
|
||||||
user_main, created = UserMain.objects.select_for_update().get_or_create(
|
update_wechat_user_login_info(user_main, kehuduan_ip)
|
||||||
openid=openid,
|
|
||||||
defaults={
|
|
||||||
'yonghuid': self.shengchengYonghuID(),
|
|
||||||
'user_type': 'normal',
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# 更新用户IP和最后登录时间
|
|
||||||
cunchu_ip = kehuduan_ip
|
|
||||||
user_main.ip = cunchu_ip
|
|
||||||
user_main.last_login_time = timezone.now()
|
|
||||||
user_main.save()
|
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
# 新用户:创建老板扩展表
|
# 新用户:创建老板扩展表
|
||||||
@@ -7650,7 +7681,7 @@ class DashouShensuView(APIView):
|
|||||||
}, status=status.HTTP_400_BAD_REQUEST)
|
}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
# 🔴🆕【关键】使用事务确保数据一致性
|
# 🔴🆕【关键】使用事务确保数据一致性
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
# 1. 查询处罚记录
|
# 1. 查询处罚记录
|
||||||
@@ -8842,7 +8873,7 @@ class KefuGetOrderDetailView(APIView):
|
|||||||
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
@@ -9070,7 +9101,7 @@ import requests
|
|||||||
import xmltodict
|
import xmltodict
|
||||||
import time
|
import time
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
@@ -9626,7 +9657,7 @@ class KefuPlatformRefundView(APIView):
|
|||||||
|
|
||||||
# dingdan/views.py
|
# dingdan/views.py
|
||||||
import logging
|
import logging
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
@@ -11489,7 +11520,7 @@ class KefuPunishmentDetailView(APIView):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
class KefuPunishmentActionView(APIView):
|
class KefuPunishmentActionView(APIView):
|
||||||
@@ -12050,7 +12081,7 @@ class KefuWithdrawDetailView(APIView):
|
|||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
@@ -12395,7 +12426,7 @@ class KefuPunishView(APIView):
|
|||||||
return Response({'code': 0, 'msg': '处罚成功'})
|
return Response({'code': 0, 'msg': '处罚成功'})
|
||||||
|
|
||||||
|
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.contrib.auth.hashers import make_password
|
from django.contrib.auth.hashers import make_password
|
||||||
|
|
||||||
class AdKftjView(APIView):
|
class AdKftjView(APIView):
|
||||||
@@ -12641,7 +12672,7 @@ import time
|
|||||||
from datetime import date
|
from datetime import date
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework import permissions, status
|
from rest_framework import permissions, status
|
||||||
@@ -12669,7 +12700,7 @@ from datetime import date
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
@@ -12700,7 +12731,7 @@ from datetime import date
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from django.db import transaction
|
from django.db import transaction, IntegrityError
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
@@ -13542,16 +13573,8 @@ class WechatLoginAndGuanshiRegisterView(APIView):
|
|||||||
kehuduan_ip = self._huoqu_kehuduan_ip(request)
|
kehuduan_ip = self._huoqu_kehuduan_ip(request)
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
user_main, created = UserMain.objects.select_for_update().get_or_create(
|
user_main, created = get_or_create_wechat_user(openid, user_type='normal')
|
||||||
openid=openid,
|
update_wechat_user_login_info(user_main, kehuduan_ip)
|
||||||
defaults={
|
|
||||||
'yonghuid': self._shengcheng_yonghu_id(),
|
|
||||||
'user_type': 'normal',
|
|
||||||
}
|
|
||||||
)
|
|
||||||
user_main.ip = kehuduan_ip
|
|
||||||
user_main.last_login_time = timezone.now()
|
|
||||||
user_main.save()
|
|
||||||
|
|
||||||
if created:
|
if created:
|
||||||
UserBoss.objects.create(user=user_main, nickname='微信用户')
|
UserBoss.objects.create(user=user_main, nickname='微信用户')
|
||||||
@@ -13784,7 +13807,7 @@ class WechatLoginAndGuanshiRegisterView(APIView):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
from django.db import transaction, models
|
from django.db import transaction, IntegrityError, models
|
||||||
|
|
||||||
class GuanshiRegisterView(APIView):
|
class GuanshiRegisterView(APIView):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user