/** * 可灵数字人 API */ import request from './http' import { message } from "ant-design-vue" import { MaterialService } from './material' /** * 显示加载提示 */ const showLoading = (text) => message.loading(text, 0) /** * 销毁加载提示 */ const hideLoading = () => message.destroy() export function identifyFace(data) { return request({ url: '/webApi/api/tik/kling/identify-face', method: 'post', data }) } export function createLipSyncTask(data) { return request({ url: '/webApi/api/tik/kling/task/create', method: 'post', data }) } export function getLipSyncTask(taskId) { return request({ url: `/webApi/api/tik/kling/lip-sync/${taskId}`, method: 'get' }) } export async function identifyUploadedVideo(videoFile) { try { showLoading('正在识别视频中的人脸...') const identifyRes = await identifyFace({ video_url: videoFile.fileUrl }) hideLoading() if (identifyRes.code !== 0) { throw new Error(identifyRes.msg || '识别失败') } return { success: true, data: { fileId: videoFile.id, videoUrl: videoFile.fileUrl, sessionId: identifyRes.data.sessionId, faceId: identifyRes.data.data.face_data[0].face_id || null, startTime: identifyRes.data.data.face_data[0].start_time || 0, endTime: identifyRes.data.data.face_data[0].end_time || 0 } } } catch (error) { hideLoading() throw error } } export async function uploadAndIdentifyVideo(file) { try { showLoading('正在提取视频封面...') let coverBase64 = null try { const { extractVideoCover } = await import('@/utils/video-cover') const cover = await extractVideoCover(file, { maxWidth: 800, quality: 0.8 }) coverBase64 = cover.base64 } catch (coverError) { // 封面提取失败不影响主流程 } hideLoading() showLoading('正在上传视频...') // 使用useUpload Hook(注意:这里需要在组件中使用,这里先用MaterialService) // TODO: 在组件中集成useUpload Hook const uploadRes = await MaterialService.uploadFile(file, 'video', coverBase64, null, null) hideLoading() if (uploadRes.code !== 0) { throw new Error(uploadRes.msg || '上传失败') } const fileId = uploadRes.data showLoading('正在生成播放链接...') const urlRes = await MaterialService.getVideoPlayUrl(fileId) hideLoading() if (urlRes.code !== 0) { throw new Error(urlRes.msg || '获取播放链接失败') } const videoUrl = urlRes.data showLoading('正在识别视频中的人脸...') const identifyRes = await identifyFace({ video_url: videoUrl }) hideLoading() if (identifyRes.code !== 0) { throw new Error(identifyRes.msg || '识别失败') } return { success: true, data: { fileId, videoUrl, sessionId: identifyRes.data.sessionId, faceId: identifyRes.data.data.face_data[0].face_id || null, startTime: identifyRes.data.data.face_data[0].start_time || 0, endTime: identifyRes.data.data.face_data[0].end_time || 0 } } } catch (error) { hideLoading() throw error } }