Ai_GirlFriend/xuniYou_pages_index_index_vue_修改后的代码.txt

171 lines
4.0 KiB
Plaintext
Raw Normal View History

// ========================================
// 在 xuniYou/pages/index/index.vue 文件中
// 找到第 2387 行的 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);
}
}
});
},
// 生成唱歌视频(从音乐库)
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
});
}
});
},
// ========================================
// 修改说明:
// 1. 删除了 "uni.showToast({ title: '功能开发中...', icon: 'none' });" 这行
// 2. 添加了外部链接检查
// 3. 添加了两个新方法generateSingVideoFromLibrary 和 generateSingVideoWithSongId
// 4. 保存文件后,重新编译前端即可
// ========================================