hookehuyr

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

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