index.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { useState } from "react";
  2. import PageCustom from "@/components/page-custom/index";
  3. import NavBarNormal from "@/components/NavBarNormal/index";
  4. import AvatarConfirm from '@/components/AvatarConfirm/index'
  5. import Taro, { nextTick } from "@tarojs/taro";
  6. import { TAvatarItem } from "@/service/storage";
  7. import { useAgentStore, useAgentStoreActions } from "@/store/agentStore";
  8. interface IProps {
  9. avatarItem?: TAvatarItem
  10. }
  11. export default function Index({ avatarItem }: IProps) {
  12. // 可以由父组件传入或从 agentStore 中获取
  13. const currentEditAvatar = avatarItem ?? useAgentStore(state => state.currentEditAvatar)
  14. const agentEdit = useAgentStore(state => state.agentEdit)
  15. const { updateEditAgent, saveAgent } = useAgentStoreActions()
  16. const currentItem = { ...(currentEditAvatar || {}) }
  17. const [enabledChatBg, setEnabledChatBg] = useState(agentEdit?.enabledChatBg ?? false)
  18. const handleConfirm = async (edit: TAvatarItem & { enabledChatBg: boolean }) => {
  19. console.log('confirm')
  20. console.log(edit, 2222)
  21. if (edit.avatarLogo && edit.avatarUrl) {
  22. updateEditAgent({...edit})
  23. // 如果是编辑,直接编辑形象成功
  24. if(agentEdit?.agentId){
  25. nextTick(()=> {
  26. saveAgent()
  27. Taro.showToast({title: '形象设置成功', icon: 'success'})
  28. setTimeout(()=> {
  29. Taro.navigateBack({delta:2})
  30. }, 2000)
  31. })
  32. return
  33. }
  34. // 如果是新建智能体,只是暂时设置并不更新至服务器
  35. Taro.showToast({title: '形象设置成功', icon: 'success'})
  36. setTimeout(()=> {
  37. Taro.navigateBack({delta:2})
  38. }, 2000)
  39. }
  40. }
  41. const handleChange = () => {
  42. Taro.navigateBack()
  43. }
  44. const handleCropDone = (url: string) => {
  45. console.log('handleCropDone', url)
  46. if (currentItem?.avatarLogo) {
  47. currentItem.avatarLogo = url
  48. }
  49. }
  50. return (
  51. <PageCustom>
  52. <NavBarNormal>形象确认</NavBarNormal>
  53. <>
  54. {!!currentEditAvatar && <AvatarConfirm
  55. avatarItem={currentEditAvatar}
  56. enabledChatBg={enabledChatBg}
  57. setEnabledChatBg={setEnabledChatBg}
  58. onCropDone={handleCropDone}
  59. onChange={handleChange}
  60. onConfirm={handleConfirm} />}
  61. </>
  62. </PageCustom>
  63. );
  64. }