4444 </div >
4545 </n-flex >
4646 <n-scrollbar ref =" commentScroll" class =" comment-scroll" >
47- <template v-if =" filteredCommentHotData && filteredCommentHotData .length > 0 " >
48- <div class =" placeholder" >
49- <div class =" title" >
50- <SvgIcon name =" Fire" />
51- <span >热门评论</span >
47+ <Transition name =" fade" >
48+ <div
49+ v-if =" filteredCommentHotData && filteredCommentHotData.length > 0"
50+ class =" hot-comments"
51+ >
52+ <div class =" placeholder" >
53+ <div class =" title" >
54+ <SvgIcon name =" Fire" />
55+ <span >热门评论</span >
56+ </div >
5257 </div >
58+ <CommentList
59+ :data =" filteredCommentHotData"
60+ :loading =" false"
61+ :type =" songType"
62+ :res-id =" songId"
63+ transparent
64+ />
5365 </div >
54- <CommentList
55- :data =" filteredCommentHotData"
56- :loading =" commentHotData?.length === 0"
57- :type =" songType"
58- :res-id =" songId"
59- transparent
60- />
61- </template >
66+ </Transition >
6267 <div class =" placeholder" >
6368 <div class =" title" >
6469 <SvgIcon name =" Message" />
@@ -112,7 +117,7 @@ const songType = computed<0 | 1 | 7 | 2 | 3 | 4 | 5 | 6>(() =>
112117// 评论数据
113118const commentLoading = ref <boolean >(true );
114119const commentData = ref <CommentType []>([]);
115- const commentHotData = ref <CommentType [] | null >([] );
120+ const commentHotData = ref <CommentType [] | null >(null );
116121const commentPage = ref <number >(1 );
117122const commentHasMore = ref <boolean >(true );
118123
@@ -149,11 +154,14 @@ const filteredCommentData = computed(() => filterComments(commentData.value));
149154const filteredCommentHotData = computed (() => filterComments (commentHotData .value ));
150155
151156// 获取热门评论
152- const getHotCommentData = async () => {
157+ const getHotCommentData = async (id ? : number ) => {
158+ const targetId = id ?? songId .value ;
153159 // 本地歌曲无法获取评论
154- if (! songId . value || typeof songId . value !== " number" ) return ;
160+ if (! targetId || typeof targetId !== " number" ) return ;
155161 // 获取评论
156- const result = await getHotComment (songId .value );
162+ const result = await getHotComment (targetId );
163+ // 确保返回时歌曲未再次变化
164+ if (targetId !== songId .value ) return ;
157165 // 处理数据
158166 const formatData = formatCommentList (result .hotComments );
159167 commentHotData .value = formatData ?.length > 0 ? formatData : null ;
@@ -162,29 +170,44 @@ const getHotCommentData = async () => {
162170};
163171
164172// 获取歌曲评论
165- const getAllComment = async () => {
173+ const getAllComment = async (id ? : number , reset ? : boolean ) => {
174+ const targetId = id ?? songId .value ;
166175 // 本地歌曲无法获取评论
167- if (! songId .value || typeof songId .value !== " number" ) return ;
168- commentLoading .value = true ;
176+ if (! targetId || typeof targetId !== " number" ) return ;
177+ // 仅在首次加载时显示骨架屏(追加分页不显示)
178+ if (reset || commentData .value .length === 0 ) {
179+ commentLoading .value = true ;
180+ }
169181 // 分页参数
182+ const page = reset ? 1 : commentPage .value ;
170183 const cursor =
171- commentPage . value > 1 && commentData .value ?.length > 0
184+ ! reset && page > 1 && commentData .value ?.length > 0
172185 ? commentData .value [commentData .value .length - 1 ]?.time
173186 : undefined ;
174187 // 获取评论
175- const result = await getComment (songId .value , songType .value , commentPage .value , 20 , 3 , cursor );
188+ const result = await getComment (
189+ targetId ,
190+ musicStore .playSong .type === " radio" ? 4 : 0 ,
191+ page ,
192+ 20 ,
193+ 3 ,
194+ cursor ,
195+ );
196+ // 确保返回时歌曲未再次变化
197+ if (targetId !== songId .value ) return ;
176198 // 更新评论总数
177199 if (result .data ?.totalCount != null ) {
178200 statusStore .songCommentCount = result .data .totalCount ;
179201 }
180202 if (isEmpty (result .data ?.comments )) {
181203 commentHasMore .value = false ;
182204 commentLoading .value = false ;
205+ if (reset ) commentData .value = [];
183206 return ;
184207 }
185208 // 处理数据
186209 const formatData = formatCommentList (result .data .comments );
187- commentData .value = commentData .value .concat (formatData );
210+ commentData .value = reset ? formatData : commentData .value .concat (formatData );
188211 // 是否还有
189212 commentHasMore .value = result .data .hasMore ;
190213 commentLoading .value = false ;
@@ -196,18 +219,31 @@ const loadMoreComment = () => {
196219 getAllComment ();
197220};
198221
222+ // 重置并加载评论
223+ const resetAndFetch = (id ? : number | string ) => {
224+ const targetId = id ?? songId .value ;
225+ commentPage .value = 1 ;
226+ commentHasMore .value = true ;
227+ statusStore .songCommentCount = 0 ;
228+ getHotCommentData (typeof targetId === " number" ? targetId : undefined );
229+ getAllComment (typeof targetId === " number" ? targetId : undefined , true );
230+ };
231+
199232// 歌曲id变化
200233watch (
201234 () => songId .value ,
202- () => {
203- commentData .value = [];
204- commentHotData .value = [];
205- commentPage .value = 1 ;
206- commentHasMore .value = true ;
207- statusStore .songCommentCount = 0 ;
208- if (! isShowComment .value ) return ;
209- getHotCommentData ();
210- getAllComment ();
235+ (newId ) => {
236+ if (! isShowComment .value ) {
237+ // 不在评论页时,仅标记数据过期,打开时再加载
238+ commentData .value = [];
239+ commentHotData .value = null ;
240+ commentPage .value = 1 ;
241+ commentHasMore .value = true ;
242+ commentLoading .value = true ;
243+ statusStore .songCommentCount = 0 ;
244+ return ;
245+ }
246+ resetAndFetch (newId );
211247 },
212248);
213249
@@ -218,16 +254,14 @@ watch(
218254 if (! newVal ) return ;
219255 // 若不存在数据,重新获取
220256 if (! commentData .value ?.length ) {
221- getHotCommentData ();
222- getAllComment ();
257+ resetAndFetch ();
223258 }
224259 },
225260);
226261
227262onMounted (() => {
228263 if (! isShowComment .value ) return ;
229- getHotCommentData ();
230- getAllComment ();
264+ resetAndFetch ();
231265});
232266 </script >
233267
0 commit comments