From 7c166f7186ae106a8436a9b2ad2b7dec92c1efc8 Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Thu, 16 Oct 2025 10:09:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=89=8B=E6=9C=BA=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E7=A0=81=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- craco.config.js | 9 + src/components/Auth/AuthFormContent.js | 4 +- src/components/Auth/WechatRegister.js | 245 ++++++++++++------ src/components/ProtectedRoute.js | 18 +- src/components/contexts/AuthContext.js | 2 +- src/contexts/AuthContext.js | 6 +- src/contexts/AuthModalContext.js | 30 ++- .../SignIn/SignInIllustration.js | 2 +- src/views/Settings/SettingsPage.js | 4 +- 9 files changed, 212 insertions(+), 108 deletions(-) diff --git a/craco.config.js b/craco.config.js index 6029b3a3..2775e3f7 100644 --- a/craco.config.js +++ b/craco.config.js @@ -219,5 +219,14 @@ module.exports = { devMiddleware: { writeToDisk: false, }, + // 代理配置:将 /api 请求代理到后端服务器 + proxy: { + '/api': { + target: 'http://49.232.185.254:5001', + changeOrigin: true, + secure: false, + logLevel: 'debug', + }, + }, }, }; diff --git a/src/components/Auth/AuthFormContent.js b/src/components/Auth/AuthFormContent.js index 54199558..d8536c9f 100644 --- a/src/components/Auth/AuthFormContent.js +++ b/src/components/Auth/AuthFormContent.js @@ -37,7 +37,7 @@ import WechatRegister from './WechatRegister'; // API配置 const isProduction = process.env.NODE_ENV === 'production'; -const API_BASE_URL = isProduction ? "" : "http://49.232.185.254:5000"; +const API_BASE_URL = isProduction ? "" : "http://49.232.185.254:5001"; // 统一配置对象 const AUTH_CONFIG = { @@ -54,7 +54,7 @@ const AUTH_CONFIG = { // API配置 api: { endpoint: '/api/auth/register/phone', - purpose: 'register', + purpose: 'login', // ⚡ 统一使用 'login' 模式 }, // 功能开关 diff --git a/src/components/Auth/WechatRegister.js b/src/components/Auth/WechatRegister.js index 90c5dfb7..5f78cf5b 100644 --- a/src/components/Auth/WechatRegister.js +++ b/src/components/Auth/WechatRegister.js @@ -1,4 +1,4 @@ -import React, { useState, useEffect, useRef, useCallback } from "react"; +import React, { useState, useEffect, useLayoutEffect, useRef, useCallback } from "react"; import { Box, Button, @@ -15,6 +15,7 @@ import { authService, WECHAT_STATUS, STATUS_MESSAGES } from "../../services/auth // 配置常量 const POLL_INTERVAL = 2000; // 轮询间隔:2秒 const QR_CODE_TIMEOUT = 300000; // 二维码超时:5分钟 +const ENABLE_MOCK = 'true'; // 通过环境变量控制 mock export default function WechatRegister() { // 状态管理 @@ -22,11 +23,13 @@ export default function WechatRegister() { const [wechatSessionId, setWechatSessionId] = useState(""); const [wechatStatus, setWechatStatus] = useState(WECHAT_STATUS.NONE); const [isLoading, setIsLoading] = useState(false); + const [scale, setScale] = useState(1); // iframe 缩放比例 // 使用 useRef 管理定时器,避免闭包问题和内存泄漏 const pollIntervalRef = useRef(null); const timeoutRef = useRef(null); const isMountedRef = useRef(true); // 追踪组件挂载状态 + const containerRef = useRef(null); // 容器DOM引用 const navigate = useNavigate(); const toast = useToast(); @@ -189,9 +192,42 @@ export default function WechatRegister() { * 获取微信二维码 */ const getWechatQRCode = async () => { + debugger try { setIsLoading(true); + // 开发环境:使用 mock 数据 + if (ENABLE_MOCK) { + console.log('🔧 开发模式:使用 Mock 数据'); + + // 模拟网络延迟 + await new Promise(resolve => setTimeout(resolve, 500)); + + // 检查组件是否已卸载 + if (!isMountedRef.current) return; + + // Mock 数据 - 使用一个测试页面 + const mockResponse = { + auth_url: 'https://open.weixin.qq.com/connect/qrconnect?appid=mock&redirect_uri=mock&response_type=code&scope=snsapi_login&state=mock#wechat_redirect', + session_id: 'mock-session-' + Date.now() + }; + + setWechatAuthUrl(mockResponse.auth_url); + setWechatSessionId(mockResponse.session_id); + setWechatStatus(WECHAT_STATUS.WAITING); + + toast({ + title: "Mock 模式", + description: "正在使用测试数据展示", + status: "info", + duration: 2000, + }); + + // 不启动轮询(避免无效请求) + return; + } + + // 生产环境:调用真实 API const response = await authService.getWechatQRCode(); // 检查组件是否已卸载 @@ -236,6 +272,46 @@ export default function WechatRegister() { }; }, [clearTimers]); + /** + * 测量容器尺寸并计算缩放比例 + */ + useLayoutEffect(() => { + // 微信授权页面的原始尺寸 + const ORIGINAL_WIDTH = 600; + const ORIGINAL_HEIGHT = 800; + + const calculateScale = () => { + if (containerRef.current) { + const { width, height } = containerRef.current.getBoundingClientRect(); + + // 计算宽高比例,取较小值确保完全适配 + const scaleX = width / ORIGINAL_WIDTH; + const scaleY = height / ORIGINAL_HEIGHT; + const newScale = Math.min(scaleX, scaleY, 1.0); // 最大不超过1.0 + + // 设置最小缩放比例为0.3,避免过小 + setScale(Math.max(newScale, 0.3)); + } + }; + + // 初始计算 + calculateScale(); + + // 使用 ResizeObserver 监听容器尺寸变化 + const resizeObserver = new ResizeObserver(() => { + calculateScale(); + }); + + if (containerRef.current) { + resizeObserver.observe(containerRef.current); + } + + // 清理 + return () => { + resizeObserver.disconnect(); + }; + }, [wechatStatus]); // 当状态变化时重新计算 + /** * 渲染状态提示文本 */ @@ -246,96 +322,119 @@ export default function WechatRegister() { return ( - {STATUS_MESSAGES[wechatStatus] || STATUS_MESSAGES[WECHAT_STATUS.WAITING]} + {STATUS_MESSAGES[wechatStatus]} ); }; return ( - - - 微信扫一扫 - - - - {/* 灰色二维码底图 - 始终显示 */} - {wechatStatus === WECHAT_STATUS.WAITING ? ( - + + {wechatStatus === WECHAT_STATUS.WAITING ? ( + <> + + 微信扫码 + +