feat: 登录状态校验功能修改

This commit is contained in:
alger
2025-09-14 00:34:35 +08:00
parent 8f9c989815
commit 8f0728d9db
5 changed files with 61 additions and 11 deletions

View File

@@ -117,6 +117,7 @@ import { createPlaylist, updatePlaylistTracks } from '@/api/music';
import { getUserPlaylist } from '@/api/user';
import { useUserStore } from '@/store';
import { getImgUrl } from '@/utils';
import { hasPermission, getLoginErrorMessage } from '@/utils/auth';
const store = useUserStore();
const { t } = useI18n();
@@ -160,6 +161,13 @@ const fetchUserPlaylists = async () => {
return;
}
// 检查是否有真实登录权限
if (!hasPermission(true)) {
message.error(getLoginErrorMessage(true));
emit('update:modelValue', false);
return;
}
const res = await getUserPlaylist(user.userId, 999);
if (res.data?.playlist) {
playlists.value = res.data.playlist.filter((item: any) => item.userId === user.userId);
@@ -173,6 +181,13 @@ const fetchUserPlaylists = async () => {
// 添加到歌单
const handleAddToPlaylist = async (playlist: any) => {
if (!props.songId) return;
// 检查是否有真实登录权限
if (!hasPermission(true)) {
message.error(getLoginErrorMessage(true));
return;
}
try {
const res = await updatePlaylistTracks({
op: 'add',
@@ -200,6 +215,12 @@ const handleCreatePlaylist = async () => {
return;
}
// 检查是否有真实登录权限
if (!hasPermission(true)) {
message.error(getLoginErrorMessage(true));
return;
}
try {
creating.value = true;

View File

@@ -21,6 +21,7 @@ import { useI18n } from 'vue-i18n';
import type { SongResult } from '@/types/music';
import { getImgUrl, isElectron } from '@/utils';
import { hasPermission } from '@/utils/auth';
const { t } = useI18n();
@@ -121,6 +122,8 @@ const renderSongPreview = () => {
// 下拉菜单选项
const dropdownOptions = computed<MenuOption[]>(() => {
const hasRealAuth = hasPermission(true);
const options: MenuOption[] = [
{
key: 'header',
@@ -153,7 +156,8 @@ const dropdownOptions = computed<MenuOption[]>(() => {
{
label: t('songItem.menu.addToPlaylist'),
key: 'addToPlaylist',
icon: () => h('i', { class: 'iconfont ri-folder-add-line' })
icon: () => h('i', { class: 'iconfont ri-folder-add-line' }),
disabled: !hasRealAuth
},
{
label: props.isFavorite ? t('songItem.menu.unfavorite') : t('songItem.menu.favorite'),
@@ -162,6 +166,7 @@ const dropdownOptions = computed<MenuOption[]>(() => {
h('i', {
class: `iconfont ${props.isFavorite ? 'ri-heart-fill text-red-500' : 'ri-heart-line'}`
})
// 收藏功能不禁用UID登录时可以本地收藏/取消收藏
},
{
label: props.isDislike ? t('songItem.menu.undislike') : t('songItem.menu.dislike'),

View File

@@ -12,6 +12,7 @@ import { audioService } from '@/services/audioService';
import type { ILyric, ILyricText, SongResult } from '@/types/music';
import { type Platform } from '@/types/music';
import { getImgUrl } from '@/utils';
import { hasPermission } from '@/utils/auth';
import { getImageLinearBackground } from '@/utils/linearColor';
import { useSettingsStore } from './settings';
@@ -1282,9 +1283,17 @@ export const usePlayerStore = defineStore('player', () => {
);
if (!isAlreadyInList) {
// 先添加到本地收藏列表
favoriteList.value.push(id);
localStorage.setItem('favoriteList', JSON.stringify(favoriteList.value));
typeof id === 'number' && useUserStore().user && likeSong(id, true);
// 只有在有真实登录权限时才调用API
if (typeof id === 'number' && useUserStore().user && hasPermission(true)) {
try {
await likeSong(id, true);
} catch (error) {
console.error('收藏歌曲API调用失败:', error);
}
}
}
};
@@ -1295,8 +1304,16 @@ export const usePlayerStore = defineStore('player', () => {
(existingId) => !isBilibiliIdMatch(existingId, id)
);
} else {
// 先从本地收藏列表中移除
favoriteList.value = favoriteList.value.filter((existingId) => existingId !== id);
useUserStore().user && likeSong(Number(id), false);
// 只有在有真实登录权限时才调用API
if (typeof id === 'number' && useUserStore().user && hasPermission(true)) {
try {
await likeSong(id, false);
} catch (error) {
console.error('取消收藏歌曲API调用失败:', error);
}
}
}
localStorage.setItem('favoriteList', JSON.stringify(favoriteList.value));
};

View File

@@ -234,6 +234,7 @@ import { useDownload } from '@/hooks/useDownload';
import { useMusicStore, usePlayerStore, useRecommendStore } from '@/store';
import { SongResult } from '@/types/music';
import { getImgUrl, isElectron, isMobile, setAnimationClass } from '@/utils';
import { getLoginErrorMessage, hasPermission } from '@/utils/auth';
const { t } = useI18n();
const route = useRoute();
@@ -816,6 +817,12 @@ const checkCollectionStatus = () => {
const toggleCollect = async () => {
if (!listInfo.value?.id) return;
// 检查是否有真实登录权限
if (!hasPermission(true)) {
message.error(getLoginErrorMessage(true));
return;
}
try {
loadingList.value = true;
const tVal = isCollected.value ? 2 : 1; // 1:收藏, 2:取消收藏