index.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import PageCustom from "@/components/page-custom/index";
  2. import { View, Image } from "@tarojs/components";
  3. import { useEffect, useState } from "react";
  4. import ComponentList from "@/components/component-list";
  5. import NavBarNormal from "@/components/NavBarNormal/index";
  6. import Taro, { getCurrentPages, useRouter } from "@tarojs/taro";
  7. import { isSuccess } from "@/utils";
  8. import IconArrowLeft from "@/components/icon/icon-arrow-left";
  9. import IconHomeOutline from "@/components/icon/IconHomeOutline";
  10. import SummaryBar from "@/components/AgentPage/components/SummaryBar";
  11. import { useAgentStore } from "@/store/agentStore";
  12. import style from "./index.module.less";
  13. import { sceneUnzip } from "@/service/wechat";
  14. import { postLog } from "./profile";
  15. import { useIsLogin } from "@/xiaolanbenlib/hooks/data/useAuth";
  16. export default function Profile() {
  17. const { agentProfile, fetchAgentProfile, createAgent } = useAgentStore();
  18. const params = useRouter().params;
  19. const isLogin = useIsLogin();
  20. // 解码
  21. const decodeSceneValue = async (sceneValue: string) => {
  22. const response = await sceneUnzip(sceneValue);
  23. if (isSuccess(response.status)) {
  24. try {
  25. const originalScene = JSON.parse(response.data.originalScene) as {
  26. shareKey?: string;
  27. agentId?: string;
  28. };
  29. if (originalScene.agentId) {
  30. console.log("originalScene:", originalScene);
  31. fetchAgentProfile(
  32. originalScene.agentId,
  33. originalScene.shareKey ?? ""
  34. );
  35. postLog(originalScene.agentId, originalScene.shareKey ?? "");
  36. }
  37. } catch (e) {
  38. Taro.showToast({
  39. title: "没有对应的知能体",
  40. icon: "error",
  41. });
  42. }
  43. }
  44. };
  45. console.log(params.scene, "场景值");
  46. // 通过小程序码识别打开的页面
  47. useEffect(() => {
  48. const scene = decodeURIComponent(params.scene ?? "");
  49. if (scene.length <= 32 && scene.length > 0) {
  50. // 有值,且小于 32 位,说明需要解码
  51. // 判断是否需要解压 shareKey
  52. decodeSceneValue(scene);
  53. }
  54. }, []);
  55. // 正常分享出来的页面
  56. useEffect(() => {
  57. if (params.agentId) {
  58. fetchAgentProfile(params.agentId, params.shareKey ?? "");
  59. isLogin && postLog(params.agentId, params.shareKey);
  60. }
  61. }, [params.agentId]);
  62. const renderNavBarLeft = () => {
  63. const pages = getCurrentPages();
  64. console.log(pages, "当前页面");
  65. // 有后退页,则不显示 home 按钮
  66. if (pages.length > 1 && pages[0].route !== "pages/profile/index") {
  67. return (
  68. <View className={style.navButtons}>
  69. <View
  70. className="flex-center"
  71. onClick={() => {
  72. Taro.navigateBack();
  73. }}
  74. >
  75. <IconArrowLeft></IconArrowLeft>
  76. </View>
  77. </View>
  78. );
  79. }
  80. return (
  81. <View className={style.navButtons}>
  82. <View
  83. className="flex-center"
  84. onClick={() => {
  85. Taro.switchTab({
  86. url: "/pages/index/index",
  87. });
  88. }}
  89. >
  90. <IconHomeOutline></IconHomeOutline>
  91. </View>
  92. </View>
  93. );
  94. // return (
  95. // <View className={style.navButtons}>
  96. // {pages.length > 1 && (
  97. // <>
  98. // <View
  99. // className="flex-center"
  100. // onClick={() => {
  101. // Taro.navigateBack();
  102. // }}
  103. // >
  104. // <IconArrowLeft></IconArrowLeft>
  105. // </View>
  106. // <View className={style.navGapLine}></View>
  107. // </>
  108. // )}
  109. // <View
  110. // className="flex-center"
  111. // onClick={() => {
  112. // Taro.switchTab({
  113. // url: "/pages/index/index",
  114. // });
  115. // }}
  116. // >
  117. // <IconHomeOutline></IconHomeOutline>
  118. // </View>
  119. // </View>
  120. // );
  121. };
  122. const handleCreate = async () => {
  123. try {
  124. const agentDetail = await createAgent();
  125. if (agentDetail) {
  126. if (agentDetail) {
  127. Taro.navigateTo({
  128. url: `/pages/agent/index?agentId=${agentDetail.agentId}`,
  129. });
  130. }
  131. // Taro.navigateTo({
  132. // url: `/pages/agent/index?agentId=${agentDetail.agentId}`
  133. // })
  134. }
  135. } catch (e) {}
  136. };
  137. const renderButton = ()=> {
  138. // 如果智能体交接了,则可以跳去新智能体
  139. if(agentProfile?.toAgentId){
  140. return <View className="button-rounded-big" onClick={()=> {
  141. Taro.navigateTo({
  142. url: `/pages/profile/index?agentId=${agentProfile?.toAgentId}`
  143. });
  144. }}>前往访问</View>
  145. }
  146. // 下线了
  147. return <View className="button-rounded-big">知道了</View>
  148. }
  149. // 如果智能体被删除或其它异常情况
  150. if (agentProfile?.status === "deleted" || agentProfile?.deletedTip?.length) {
  151. return (
  152. <PageCustom fullPage>
  153. <View className="w-full h-full">
  154. <NavBarNormal leftColumn={renderNavBarLeft}></NavBarNormal>
  155. <View className="w-full h-full flex flex-col items-center justify-center">
  156. <View className="max-w-[280px]">
  157. <View className="mb-12">{agentProfile?.deletedTip}</View>
  158. {renderButton()}
  159. </View>
  160. </View>
  161. </View>
  162. </PageCustom>
  163. );
  164. }
  165. return (
  166. <PageCustom styleBg={agentProfile?.avatarUrl}>
  167. <View className="w-full">
  168. <NavBarNormal leftColumn={renderNavBarLeft}></NavBarNormal>
  169. {agentProfile && <View className="blur-rounded-container">
  170. <SummaryBar isVisitor agent={agentProfile}></SummaryBar>
  171. <View className="flex flex-col w-full p-16 gap-12">
  172. <ComponentList
  173. components={agentProfile.components ?? []}
  174. ></ComponentList>
  175. </View>
  176. </View>}
  177. </View>
  178. </PageCustom>
  179. );
  180. }