StepPick.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { View, Swiper, SwiperItem, Image } from "@tarojs/components";
  2. import React, { useEffect, useState } from "react";
  3. import style from "./index.module.less";
  4. import IconStarColor from "@/components/icon/icon-star-color";
  5. import { getUploadedAvatarStatus, TAvatarItem } from '@/service/storage'
  6. import useSWR from 'swr';
  7. import Taro from "@tarojs/taro";
  8. interface IProps {
  9. prev: () => void;
  10. next: () => void;
  11. setPickedAvatar: (pickedAvatar: TAvatarItem)=> void
  12. taskId: string|number
  13. }
  14. export default React.memo(function Index({ prev, next, taskId, setPickedAvatar }: IProps) {
  15. const [currentSwiperIndex, setCurrentSwiperIndex] = useState(0);
  16. const [avatars, setAvatars] = useState<TAvatarItem[]>([]);
  17. const [shouldPoll, setShouldPoll] = useState(false);
  18. const goNext = () => {
  19. const pickedAvatar = avatars[currentSwiperIndex]
  20. setPickedAvatar(pickedAvatar)
  21. console.log(pickedAvatar)
  22. next()
  23. }
  24. Taro.hideLoading()
  25. const { data } = useSWR(
  26. shouldPoll ? `getUploadedAvatarStatus${taskId}` : null,
  27. ()=> getUploadedAvatarStatus(taskId),
  28. {
  29. revalidateOnFocus: false,
  30. refreshInterval: shouldPoll ? 3000 : 0,
  31. refreshWhenHidden: false,
  32. refreshWhenOffline: false,
  33. revalidateOnMount: false
  34. }
  35. );
  36. useEffect(() => {
  37. setShouldPoll(true);
  38. return () => {
  39. setShouldPoll(false);
  40. };
  41. }, []);
  42. useEffect(()=> {
  43. if(data?.data.status === 'success'){
  44. setAvatars(data.data?.data.reverse())
  45. setShouldPoll(false);
  46. }
  47. return ()=> {
  48. }
  49. }, [data])
  50. const onSwiperChange = (e: any) => {
  51. const i = e.detail.current;
  52. setCurrentSwiperIndex(i);
  53. };
  54. const renderSwipers = () => {
  55. const renderIndicator = (currentIndex: number) => {
  56. return (
  57. <>
  58. <View className={style.indicatorContainer}>
  59. {avatars.map((_item, index) => {
  60. return (
  61. <View
  62. key={index}
  63. className={
  64. index === currentIndex
  65. ? `${style.indicator} ${style.indicatorActive}`
  66. : style.indicator
  67. }
  68. ></View>
  69. );
  70. })}
  71. </View>
  72. </>
  73. );
  74. };
  75. return (
  76. <View className={`${style.pickAvatarCard}`}>
  77. <Swiper
  78. className={style.mySwiper}
  79. indicatorColor="#999"
  80. indicatorActiveColor="#333"
  81. indicatorDots={false}
  82. duration={200}
  83. next-margin="-120rpx"
  84. current={currentSwiperIndex}
  85. onChange={(e) => onSwiperChange(e)}
  86. >
  87. {avatars.map(avatar => {
  88. return <SwiperItem>
  89. <View className={style.swiperItem}>
  90. <Image
  91. mode="widthFix"
  92. src={avatar.avatarUrl}
  93. ></Image>
  94. {avatar.isOriginal && <View className={style.pickAvatarOriginal}>
  95. <View>原图</View>
  96. </View> }
  97. </View>
  98. </SwiperItem>
  99. })}
  100. </Swiper>
  101. {/* <!-- 自定义指示点容器 --> */}
  102. {renderIndicator(currentSwiperIndex)}
  103. </View>
  104. );
  105. };
  106. const renderContent = ()=> {
  107. if(avatars.length){
  108. return renderSwipers()
  109. }
  110. return <View className={`${style.pickAvatarCard} ${style.pickGenCard}`}>
  111. <View className="flex items-center gap-4">
  112. <IconStarColor></IconStarColor>{" "}
  113. <View className="gradient-text">AI生成中</View>
  114. </View>
  115. </View>
  116. }
  117. return (
  118. <View>
  119. <View className={style.pickContainer}>
  120. {renderContent()}
  121. </View>
  122. <View className="bottom-bar">
  123. <View className="grid grid-cols-2 gap-8 px-20 py-12">
  124. <View className={`button-rounded`} onClick={prev}>
  125. 上一步
  126. </View>
  127. <View className={`button-rounded primary ${avatars.length ? '' : 'opacity-20'}`} onClick={()=> goNext()}>
  128. 使用这张
  129. </View>
  130. </View>
  131. </View>
  132. </View>
  133. );
  134. });