This commit is contained in:
@@ -15,10 +15,9 @@ const Layout = ({ children }) => {
|
|||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const [searchParams] = useSearchParams();
|
const [searchParams] = useSearchParams();
|
||||||
const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
|
const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
|
||||||
const [loginVisible, setLoginVisible] = useState(false);
|
|
||||||
const [profileVisible, setProfileVisible] = useState(false);
|
const [profileVisible, setProfileVisible] = useState(false);
|
||||||
|
|
||||||
const { user, login, logout } = useAuth();
|
const { user, login, logout, loginModalVisible, showLoginModal, hideLoginModal } = useAuth();
|
||||||
|
|
||||||
// 全局监听并持久化 ref 参数
|
// 全局监听并持久化 ref 参数
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -178,7 +177,7 @@ const Layout = ({ children }) => {
|
|||||||
</Dropdown>
|
</Dropdown>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<Button type="primary" onClick={() => setLoginVisible(true)}>登录</Button>
|
<Button type="primary" onClick={showLoginModal}>登录</Button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<style>{`
|
<style>{`
|
||||||
@@ -222,7 +221,7 @@ const Layout = ({ children }) => {
|
|||||||
<Button type="link" danger onClick={handleLogout} style={{ marginTop: 10 }}>退出登录</Button>
|
<Button type="link" danger onClick={handleLogout} style={{ marginTop: 10 }}>退出登录</Button>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<Button type="primary" block onClick={() => { setLoginVisible(true); setMobileMenuOpen(false); }}>登录 / 注册</Button>
|
<Button type="primary" block onClick={() => { showLoginModal(); setMobileMenuOpen(false); }}>登录 / 注册</Button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<Menu
|
<Menu
|
||||||
@@ -236,8 +235,8 @@ const Layout = ({ children }) => {
|
|||||||
</Drawer>
|
</Drawer>
|
||||||
|
|
||||||
<LoginModal
|
<LoginModal
|
||||||
visible={loginVisible}
|
visible={loginModalVisible}
|
||||||
onClose={() => setLoginVisible(false)}
|
onClose={hideLoginModal}
|
||||||
onLoginSuccess={(userData) => login(userData)}
|
onLoginSuccess={(userData) => login(userData)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ const AuthContext = createContext(null);
|
|||||||
export const AuthProvider = ({ children }) => {
|
export const AuthProvider = ({ children }) => {
|
||||||
const [user, setUser] = useState(null);
|
const [user, setUser] = useState(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [loginModalVisible, setLoginModalVisible] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const initAuth = async () => {
|
const initAuth = async () => {
|
||||||
@@ -71,8 +72,11 @@ export const AuthProvider = ({ children }) => {
|
|||||||
localStorage.setItem('user', JSON.stringify(newUser));
|
localStorage.setItem('user', JSON.stringify(newUser));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const showLoginModal = () => setLoginModalVisible(true);
|
||||||
|
const hideLoginModal = () => setLoginModalVisible(false);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AuthContext.Provider value={{ user, login, logout, updateUser, loading }}>
|
<AuthContext.Provider value={{ user, login, logout, updateUser, loading, loginModalVisible, showLoginModal, hideLoginModal }}>
|
||||||
{children}
|
{children}
|
||||||
</AuthContext.Provider>
|
</AuthContext.Provider>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ const ServiceDetail = () => {
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const [searchParams] = useSearchParams();
|
const [searchParams] = useSearchParams();
|
||||||
const { user } = useAuth();
|
const { user, showLoginModal } = useAuth();
|
||||||
const [service, setService] = useState(null);
|
const [service, setService] = useState(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [isModalOpen, setIsModalOpen] = useState(false);
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||||
@@ -223,7 +223,7 @@ const ServiceDetail = () => {
|
|||||||
onClick={() => {
|
onClick={() => {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
message.info('请先登录后再进行咨询');
|
message.info('请先登录后再进行咨询');
|
||||||
navigate('/login', { state: { from: location.pathname } });
|
showLoginModal();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setIsModalOpen(true);
|
setIsModalOpen(true);
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ const VCCourseDetail = () => {
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const [searchParams] = useSearchParams();
|
const [searchParams] = useSearchParams();
|
||||||
const { user } = useAuth();
|
const { user, showLoginModal } = useAuth();
|
||||||
const [course, setCourse] = useState(null);
|
const [course, setCourse] = useState(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [isModalOpen, setIsModalOpen] = useState(false);
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||||
@@ -276,7 +276,7 @@ const VCCourseDetail = () => {
|
|||||||
onClick={() => {
|
onClick={() => {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
message.info('请先登录后再进行报名或购买');
|
message.info('请先登录后再进行报名或购买');
|
||||||
navigate('/login', { state: { from: location.pathname } });
|
showLoginModal();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setIsModalOpen(true);
|
setIsModalOpen(true);
|
||||||
@@ -445,14 +445,14 @@ const VCCourseDetail = () => {
|
|||||||
cursor: course.is_purchased ? 'not-allowed' : 'pointer'
|
cursor: course.is_purchased ? 'not-allowed' : 'pointer'
|
||||||
}}
|
}}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
if (course.is_purchased) return;
|
if (course.is_purchased) return;
|
||||||
if (!user) {
|
if (!user) {
|
||||||
message.info('请先登录后再进行报名或购买');
|
message.info('请先登录后再进行报名或购买');
|
||||||
navigate('/login', { state: { from: location.pathname } });
|
showLoginModal();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setIsModalOpen(true);
|
setIsModalOpen(true);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{course.is_purchased ? '已购买' : (course.is_video_course ? '购买视频课程' : '立即报名 / 咨询')}
|
{course.is_purchased ? '已购买' : (course.is_video_course ? '购买视频课程' : '立即报名 / 咨询')}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Reference in New Issue
Block a user