hookehuyr

feat(课程详情页): 添加未登录用户购买课程时的登录检查

在立即购买操作前检查用户是否登录,未登录则跳转至登录页并提示
...@@ -213,6 +213,7 @@ ...@@ -213,6 +213,7 @@
213 import { ref, onMounted, defineComponent, h } from 'vue' 213 import { ref, onMounted, defineComponent, h } from 'vue'
214 import { useRoute, useRouter } from 'vue-router' 214 import { useRoute, useRouter } from 'vue-router'
215 import { useCart } from '@/contexts/cart' 215 import { useCart } from '@/contexts/cart'
216 +import { useAuth } from '@/contexts/auth'
216 import { useTitle } from '@vueuse/core'; 217 import { useTitle } from '@vueuse/core';
217 import { showToast } from 'vant'; 218 import { showToast } from 'vant';
218 import { formatDate } from '@/utils/tools' 219 import { formatDate } from '@/utils/tools'
...@@ -230,6 +231,7 @@ useTitle($route.meta.title); ...@@ -230,6 +231,7 @@ useTitle($route.meta.title);
230 231
231 const route = useRoute() 232 const route = useRoute()
232 const router = useRouter() 233 const router = useRouter()
234 +const { currentUser } = useAuth()
233 235
234 const course = ref(null) 236 const course = ref(null)
235 const lecturers = ref([]) 237 const lecturers = ref([])
...@@ -311,6 +313,13 @@ const rightContent = h(RightContent) ...@@ -311,6 +313,13 @@ const rightContent = h(RightContent)
311 313
312 // 立即购买操作 314 // 立即购买操作
313 const handlePurchase = () => { 315 const handlePurchase = () => {
316 + // 检查用户是否已登录
317 + if (!currentUser.value) {
318 + showToast('请先登录')
319 + router.replace({ path: '/login', query: { redirect: $route.fullPath } })
320 + return
321 + }
322 +
314 if (course.value) { 323 if (course.value) {
315 addToCart({ 324 addToCart({
316 id: course.value.id, 325 id: course.value.id,
......