206 lines
5.0 KiB
Plaintext
206 lines
5.0 KiB
Plaintext
========================================
|
||
前端代码修改补丁
|
||
文件: xuniYou/pages/index/index.vue
|
||
位置: 约第 2387-2420 行
|
||
========================================
|
||
|
||
找到这段代码:
|
||
----------------------------------------
|
||
selectMusicFromLibrary(music) {
|
||
// 记录播放次数
|
||
uni.request({
|
||
url: baseURLPy + '/music/' + music.id + '/play',
|
||
method: 'POST',
|
||
header: {
|
||
'Authorization': 'Bearer ' + uni.getStorageSync("token")
|
||
}
|
||
});
|
||
|
||
// 使用音乐库的歌曲生成视频
|
||
if (this.singGenerating) {
|
||
uni.showToast({ title: '视频生成中,请稍候...', icon: 'none', duration: 2000 });
|
||
return;
|
||
}
|
||
|
||
// 这里需要调用唱歌API,传入音乐库的音乐URL
|
||
uni.showModal({
|
||
title: '提示',
|
||
content: `确定让她唱《${music.title}》吗?`,
|
||
success: (modalRes) => {
|
||
if (modalRes.confirm) {
|
||
// TODO: 调用唱歌API,使用 music.music_url
|
||
uni.showToast({ title: '功能开发中...', icon: 'none' });
|
||
}
|
||
}
|
||
});
|
||
},
|
||
|
||
// 切换音乐点赞
|
||
toggleMusicLike(music) {
|
||
----------------------------------------
|
||
|
||
替换为:
|
||
----------------------------------------
|
||
selectMusicFromLibrary(music) {
|
||
// 记录播放次数
|
||
uni.request({
|
||
url: baseURLPy + '/music/' + music.id + '/play',
|
||
method: 'POST',
|
||
header: {
|
||
'Authorization': 'Bearer ' + uni.getStorageSync("token")
|
||
}
|
||
});
|
||
|
||
// 使用音乐库的歌曲生成视频
|
||
if (this.singGenerating) {
|
||
uni.showToast({ title: '视频生成中,请稍候...', icon: 'none', duration: 2000 });
|
||
return;
|
||
}
|
||
|
||
// 检查音乐类型
|
||
if (music.upload_type === 'external') {
|
||
uni.showModal({
|
||
title: '提示',
|
||
content: '外部平台音乐无法生成视频,请使用直链或上传的音乐',
|
||
showCancel: false
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 确认生成
|
||
uni.showModal({
|
||
title: '生成唱歌视频',
|
||
content: `确定让她唱《${music.title}》吗?`,
|
||
success: (modalRes) => {
|
||
if (modalRes.confirm) {
|
||
this.generateSingVideoFromLibrary(music);
|
||
}
|
||
}
|
||
});
|
||
},
|
||
|
||
// 生成唱歌视频(从音乐库)
|
||
generateSingVideoFromLibrary(music) {
|
||
const that = this;
|
||
|
||
// 显示加载
|
||
uni.showLoading({ title: '准备中...' });
|
||
|
||
// 第 1 步:转换为系统歌曲
|
||
uni.request({
|
||
url: baseURLPy + '/music/convert-to-song',
|
||
method: 'POST',
|
||
header: {
|
||
'Authorization': 'Bearer ' + uni.getStorageSync("token")
|
||
},
|
||
data: {
|
||
music_id: music.id
|
||
},
|
||
success: (res) => {
|
||
if (res.data && res.data.code === 1) {
|
||
const songId = res.data.data.song_id;
|
||
|
||
// 第 2 步:调用现有的唱歌生成 API
|
||
that.generateSingVideoWithSongId(songId, music.title);
|
||
} else {
|
||
uni.hideLoading();
|
||
uni.showModal({
|
||
title: '转换失败',
|
||
content: res.data.message || '未知错误',
|
||
showCancel: false
|
||
});
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
uni.hideLoading();
|
||
console.error('转换失败:', err);
|
||
uni.showModal({
|
||
title: '转换失败',
|
||
content: '网络错误,请重试',
|
||
showCancel: false
|
||
});
|
||
}
|
||
});
|
||
},
|
||
|
||
// 使用 song_id 生成唱歌视频
|
||
generateSingVideoWithSongId(songId, songTitle) {
|
||
const that = this;
|
||
|
||
uni.showLoading({ title: '生成中...' });
|
||
|
||
uni.request({
|
||
url: baseURLPy + '/sing/generate',
|
||
method: 'POST',
|
||
header: {
|
||
'Authorization': 'Bearer ' + uni.getStorageSync("token")
|
||
},
|
||
data: {
|
||
song_id: songId
|
||
},
|
||
success: (res) => {
|
||
uni.hideLoading();
|
||
|
||
if (res.data && res.data.code === 1) {
|
||
const data = res.data.data;
|
||
|
||
if (data.status === 'succeeded') {
|
||
// 立即成功(有缓存)
|
||
uni.showToast({
|
||
title: '生成成功',
|
||
icon: 'success'
|
||
});
|
||
|
||
// 刷新历史记录
|
||
that.getSingHistory();
|
||
|
||
// 切换到历史记录 tab
|
||
that.switchSingTab('history');
|
||
|
||
// 播放视频
|
||
if (data.video_url) {
|
||
that.openVideoPlayer(data.video_url);
|
||
}
|
||
} else {
|
||
// 生成中
|
||
that.singGenerating = true;
|
||
that.singGeneratingTaskId = data.generation_task_id;
|
||
|
||
uni.showToast({
|
||
title: '视频生成中...',
|
||
icon: 'loading',
|
||
duration: 2000
|
||
});
|
||
|
||
// 开始轮询
|
||
that.getSingGenerateTask(data.generation_task_id);
|
||
|
||
// 切换到历史记录 tab
|
||
that.switchSingTab('history');
|
||
}
|
||
} else {
|
||
uni.showModal({
|
||
title: '生成失败',
|
||
content: res.data.message || '未知错误',
|
||
showCancel: false
|
||
});
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
uni.hideLoading();
|
||
console.error('生成失败:', err);
|
||
uni.showModal({
|
||
title: '生成失败',
|
||
content: '网络错误,请重试',
|
||
showCancel: false
|
||
});
|
||
}
|
||
});
|
||
},
|
||
|
||
// 切换音乐点赞
|
||
toggleMusicLike(music) {
|
||
----------------------------------------
|
||
|
||
保存文件后,重新编译前端即可。
|