fix: 扫码注册统一用户查找与俱乐部校验,修复wdlyhdl与自动注册失败
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -550,6 +550,9 @@ class DashouZhuceView(APIView):
|
||||
}, status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
from jituan.services.invite_guard import assert_invite_same_club
|
||||
from jituan.services.club_resolver import resolve_club_id_for_wechat_request
|
||||
from jituan.services.club_user import ensure_user_club_id
|
||||
|
||||
ok, msg = assert_invite_same_club(request, guanshi_profile.user, current_user)
|
||||
if not ok:
|
||||
return Response({
|
||||
@@ -558,6 +561,8 @@ class DashouZhuceView(APIView):
|
||||
'data': None
|
||||
}, status=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
club_id = resolve_club_id_for_wechat_request(request)
|
||||
|
||||
# 4. 验证管事状态
|
||||
|
||||
# 5. 核心:在数据库事务中创建用户的所有身份
|
||||
@@ -617,6 +622,8 @@ class DashouZhuceView(APIView):
|
||||
'data': None
|
||||
}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
ensure_user_club_id(current_user, club_id)
|
||||
|
||||
# 6. 注册成功,返回信息 (新注册用户没有会员记录,clumber返回空列表)
|
||||
fanhui_data = self.zhuangbeiFanhuiShuju(dashou_profile, current_user, is_new=True)
|
||||
return Response({
|
||||
@@ -851,31 +858,51 @@ class WechatLoginAndDashouRegisterView(APIView):
|
||||
}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
openid = wechat_data['openid']
|
||||
unionid = wechat_data.get('unionid', '')
|
||||
|
||||
# 3. 获取客户端真实IP
|
||||
kehuduan_ip = self.huoquKehuduanIP(request)
|
||||
|
||||
from jituan.constants import CLUB_ID_DEFAULT
|
||||
from jituan.services.club_user import (
|
||||
ensure_user_club_id,
|
||||
ensure_wx_openid_binding,
|
||||
find_user_by_wx_openid,
|
||||
)
|
||||
|
||||
# 4. 开始数据库事务(确保登录和注册的原子性)
|
||||
with transaction.atomic():
|
||||
# 4.1 创建或获取用户(登录逻辑)
|
||||
user_main, created = User.objects.select_for_update().get_or_create(
|
||||
OpenID=openid,
|
||||
defaults={
|
||||
'UserUID': self.shengchengYonghuID(),
|
||||
'UserName': openid,
|
||||
}
|
||||
)
|
||||
# 4.1 定位或创建用户(兼容俱乐部登录建立的 user_wx_openid)
|
||||
user_main = find_user_by_wx_openid(club_id, openid)
|
||||
created = False
|
||||
|
||||
if not user_main:
|
||||
try:
|
||||
user_main = User(
|
||||
UserUUID=uuid.uuid4().bytes,
|
||||
UserUID=self.shengchengYonghuID(),
|
||||
UserName=openid,
|
||||
OpenID=openid if club_id == CLUB_ID_DEFAULT else None,
|
||||
UnionID=unionid or None,
|
||||
)
|
||||
user_main.save()
|
||||
created = True
|
||||
UserBoss.query.create(user=user_main, nickname='微信用户')
|
||||
except IntegrityError:
|
||||
user_main = find_user_by_wx_openid(club_id, openid)
|
||||
if not user_main:
|
||||
raise
|
||||
|
||||
ensure_wx_openid_binding(club_id, openid, user_main, unionid)
|
||||
|
||||
# 更新用户IP和最后登录时间
|
||||
cunchu_ip = kehuduan_ip
|
||||
user_main.IP = cunchu_ip
|
||||
user_main.UserLastLoginDate = timezone.now()
|
||||
if unionid and unionid != (user_main.UnionID or ''):
|
||||
user_main.UnionID = unionid
|
||||
user_main.save()
|
||||
|
||||
if created:
|
||||
# 新用户:创建老板扩展表
|
||||
UserBoss.query.create(user=user_main, nickname='微信用户')
|
||||
|
||||
# 4.2 验证邀请码对应的管事(注册逻辑)
|
||||
try:
|
||||
# 使用select_related获取管事及其关联的主表用户信息
|
||||
@@ -886,7 +913,6 @@ class WechatLoginAndDashouRegisterView(APIView):
|
||||
return self.fanhuiZhihouDengluXinxi(user_main, yaoqingma_wuxiao=True)
|
||||
|
||||
from jituan.services.invite_guard import assert_invite_same_club
|
||||
from jituan.services.club_user import ensure_user_club_id
|
||||
from jituan.services.club_resolver import resolve_club_id_for_wechat_request
|
||||
|
||||
ok, invite_msg = assert_invite_same_club(
|
||||
@@ -1204,6 +1230,7 @@ class WechatLoginAndDashouRegisterView(APIView):
|
||||
'shangjiastatus': shangjia_status,
|
||||
'dashoustatus': dashou_status,
|
||||
'guanshistatus': guanshi_status,
|
||||
'club_id': getattr(user_main, 'ClubID', None) or '',
|
||||
'dashouqun': group_info.get('dashouqun', ''),
|
||||
'dashouqunid': group_info.get('dashouqunid', ''),
|
||||
'guanshiqun': group_info.get('guanshiqun', ''),
|
||||
|
||||
Reference in New Issue
Block a user