|
@@ -2,9 +2,10 @@ import { View, Swiper, SwiperItem, Image } from "@tarojs/components";
|
|
import React, { useEffect, useState } from "react";
|
|
import React, { useEffect, useState } from "react";
|
|
import style from "./index.module.less";
|
|
import style from "./index.module.less";
|
|
import IconStarColor from "@/components/icon/icon-star-color";
|
|
import IconStarColor from "@/components/icon/icon-star-color";
|
|
-import { getUploadedAvatarStatus, TAvatarItem } from '@/service/storage'
|
|
|
|
|
|
+import { getUploadedAvatarStatus, genAvatarVideo, type TAvatarItem } from '@/service/storage'
|
|
import useSWR from 'swr';
|
|
import useSWR from 'swr';
|
|
import Taro from "@tarojs/taro";
|
|
import Taro from "@tarojs/taro";
|
|
|
|
+import { isSuccess } from "@/utils";
|
|
interface IProps {
|
|
interface IProps {
|
|
prev: () => void;
|
|
prev: () => void;
|
|
next: () => void;
|
|
next: () => void;
|
|
@@ -15,7 +16,8 @@ export default React.memo(function Index({ prev, next, taskId, setPickedAvatar }
|
|
const [currentSwiperIndex, setCurrentSwiperIndex] = useState(0);
|
|
const [currentSwiperIndex, setCurrentSwiperIndex] = useState(0);
|
|
const [avatars, setAvatars] = useState<TAvatarItem[]>([]);
|
|
const [avatars, setAvatars] = useState<TAvatarItem[]>([]);
|
|
const [shouldPoll, setShouldPoll] = useState(false);
|
|
const [shouldPoll, setShouldPoll] = useState(false);
|
|
-
|
|
|
|
|
|
+ const [videoGen, setVideoGen] = useState(false);
|
|
|
|
+ const [currentTaskId, setCurrentTaskId] = useState<string|number>(taskId);
|
|
|
|
|
|
const goNext = () => {
|
|
const goNext = () => {
|
|
const pickedAvatar = avatars[currentSwiperIndex]
|
|
const pickedAvatar = avatars[currentSwiperIndex]
|
|
@@ -26,9 +28,9 @@ export default React.memo(function Index({ prev, next, taskId, setPickedAvatar }
|
|
|
|
|
|
Taro.hideLoading()
|
|
Taro.hideLoading()
|
|
|
|
|
|
- const { data } = useSWR(
|
|
|
|
- shouldPoll ? `getUploadedAvatarStatus${taskId}` : null,
|
|
|
|
- ()=> getUploadedAvatarStatus(taskId),
|
|
|
|
|
|
+ const { data, mutate } = useSWR(
|
|
|
|
+ shouldPoll ? `getUploadedAvatarStatus${currentTaskId}` : null,
|
|
|
|
+ ()=> getUploadedAvatarStatus(currentTaskId),
|
|
{
|
|
{
|
|
revalidateOnFocus: false,
|
|
revalidateOnFocus: false,
|
|
refreshInterval: shouldPoll ? 3000 : 0,
|
|
refreshInterval: shouldPoll ? 3000 : 0,
|
|
@@ -50,20 +52,37 @@ export default React.memo(function Index({ prev, next, taskId, setPickedAvatar }
|
|
setAvatars(data.data?.data.reverse())
|
|
setAvatars(data.data?.data.reverse())
|
|
setShouldPoll(false);
|
|
setShouldPoll(false);
|
|
}
|
|
}
|
|
|
|
+ if(data?.data.status === 'process_fail'){
|
|
|
|
+ setShouldPoll(false);
|
|
|
|
+ }
|
|
return ()=> {
|
|
return ()=> {
|
|
|
|
|
|
}
|
|
}
|
|
}, [data])
|
|
}, [data])
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ const genVideo = async () => {
|
|
|
|
+ const a = avatars[currentSwiperIndex]
|
|
|
|
+ if(!a.avatarUrl){
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ const avatarUrl = a.avatarUrl
|
|
|
|
+ setVideoGen(true)
|
|
|
|
+ // 清空 useSWR 缓存
|
|
|
|
+ mutate(undefined, false);
|
|
|
|
+ setAvatars([]);
|
|
|
|
+ const response = await genAvatarVideo({avatarUrl})
|
|
|
|
+ if(isSuccess(response.status)){
|
|
|
|
+ setCurrentTaskId(response.data.taskId)
|
|
|
|
+ setShouldPoll(true)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
const onSwiperChange = (e: any) => {
|
|
const onSwiperChange = (e: any) => {
|
|
const i = e.detail.current;
|
|
const i = e.detail.current;
|
|
setCurrentSwiperIndex(i);
|
|
setCurrentSwiperIndex(i);
|
|
- };
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+ };
|
|
|
|
|
|
const renderSwipers = () => {
|
|
const renderSwipers = () => {
|
|
const renderIndicator = (currentIndex: number) => {
|
|
const renderIndicator = (currentIndex: number) => {
|
|
@@ -119,15 +138,30 @@ export default React.memo(function Index({ prev, next, taskId, setPickedAvatar }
|
|
);
|
|
);
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ const renderProcessingStatus = () => {
|
|
|
|
+ const targetText = !videoGen ? 'AI' : '视频'
|
|
|
|
+
|
|
|
|
+ if(data?.data.status === 'process_fail'){
|
|
|
|
+ return <View className="gradient-text">{`${targetText}生成失败`}</View>
|
|
|
|
+ }
|
|
|
|
+ if(data?.data.status === 'success'){
|
|
|
|
+ return <View className="gradient-text">{`${targetText}生成成功`}</View>
|
|
|
|
+ }
|
|
|
|
+ return <View className="gradient-text">{`${targetText}生成中`}</View>
|
|
|
|
+ }
|
|
|
|
+
|
|
const renderContent = ()=> {
|
|
const renderContent = ()=> {
|
|
if(avatars.length){
|
|
if(avatars.length){
|
|
|
|
+
|
|
return renderSwipers()
|
|
return renderSwipers()
|
|
}
|
|
}
|
|
|
|
|
|
return <View className={`${style.pickAvatarCard} ${style.pickGenCard}`}>
|
|
return <View className={`${style.pickAvatarCard} ${style.pickGenCard}`}>
|
|
<View className="flex items-center gap-4">
|
|
<View className="flex items-center gap-4">
|
|
<IconStarColor></IconStarColor>{" "}
|
|
<IconStarColor></IconStarColor>{" "}
|
|
- <View className="gradient-text">AI生成中</View>
|
|
|
|
|
|
+ {renderProcessingStatus()}
|
|
</View>
|
|
</View>
|
|
</View>
|
|
</View>
|
|
}
|
|
}
|
|
@@ -145,6 +179,9 @@ export default React.memo(function Index({ prev, next, taskId, setPickedAvatar }
|
|
<View className={`button-rounded`} onClick={prev}>
|
|
<View className={`button-rounded`} onClick={prev}>
|
|
上一步
|
|
上一步
|
|
</View>
|
|
</View>
|
|
|
|
+ <View className={`button-rounded`} onClick={genVideo}>
|
|
|
|
+ 生成微视频
|
|
|
|
+ </View>
|
|
<View className={`button-rounded primary ${avatars.length ? '' : 'opacity-20'}`} onClick={()=> goNext()}>
|
|
<View className={`button-rounded primary ${avatars.length ? '' : 'opacity-20'}`} onClick={()=> goNext()}>
|
|
使用这张
|
|
使用这张
|
|
</View>
|
|
</View>
|