diff --git a/app.py b/app.py index 7f8ac1ae..c10a6a8d 100755 --- a/app.py +++ b/app.py @@ -345,6 +345,9 @@ WECHAT_JSAPI_TICKET_PREFIX = "wechat:jsapi_ticket:" # 微信回调地址 WECHAT_REDIRECT_URI = 'https://api.valuefrontier.cn/api/auth/wechat/callback' +# 前端域名(用于登录成功后重定向) +FRONTEND_URL = 'https://valuefrontier.cn' + # 邮件服务配置(QQ企业邮箱) MAIL_SERVER = 'smtp.exmail.qq.com' MAIL_PORT = 465 @@ -2976,13 +2979,13 @@ def alipay_payment_return(): if out_trade_no: # 重定向到前端支付结果页面 - return redirect(f'/pricing?payment_return=alipay&order_no={out_trade_no}') + return redirect(f'{FRONTEND_URL}/pricing?payment_return=alipay&order_no={out_trade_no}') else: - return redirect('/pricing?payment_return=alipay&error=missing_order') + return redirect(f'{FRONTEND_URL}/pricing?payment_return=alipay&error=missing_order') except Exception as e: print(f"❌ 支付宝同步返回处理失败: {e}") - return redirect('/pricing?payment_return=alipay&error=exception') + return redirect(f'{FRONTEND_URL}/pricing?payment_return=alipay&error=exception') @app.route('/api/payment/alipay/order//status', methods=['GET']) @@ -4510,18 +4513,18 @@ def wechat_callback(): if state and wechat_session_exists(state): update_wechat_session(state, {'status': 'auth_denied', 'error': '用户拒绝授权'}) print(f"❌ 用户拒绝授权: state={state}") - return redirect('/home?error=wechat_auth_denied') + return redirect(f'{FRONTEND_URL}/home?error=wechat_auth_denied') # 参数验证 if not code or not state: if state and wechat_session_exists(state): update_wechat_session(state, {'status': 'auth_failed', 'error': '授权参数缺失'}) - return redirect('/home?error=wechat_auth_failed') + return redirect(f'{FRONTEND_URL}/home?error=wechat_auth_failed') # 从 Redis 获取 session(自动处理过期) session_data = get_wechat_session(state) if not session_data: - return redirect('/home?error=session_expired') + return redirect(f'{FRONTEND_URL}/home?error=session_expired') try: # 步骤1: 用户已扫码并授权(微信回调过来说明用户已完成扫码+授权) @@ -4544,7 +4547,7 @@ def wechat_callback(): if not token_data: update_wechat_session(state, {'status': 'auth_failed', 'error': '获取访问令牌失败'}) print(f"❌ 获取微信access_token失败: state={state}") - return redirect('/home?error=token_failed') + return redirect(f'{FRONTEND_URL}/home?error=token_failed') # 步骤3: Token获取成功,标记为已授权 update_wechat_session(state, {'status': 'authorized'}) @@ -4555,7 +4558,7 @@ def wechat_callback(): if not user_info: update_wechat_session(state, {'status': 'auth_failed', 'error': '获取用户信息失败'}) print(f"❌ 获取微信用户信息失败: openid={token_data['openid']}") - return redirect('/home?error=userinfo_failed') + return redirect(f'{FRONTEND_URL}/home?error=userinfo_failed') # 查找或创建用户 / 或处理绑定 openid = token_data['openid'] @@ -4566,11 +4569,11 @@ def wechat_callback(): try: target_user_id = session.get('user_id') or session_data.get('bind_user_id') if not target_user_id: - return redirect('/home?error=bind_no_user') + return redirect(f'{FRONTEND_URL}/home?error=bind_no_user') target_user = User.query.get(target_user_id) if not target_user: - return redirect('/home?error=bind_user_missing') + return redirect(f'{FRONTEND_URL}/home?error=bind_user_missing') # 检查该微信是否已被其他账户绑定 existing = None @@ -4581,7 +4584,7 @@ def wechat_callback(): if existing and existing.id != target_user.id: update_wechat_session(state, {'status': 'bind_conflict'}) - return redirect('/home?bind=conflict') + return redirect(f'{FRONTEND_URL}/home?bind=conflict') # 执行绑定 target_user.bind_wechat(openid, unionid, wechat_info=user_info) @@ -4589,12 +4592,12 @@ def wechat_callback(): # 标记绑定完成,供前端轮询 update_wechat_session(state, {'status': 'bind_ready', 'user_info': {'user_id': target_user.id}}) - return redirect('/home?bind=success') + return redirect(f'{FRONTEND_URL}/home?bind=success') except Exception as e: print(f"❌ 微信绑定失败: {e}") db.session.rollback() update_wechat_session(state, {'status': 'bind_failed'}) - return redirect('/home?bind=failed') + return redirect(f'{FRONTEND_URL}/home?bind=failed') user = None is_new_user = False @@ -4615,7 +4618,7 @@ def wechat_callback(): 'debug_nickname': user_info.get('nickname', '')[:10], 'debug_keys_in_userinfo': ','.join(user_info.keys()) if user_info else 'null', }) - return redirect(f'/home?{debug_params}') + return redirect(f'{FRONTEND_URL}/home?{debug_params}') user = User.query.filter_by(wechat_union_id=unionid).first() @@ -4697,7 +4700,7 @@ def wechat_callback(): 'is_new_user': '1' if is_new_user else '0', }) print(f"✅ PC 微信登录成功,重定向到前端回调页面") - return redirect(f"/home/wechat-callback?{pc_redirect_params}") + return redirect(f"{FRONTEND_URL}/home/wechat-callback?{pc_redirect_params}") except Exception as e: print(f"❌ 微信登录失败: {e}") @@ -4710,7 +4713,7 @@ def wechat_callback(): update_wechat_session(state, {'status': 'auth_failed', 'error': str(e)}) # ⚡ 重定向到首页并显示错误 - return redirect('/home?error=wechat_login_failed') + return redirect(f'{FRONTEND_URL}/home?error=wechat_login_failed') @app.route('/api/auth/login/wechat', methods=['POST'])