fix: 老小程序不改端,后端兼容扫码注册(dashouzhuce/wdlyhdl)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
XingQue
2026-07-08 18:20:45 +08:00
parent 7fe49775b9
commit e0c48073f1
4 changed files with 95 additions and 69 deletions

View File

@@ -509,22 +509,16 @@ class DashouZhuceView(APIView):
permission_classes = [IsAuthenticated]
def post(self, request):
from jituan.services.api_compat import legacy_miniapp_error
# 1. 获取并验证邀请码参数
yaoqingma = request.data.get('inviteCode')
if not yaoqingma:
return Response({
'code': 400,
'message': '邀请码不能为空',
'data': None
}, status=status.HTTP_400_BAD_REQUEST)
return legacy_miniapp_error(400, '邀请码不能为空')
yaoqingma = yaoqingma.strip()
if len(yaoqingma) > 100:
return Response({
'code': 400,
'message': '邀请码长度超过限制',
'data': None
}, status=status.HTTP_400_BAD_REQUEST)
return legacy_miniapp_error(400, '邀请码长度超过限制')
current_user = request.user
# 注意这里request.user是User实例
@@ -543,11 +537,7 @@ class DashouZhuceView(APIView):
# 使用select_related一次性获取管事及其关联的主表用户信息性能最高
guanshi_profile = UserGuanshi.query.select_related('user').get(yaoqingma=yaoqingma)
except UserGuanshi.DoesNotExist:
return Response({
'code': 404,
'message': '邀请码无效或不存在',
'data': None
}, status=status.HTTP_404_NOT_FOUND)
return legacy_miniapp_error(404, '邀请码无效或不存在')
from jituan.services.invite_guard import assert_invite_same_club
from jituan.services.club_resolver import resolve_club_id_for_wechat_request
@@ -559,12 +549,7 @@ class DashouZhuceView(APIView):
'dashouzhuce 俱乐部校验失败 uid=%s club=%s msg=%s',
current_user.UserUID, resolve_club_id_for_wechat_request(request), msg,
)
return Response({
'code': 403,
'message': msg,
'msg': msg,
'data': None
}, status=status.HTTP_403_FORBIDDEN)
return legacy_miniapp_error(403, msg)
club_id = resolve_club_id_for_wechat_request(request)
@@ -620,12 +605,8 @@ class DashouZhuceView(APIView):
except Exception as e:
# 事务会自动回滚
# 这里可以记录更详细的日志
return Response({
'code': 500,
'message': f'注册过程中发生系统错误: {str(e)}',
'data': None
}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
logger.exception('dashouzhuce 注册异常 uid=%s', current_user.UserUID)
return legacy_miniapp_error(500, f'注册过程中发生系统错误: {str(e)}')
ensure_user_club_id(current_user, club_id)
@@ -634,6 +615,7 @@ class DashouZhuceView(APIView):
return Response({
'code': 200,
'message': '注册成功!已开通打手、管事身份。',
'msg': '注册成功!已开通打手、管事身份。',
'data': fanhui_data
})
@@ -824,30 +806,20 @@ class WechatLoginAndDashouRegisterView(APIView):
处理微信登录并注册打手请求
"""
try:
from jituan.services.api_compat import legacy_miniapp_error
# 1. 获取并验证参数
code = request.data.get('code', '').strip()
yaoqingma = request.data.get('inviteCode', '').strip()
if not code:
return Response({
'code': 1,
'msg': '微信授权码不能为空',
'data': None
}, status=status.HTTP_400_BAD_REQUEST)
return legacy_miniapp_error(1, '微信授权码不能为空')
if not yaoqingma:
return Response({
'code': 2,
'msg': '邀请码不能为空',
'data': None
}, status=status.HTTP_400_BAD_REQUEST)
return legacy_miniapp_error(2, '邀请码不能为空')
if len(yaoqingma) > 100:
return Response({
'code': 3,
'msg': '邀请码长度超过限制',
'data': None
}, status=status.HTTP_400_BAD_REQUEST)
return legacy_miniapp_error(3, '邀请码长度超过限制')
# 2. 获取微信openid按俱乐部小程序 appid勿用全局星阙配置
from jituan.services.club_resolver import (
@@ -859,11 +831,7 @@ class WechatLoginAndDashouRegisterView(APIView):
if not wechat_data or 'openid' not in wechat_data:
error_msg = wechat_data.get('errmsg', '微信授权失败')
logger.warning('wdlyhdl 微信登录失败 club=%s err=%s', club_id, error_msg)
return Response({
'code': 4,
'msg': f'微信登录失败: {error_msg}',
'data': None
}, status=status.HTTP_400_BAD_REQUEST)
return legacy_miniapp_error(4, f'微信登录失败: {error_msg}')
openid = wechat_data['openid']
unionid = wechat_data.get('unionid', '')
@@ -916,9 +884,8 @@ class WechatLoginAndDashouRegisterView(APIView):
# 使用select_related获取管事及其关联的主表用户信息
guanshi_profile = UserGuanshi.query.select_related('user').get(yaoqingma=yaoqingma)
except UserGuanshi.DoesNotExist:
# 邀请码无效,但用户已登录,可以返回登录成功但注册失败
# 这里我们继续执行,但只返回登录信息
return self.fanhuiZhihouDengluXinxi(user_main, yaoqingma_wuxiao=True)
ensure_user_club_id(user_main, club_id)
return legacy_miniapp_error(404, '邀请码无效或不存在')
from jituan.services.invite_guard import assert_invite_same_club
from jituan.services.club_resolver import resolve_club_id_for_wechat_request
@@ -931,12 +898,7 @@ class WechatLoginAndDashouRegisterView(APIView):
'wdlyhdl 俱乐部校验失败 uid=%s club=%s msg=%s invite=%s',
user_main.UserUID, club_id, invite_msg, yaoqingma[:8],
)
return Response({
'code': 403,
'msg': invite_msg,
'message': invite_msg,
'data': None
}, status=status.HTTP_403_FORBIDDEN)
return legacy_miniapp_error(403, invite_msg)
ensure_user_club_id(user_main, resolve_club_id_for_wechat_request(request))
@@ -1003,12 +965,9 @@ class WechatLoginAndDashouRegisterView(APIView):
exc_info=True
)
# 返回友好错误信息
return Response({
'code': 99,
'msg': '系统繁忙,请稍后重试',
'data': None
}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# 返回友好错误信息HTTP 200 以便老端读出 msg
from jituan.services.api_compat import legacy_miniapp_error
return legacy_miniapp_error(99, '系统繁忙,请稍后重试')
def get_wechat_openid(self, code):
"""