hookehuyr

fix(收藏功能): 修复未登录时收藏操作未跳转登录页的问题

处理收藏/取消收藏接口返回401未登录时,参考全局axios重定向逻辑跳转到登录页
...@@ -389,7 +389,7 @@ import { useAuth } from '@/contexts/auth' ...@@ -389,7 +389,7 @@ import { useAuth } from '@/contexts/auth'
389 import { useTitle } from '@vueuse/core'; 389 import { useTitle } from '@vueuse/core';
390 import { wxInfo } from '@/utils/tools'; 390 import { wxInfo } from '@/utils/tools';
391 import { startWxAuth } from '@/router/guards' 391 import { startWxAuth } from '@/router/guards'
392 -import { getAuthInfoAPI } from '@/api/auth' 392 +import { getAuthInfoAPI, getUserIsLoginAPI } from '@/api/auth'
393 import { showToast, showDialog, showImagePreview } from 'vant'; 393 import { showToast, showDialog, showImagePreview } from 'vant';
394 import { formatDate } from '@/utils/tools' 394 import { formatDate } from '@/utils/tools'
395 import { sharePage } from '@/composables/useShare.js' 395 import { sharePage } from '@/composables/useShare.js'
...@@ -621,24 +621,43 @@ const call_phone = (phone) => { ...@@ -621,24 +621,43 @@ const call_phone = (phone) => {
621 const { addToCart, proceedToCheckout } = useCart() 621 const { addToCart, proceedToCheckout } = useCart()
622 622
623 623
624 -// Handle favorite toggle 624 +/**
625 -// 收藏/取消收藏操作 625 + * 收藏/取消收藏操作
626 + * @description 添加收藏接口返回401未登录时,参考全局axios重定向逻辑,跳转到登录页并带上当前路径。
627 + * @returns {Promise<void>}
628 + */
626 const toggleFavorite = async () => { 629 const toggleFavorite = async () => {
627 if (isFavorite.value) { 630 if (isFavorite.value) {
628 - const { code, msg } = await cancelFavoriteAPI({ 631 + const resp = await cancelFavoriteAPI({
629 group_id: course.value.id 632 group_id: course.value.id
630 }) 633 })
634 + const code = resp && typeof resp === 'object' ? resp.code : 0
631 if (code) { 635 if (code) {
632 isFavorite.value = !isFavorite.value 636 isFavorite.value = !isFavorite.value
633 showToast('取消收藏') 637 showToast('取消收藏')
638 + } else {
639 + const msg = resp && resp.msg ? resp.msg : '取消收藏失败'
640 + showToast(msg)
634 } 641 }
635 } else { 642 } else {
636 - const { code, msg } = await addFavoriteAPI({ 643 + const resp = await addFavoriteAPI({
637 group_id: course.value.id 644 group_id: course.value.id
638 }) 645 })
646 + const code = resp && typeof resp === 'object' ? resp.code : 0
639 if (code) { 647 if (code) {
640 isFavorite.value = !isFavorite.value 648 isFavorite.value = !isFavorite.value
641 showToast('收藏成功') 649 showToast('收藏成功')
650 + } else {
651 + // 未登录时跳转到登录页,带上回跳地址(参考 axios.js 逻辑)
652 + const login_check = await getUserIsLoginAPI()
653 + const is_login = login_check && login_check.data ? login_check.data.is_login : false
654 + if (is_login === false) {
655 + const current_path = $route.fullPath
656 + router.push(`/login?redirect=${encodeURIComponent(current_path)}`)
657 + return
658 + }
659 + const msg = resp && resp.msg ? resp.msg : '收藏失败'
660 + showToast(msg)
642 } 661 }
643 } 662 }
644 } 663 }
......