fix: 统一微信用户定位,修复wechatlogin等UserName(openid)重复1062
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -123,20 +123,21 @@ class WechatMiniProgramLoginView(APIView):
|
||||
|
||||
openid = wechat_data['openid']
|
||||
session_key = wechat_data.get('session_key', '')
|
||||
unionid = wechat_data.get('unionid', '') # 🆕【新增】获取unionid
|
||||
unionid = wechat_data.get('unionid', '')
|
||||
|
||||
# 获取客户端真实IP
|
||||
from jituan.services.club_resolver import resolve_club_id_for_wechat_request
|
||||
from jituan.services.club_user import ensure_user_club_id, resolve_or_create_wx_user
|
||||
|
||||
club_id = resolve_club_id_for_wechat_request(request)
|
||||
kehuduan_ip = self.huoquKehuduanIP(request)
|
||||
|
||||
with transaction.atomic():
|
||||
# 创建或获取用户
|
||||
user_main, created = User.objects.select_for_update().get_or_create(
|
||||
OpenID=openid,
|
||||
defaults={'UserUID': self.shengchengYonghuID(), 'UserName': openid}
|
||||
user_main, created = resolve_or_create_wx_user(
|
||||
club_id, openid, self.shengchengYonghuID, unionid=unionid,
|
||||
)
|
||||
ensure_user_club_id(user_main, club_id)
|
||||
|
||||
# 保存unionid
|
||||
if unionid and unionid != user_main.UnionID:
|
||||
if unionid and unionid != (user_main.UnionID or ''):
|
||||
user_main.UnionID = unionid
|
||||
|
||||
user_main.IP = kehuduan_ip
|
||||
@@ -842,35 +843,16 @@ class WechatLoginAndDashouRegisterView(APIView):
|
||||
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,
|
||||
resolve_or_create_wx_user,
|
||||
)
|
||||
|
||||
# 4. 开始数据库事务(确保登录和注册的原子性)
|
||||
with transaction.atomic():
|
||||
# 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,
|
||||
ClubID='', # 勿用模型历史默认 xq,注册成功后再写
|
||||
)
|
||||
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)
|
||||
user_main, created = resolve_or_create_wx_user(
|
||||
club_id, openid, self.shengchengYonghuID, unionid=unionid,
|
||||
)
|
||||
if created:
|
||||
UserBoss.query.create(user=user_main, nickname='微信用户')
|
||||
|
||||
# 更新用户IP和最后登录时间
|
||||
cunchu_ip = kehuduan_ip
|
||||
@@ -1282,14 +1264,16 @@ class WechatLoginAndGuanshiRegisterView(APIView):
|
||||
openid = wechat_data['openid']
|
||||
kehuduan_ip = self._huoqu_kehuduan_ip(request)
|
||||
|
||||
from jituan.services.club_resolver import resolve_club_id_for_wechat_request
|
||||
from jituan.services.club_user import ensure_user_club_id, resolve_or_create_wx_user
|
||||
|
||||
club_id = resolve_club_id_for_wechat_request(request)
|
||||
|
||||
with transaction.atomic():
|
||||
user_main, created = User.objects.select_for_update().get_or_create(
|
||||
OpenID=openid,
|
||||
defaults={
|
||||
'UserUID': self._shengcheng_yonghu_id(),
|
||||
'UserName': openid,
|
||||
}
|
||||
user_main, created = resolve_or_create_wx_user(
|
||||
club_id, openid, self._shengcheng_yonghu_id,
|
||||
)
|
||||
ensure_user_club_id(user_main, club_id)
|
||||
user_main.IP = kehuduan_ip
|
||||
user_main.UserLastLoginDate = timezone.now()
|
||||
user_main.save()
|
||||
|
||||
Reference in New Issue
Block a user