forked from 77media/video-flow
处理前端报错
This commit is contained in:
parent
6220f2f8df
commit
72f7016680
@ -3,7 +3,7 @@ import { BASE_URL } from './constants'
|
|||||||
|
|
||||||
export interface ApiResponse<T = any> {
|
export interface ApiResponse<T = any> {
|
||||||
code: number
|
code: number
|
||||||
successfull: boolean
|
successful: boolean
|
||||||
message: string
|
message: string
|
||||||
data: T
|
data: T
|
||||||
}
|
}
|
||||||
@ -14,7 +14,7 @@ export interface TokenResponse {
|
|||||||
data: {
|
data: {
|
||||||
token: string
|
token: string
|
||||||
}
|
}
|
||||||
successfull: boolean
|
successful: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TokenWithDomainResponse {
|
export interface TokenWithDomainResponse {
|
||||||
@ -24,7 +24,7 @@ export interface TokenWithDomainResponse {
|
|||||||
token: string
|
token: string
|
||||||
domain: string
|
domain: string
|
||||||
}
|
}
|
||||||
successfull: boolean
|
successful: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface QiniuUploadResponse {
|
export interface QiniuUploadResponse {
|
||||||
@ -34,63 +34,8 @@ export interface QiniuUploadResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 获取七牛云上传token
|
|
||||||
export const getUploadToken = async (timeoutMs: number = 10000): Promise<string> => {
|
|
||||||
// 添加超时控制
|
|
||||||
const controller = new AbortController()
|
|
||||||
const timeoutId = setTimeout(() => {
|
|
||||||
console.log(`请求超时(${timeoutMs / 1000}秒),正在中断请求...`)
|
|
||||||
controller.abort()
|
|
||||||
}, timeoutMs)
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch(`${BASE_URL}/common/get-upload-token`, {
|
|
||||||
method: "GET",
|
|
||||||
headers: {
|
|
||||||
Accept: "application/json",
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
signal: controller.signal,
|
|
||||||
mode: "cors",
|
|
||||||
})
|
|
||||||
|
|
||||||
// 请求完成后清除超时
|
|
||||||
clearTimeout(timeoutId)
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const errorText = await response.text()
|
|
||||||
console.error("获取token响应错误:", response.status, errorText)
|
|
||||||
throw new Error(`获取token失败: ${response.status} ${response.statusText}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
const result: TokenResponse = await response.json()
|
|
||||||
console.log("获取token响应:", result)
|
|
||||||
|
|
||||||
if (result.code === 0 && result.successfull && result.data.token) {
|
|
||||||
console.log("成功获取token")
|
|
||||||
return result.data.token
|
|
||||||
} else {
|
|
||||||
throw new Error(result.message || "获取token失败,服务器未返回有效token")
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
clearTimeout(timeoutId)
|
|
||||||
console.error("获取上传token失败:", error)
|
|
||||||
|
|
||||||
if (error instanceof Error) {
|
|
||||||
if (error.name === "AbortError") {
|
|
||||||
throw new Error("请求超时,请检查网络连接")
|
|
||||||
} else if (error.message.includes("Failed to fetch")) {
|
|
||||||
throw new Error("网络连接失败,可能是CORS策略限制或服务器不可达")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取七牛云上传token(包含domain)
|
// 获取七牛云上传token(包含domain)
|
||||||
export const getUploadTokenWithDomain = async (timeoutMs: number = 10000): Promise<{ token: string, domain: string }> => {
|
export const getUploadToken = async (timeoutMs: number = 10000): Promise<{ token: string, domain: string }> => {
|
||||||
// 添加超时控制
|
// 添加超时控制
|
||||||
const controller = new AbortController()
|
const controller = new AbortController()
|
||||||
const timeoutId = setTimeout(() => {
|
const timeoutId = setTimeout(() => {
|
||||||
@ -121,7 +66,7 @@ export const getUploadTokenWithDomain = async (timeoutMs: number = 10000): Promi
|
|||||||
const result: TokenWithDomainResponse | TokenResponse = await response.json()
|
const result: TokenWithDomainResponse | TokenResponse = await response.json()
|
||||||
console.log("获取token响应:", result)
|
console.log("获取token响应:", result)
|
||||||
|
|
||||||
if (result.code === 0 && result.successfull && result.data.token) {
|
if (result.code === 0 && result.successful && result.data.token) {
|
||||||
console.log("成功获取token")
|
console.log("成功获取token")
|
||||||
// Support both old and new API response formats
|
// Support both old and new API response formats
|
||||||
const domain = 'domain' in result.data ? result.data.domain : 'cdn.qikongjian.com'
|
const domain = 'domain' in result.data ? result.data.domain : 'cdn.qikongjian.com'
|
||||||
|
|||||||
@ -18,7 +18,7 @@ import Image from 'next/image';
|
|||||||
import dynamic from 'next/dynamic';
|
import dynamic from 'next/dynamic';
|
||||||
import { ProjectTypeEnum, ModeEnum, ResolutionEnum } from "@/api/enums";
|
import { ProjectTypeEnum, ModeEnum, ResolutionEnum } from "@/api/enums";
|
||||||
import { createScriptEpisode, CreateScriptEpisodeRequest, updateScriptEpisode, UpdateScriptEpisodeRequest } from "@/api/script_episode";
|
import { createScriptEpisode, CreateScriptEpisodeRequest, updateScriptEpisode, UpdateScriptEpisodeRequest } from "@/api/script_episode";
|
||||||
import { getUploadTokenWithDomain, uploadToQiniu } from "@/api/common";
|
import { getUploadToken, uploadToQiniu } from "@/api/common";
|
||||||
import { convertScriptToScene, convertVideoToScene } from "@/api/video_flow";
|
import { convertScriptToScene, convertVideoToScene } from "@/api/video_flow";
|
||||||
import { EmptyStateAnimation } from '@/components/common/EmptyStateAnimation';
|
import { EmptyStateAnimation } from '@/components/common/EmptyStateAnimation';
|
||||||
|
|
||||||
@ -94,7 +94,7 @@ export function CreateToVideo2() {
|
|||||||
setIsUploading(true);
|
setIsUploading(true);
|
||||||
|
|
||||||
// 获取上传token
|
// 获取上传token
|
||||||
const { token } = await getUploadTokenWithDomain();
|
const { token } = await getUploadToken();
|
||||||
|
|
||||||
// 上传到七牛云
|
// 上传到七牛云
|
||||||
const videoUrl = await uploadToQiniu(file, token);
|
const videoUrl = await uploadToQiniu(file, token);
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import { useRouter } from 'next/navigation';
|
|||||||
import { ModeEnum, ResolutionEnum } from "@/api/enums";
|
import { ModeEnum, ResolutionEnum } from "@/api/enums";
|
||||||
import { createScriptEpisode, CreateScriptEpisodeRequest, updateScriptEpisode, UpdateScriptEpisodeRequest } from "@/api/script_episode";
|
import { createScriptEpisode, CreateScriptEpisodeRequest, updateScriptEpisode, UpdateScriptEpisodeRequest } from "@/api/script_episode";
|
||||||
import { convertVideoToScene } from "@/api/video_flow";
|
import { convertVideoToScene } from "@/api/video_flow";
|
||||||
import { getUploadTokenWithDomain, uploadToQiniu } from "@/api/common";
|
import { getUploadToken, uploadToQiniu } from "@/api/common";
|
||||||
|
|
||||||
export function VideoToVideo() {
|
export function VideoToVideo() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@ -34,7 +34,7 @@ export function VideoToVideo() {
|
|||||||
setIsUploading(true);
|
setIsUploading(true);
|
||||||
|
|
||||||
// Get upload token
|
// Get upload token
|
||||||
const { token } = await getUploadTokenWithDomain();
|
const { token } = await getUploadToken();
|
||||||
|
|
||||||
// Upload to Qiniu
|
// Upload to Qiniu
|
||||||
const uploadedVideoUrl = await uploadToQiniu(file, token);
|
const uploadedVideoUrl = await uploadToQiniu(file, token);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user