index.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { View } from "@tarojs/components";
  2. import IconStarColor from "@/components/icon/IconStarColor";
  3. import { textPolishing } from '@/service/agent'
  4. import { isSuccess } from "@/utils";
  5. import { useState } from "react";
  6. import Taro from '@tarojs/taro';
  7. interface Props {
  8. text: string;
  9. type: "personality" | "greeting"
  10. onPolished: (text:string|null) => void
  11. onStateChange: (loading: boolean) => void
  12. }
  13. const Index = ({
  14. text,
  15. type,
  16. onPolished,
  17. onStateChange,
  18. }: Props) => {
  19. const [isLoading, setIsLoading] = useState(false)
  20. const disabled = text.length < 2
  21. const handleClick = async () => {
  22. // console.log('请求服务端润色')
  23. if(isLoading || disabled){
  24. return
  25. }
  26. setIsLoading(true)
  27. onStateChange(true)
  28. try{
  29. Taro.showLoading()
  30. const response = await textPolishing({
  31. content: text,
  32. type: type
  33. })
  34. Taro.hideLoading()
  35. setIsLoading(false)
  36. onStateChange(false)
  37. if(isSuccess(response.status)){
  38. onPolished(response.data?.content)
  39. }
  40. }catch(e){
  41. Taro.hideLoading()
  42. setIsLoading(false)
  43. onStateChange(false)
  44. }
  45. }
  46. return (
  47. <View className={`flex items-center gap-4 ${disabled ? 'opacity-20' : ''}`} onClick={handleClick}>
  48. {!isLoading ? <View className="text-primary flex flex-center"><IconStarColor color="blue"></IconStarColor><View>润色</View></View> : <View className="flex flex-center"><IconStarColor color="colorful"></IconStarColor><View className="gradient-text">润色中...</View></View>}
  49. </View>
  50. );
  51. };
  52. export default Index;