171 lines
3.9 KiB
JavaScript
171 lines
3.9 KiB
JavaScript
// ========================================
|
|
// 完整的替换代码
|
|
// 文件: xuniYou/pages/index/index.vue
|
|
// 位置: selectMusicFromLibrary 方法及后续
|
|
// ========================================
|
|
|
|
// 找到 selectMusicFromLibrary 方法,完整替换为以下代码:
|
|
|
|
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);
|
|
}
|
|
}
|
|
});
|
|
},
|
|
|
|
// 在 selectMusicFromLibrary 后面添加这两个新方法:
|
|
|
|
// 生成唱歌视频(从音乐库)
|
|
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
|
|
});
|
|
}
|
|
});
|
|
},
|
|
|
|
// ========================================
|
|
// 修改完成后,保存文件并重新编译前端
|
|
// ========================================
|