整合register端口进入login端口
This commit is contained in:
70
app.py
70
app.py
@@ -1932,13 +1932,57 @@ def login_with_verification_code():
|
|||||||
|
|
||||||
# 验证码正确,查找用户
|
# 验证码正确,查找用户
|
||||||
user = None
|
user = None
|
||||||
|
is_new_user = False
|
||||||
|
|
||||||
if login_type == 'phone':
|
if login_type == 'phone':
|
||||||
user = User.query.filter_by(phone=credential).first()
|
user = User.query.filter_by(phone=credential).first()
|
||||||
elif login_type == 'email':
|
elif login_type == 'email':
|
||||||
user = User.query.filter_by(email=credential).first()
|
user = User.query.filter_by(email=credential).first()
|
||||||
|
|
||||||
|
# 如果用户不存在,自动创建新用户
|
||||||
if not user:
|
if not user:
|
||||||
return jsonify({'success': False, 'error': '用户不存在'}), 404
|
try:
|
||||||
|
# 生成用户名
|
||||||
|
if login_type == 'phone':
|
||||||
|
# 使用手机号生成用户名
|
||||||
|
base_username = f"用户{credential[-4:]}"
|
||||||
|
elif login_type == 'email':
|
||||||
|
# 使用邮箱前缀生成用户名
|
||||||
|
base_username = credential.split('@')[0]
|
||||||
|
else:
|
||||||
|
base_username = "新用户"
|
||||||
|
|
||||||
|
# 确保用户名唯一
|
||||||
|
username = base_username
|
||||||
|
counter = 1
|
||||||
|
while User.is_username_taken(username):
|
||||||
|
username = f"{base_username}_{counter}"
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
# 创建新用户
|
||||||
|
user = User(username=username)
|
||||||
|
|
||||||
|
# 设置手机号或邮箱
|
||||||
|
if login_type == 'phone':
|
||||||
|
user.phone = credential
|
||||||
|
elif login_type == 'email':
|
||||||
|
user.email = credential
|
||||||
|
|
||||||
|
# 设置默认密码(使用随机密码,用户后续可以修改)
|
||||||
|
user.set_password(uuid.uuid4().hex)
|
||||||
|
user.status = 'active'
|
||||||
|
user.nickname = username
|
||||||
|
|
||||||
|
db.session.add(user)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
is_new_user = True
|
||||||
|
print(f"✅ 自动创建新用户: {username}, {login_type}: {credential}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ 创建用户失败: {e}")
|
||||||
|
db.session.rollback()
|
||||||
|
return jsonify({'success': False, 'error': '创建用户失败'}), 500
|
||||||
|
|
||||||
# 清除验证码
|
# 清除验证码
|
||||||
session.pop(session_key, None)
|
session.pop(session_key, None)
|
||||||
@@ -1955,9 +1999,13 @@ def login_with_verification_code():
|
|||||||
# 更新最后登录时间
|
# 更新最后登录时间
|
||||||
user.update_last_seen()
|
user.update_last_seen()
|
||||||
|
|
||||||
|
# 根据是否为新用户返回不同的消息
|
||||||
|
message = '注册成功,欢迎加入!' if is_new_user else '登录成功'
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'success': True,
|
'success': True,
|
||||||
'message': '登录成功',
|
'message': message,
|
||||||
|
'is_new_user': is_new_user,
|
||||||
'user': {
|
'user': {
|
||||||
'id': user.id,
|
'id': user.id,
|
||||||
'username': user.username,
|
'username': user.username,
|
||||||
@@ -1971,6 +2019,7 @@ def login_with_verification_code():
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"验证码登录错误: {e}")
|
print(f"验证码登录错误: {e}")
|
||||||
|
db.session.rollback()
|
||||||
return jsonify({'success': False, 'error': '登录失败'}), 500
|
return jsonify({'success': False, 'error': '登录失败'}), 500
|
||||||
|
|
||||||
|
|
||||||
@@ -2696,6 +2745,8 @@ def wechat_callback():
|
|||||||
return redirect('/home?bind=failed')
|
return redirect('/home?bind=failed')
|
||||||
|
|
||||||
user = None
|
user = None
|
||||||
|
is_new_user = False
|
||||||
|
|
||||||
if unionid:
|
if unionid:
|
||||||
user = User.query.filter_by(wechat_union_id=unionid).first()
|
user = User.query.filter_by(wechat_union_id=unionid).first()
|
||||||
if not user:
|
if not user:
|
||||||
@@ -2726,6 +2777,9 @@ def wechat_callback():
|
|||||||
db.session.add(user)
|
db.session.add(user)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
is_new_user = True
|
||||||
|
print(f"✅ 微信扫码自动创建新用户: {username}, openid: {openid}")
|
||||||
|
|
||||||
# 更新最后登录时间
|
# 更新最后登录时间
|
||||||
user.update_last_seen()
|
user.update_last_seen()
|
||||||
|
|
||||||
@@ -2739,11 +2793,15 @@ def wechat_callback():
|
|||||||
# Flask-Login 登录
|
# Flask-Login 登录
|
||||||
login_user(user, remember=True)
|
login_user(user, remember=True)
|
||||||
|
|
||||||
# 清理微信session(仅登录/注册流程清理;绑定流程在上方已处理,不在此处清理)
|
# 更新微信session状态,供前端轮询检测
|
||||||
if state in wechat_qr_sessions:
|
if state in wechat_qr_sessions:
|
||||||
# 仅当不是绑定流程,或没有模式信息时清理
|
session_item = wechat_qr_sessions[state]
|
||||||
if not wechat_qr_sessions[state].get('mode'):
|
# 仅处理登录/注册流程,不处理绑定流程
|
||||||
del wechat_qr_sessions[state]
|
if not session_item.get('mode'):
|
||||||
|
# 更新状态和用户信息
|
||||||
|
session_item['status'] = 'register_ready' if is_new_user else 'login_ready'
|
||||||
|
session_item['user_info'] = {'user_id': user.id}
|
||||||
|
print(f"✅ 微信扫码状态已更新: {session_item['status']}, user_id: {user.id}")
|
||||||
|
|
||||||
# 直接跳转到首页
|
# 直接跳转到首页
|
||||||
return redirect('/home')
|
return redirect('/home')
|
||||||
|
|||||||
Reference in New Issue
Block a user