作者 202304001

修复PPT相关BUG

@@ -1742,7 +1742,7 @@ function _Chat() { @@ -1742,7 +1742,7 @@ function _Chat() {
1742 1742
1743 //20250328新增PPT导出 1743 //20250328新增PPT导出
1744 function toPowerpoint(pptMessage: string) { 1744 function toPowerpoint(pptMessage: string) {
1745 - navigate("/powerpoint", { state: { pptMessage: pptMessage, msg: true } }); 1745 + navigate("/powerpoint", { state: { msg: true, pptMessage: pptMessage } });
1746 } 1746 }
1747 1747
1748 return ( 1748 return (
@@ -24,16 +24,11 @@ export function MindPanel(props: MindPanelProps) { @@ -24,16 +24,11 @@ export function MindPanel(props: MindPanelProps) {
24 setIsLoading(true); 24 setIsLoading(true);
25 try { 25 try {
26 const prompt = getMindPrompt(inputValue, false); 26 const prompt = getMindPrompt(inputValue, false);
27 - console.log("请求------------");  
28 const response = await chatStore.directLlmInvoke(prompt, "gpt-4o-mini"); 27 const response = await chatStore.directLlmInvoke(prompt, "gpt-4o-mini");
29 - console.log(response);  
30 const cleanedContent = response.replace(/^```json|```$/g, ""); 28 const cleanedContent = response.replace(/^```json|```$/g, "");
31 - console.log(cleanedContent);  
32 const parsedData: MindElixirData = JSON.parse(cleanedContent); 29 const parsedData: MindElixirData = JSON.parse(cleanedContent);
33 - console.log("-----" + parsedData);  
34 setData(parsedData); 30 setData(parsedData);
35 } catch (error) { 31 } catch (error) {
36 - console.log(error);  
37 message.error(Locale.BgRemoval.error.reqErr); 32 message.error(Locale.BgRemoval.error.reqErr);
38 } finally { 33 } finally {
39 setIsLoading(false); // 确保关闭加载状态‌:ml-citation{ref="2,5" data="citationList"} 34 setIsLoading(false); // 确保关闭加载状态‌:ml-citation{ref="2,5" data="citationList"}
@@ -52,6 +52,31 @@ export function MindPage() { @@ -52,6 +52,31 @@ export function MindPage() {
52 let { msg } = query.state || {}; 52 let { msg } = query.state || {};
53 const [data, setData] = useState<MindElixirData>(INITIAL_DATA); 53 const [data, setData] = useState<MindElixirData>(INITIAL_DATA);
54 54
  55 + const fetchData = async () => {
  56 + if (!msg) return;
  57 + if (!content) return;
  58 + setIsLoading(true);
  59 + try {
  60 + const response = await chatStore.getMindData(newMessages, "gpt-4o-mini");
  61 + const cleanedContent = response.replace(/^```json|```$/g, "");
  62 + const parsedData: MindElixirData = JSON.parse(cleanedContent);
  63 + // 增强校验逻辑
  64 + if (
  65 + !parsedData?.nodeData?.id ||
  66 + !Array.isArray(parsedData.nodeData.children)
  67 + ) {
  68 + throw new Error("数据结构不完整");
  69 + }
  70 + setData(parsedData);
  71 + navigate(Path.Mind, { replace: true, state: { msg: null } });
  72 + } catch (error) {
  73 + console.log(error);
  74 + message.error("请求失败,请重试");
  75 + } finally {
  76 + setIsLoading(false); // 确保关闭加载状态
  77 + }
  78 + };
  79 +
55 useEffect(() => { 80 useEffect(() => {
56 // 确保容器元素已挂载 81 // 确保容器元素已挂载
57 if (!containerRef.current) return; 82 if (!containerRef.current) return;
@@ -59,7 +84,7 @@ export function MindPage() { @@ -59,7 +84,7 @@ export function MindPage() {
59 const options: Options = { 84 const options: Options = {
60 el: containerRef.current, 85 el: containerRef.current,
61 locale: "zh_CN", 86 locale: "zh_CN",
62 - draggable: true, 87 + draggable: false,
63 contextMenu: true, 88 contextMenu: true,
64 toolBar: true, 89 toolBar: true,
65 nodeMenu: true, 90 nodeMenu: true,
@@ -67,39 +92,25 @@ export function MindPage() { @@ -67,39 +92,25 @@ export function MindPage() {
67 // 创建实例 92 // 创建实例
68 mindInstance.current = new MindElixir(options); 93 mindInstance.current = new MindElixir(options);
69 mindInstance.current.init(data); 94 mindInstance.current.init(data);
70 -  
71 - const fetchData = async () => {  
72 - if (msg) {  
73 - if (content) {  
74 - setIsLoading(true);  
75 - try {  
76 - const response = await chatStore.getMindData(  
77 - newMessages,  
78 - "gpt-4o-mini",  
79 - );  
80 - const cleanedContent = response.replace(/^```json|```$/g, "");  
81 - const parsedData: MindElixirData = JSON.parse(cleanedContent);  
82 - // 增强校验逻辑  
83 - if (  
84 - !parsedData?.nodeData?.id ||  
85 - !Array.isArray(parsedData.nodeData.children)  
86 - ) {  
87 - throw new Error("数据结构不完整");  
88 - }  
89 - setData(parsedData);  
90 - navigate(Path.Mind, { replace: true, state: { msg: null } });  
91 - } catch (error) {  
92 - console.log(error);  
93 - message.error("请求失败,请重试");  
94 - } finally {  
95 - setIsLoading(false); // 确保关闭加载状态  
96 - }  
97 - } 95 + const el = mindInstance.current?.container.querySelector("me-root");
  96 + const handleContainerClick = (e: MouseEvent) => {
  97 + const target = e.target as HTMLElement;
  98 + if (target.closest("me-root")) {
  99 + console.log("Clicked me-root element!", target);
98 } 100 }
99 }; 101 };
  102 + mindInstance.current.container.addEventListener(
  103 + "click",
  104 + handleContainerClick,
  105 + );
  106 +
100 fetchData(); 107 fetchData();
101 108
102 return () => { 109 return () => {
  110 + mindInstance.current?.container.removeEventListener(
  111 + "click",
  112 + handleContainerClick,
  113 + );
103 if (mindInstance.current) { 114 if (mindInstance.current) {
104 mindInstance.current.destroy(); 115 mindInstance.current.destroy();
105 } 116 }
@@ -36,9 +36,10 @@ export function PowerPoint() { @@ -36,9 +36,10 @@ export function PowerPoint() {
36 const { msg, pptMessage } = query.state || {}; //获取路由参数 36 const { msg, pptMessage } = query.state || {}; //获取路由参数
37 37
38 const localData: LocalData = getLocalData(accessStore.accessCode); //获取限制次数 38 const localData: LocalData = getLocalData(accessStore.accessCode); //获取限制次数
  39 + const localDataRef = useRef(localData);
39 const getToken = async () => { 40 const getToken = async () => {
40 if (!accessStore.accessCode) { 41 if (!accessStore.accessCode) {
41 - return message.error("请先输入登录秘钥"); 42 + return navigate(Path.Auth);
42 } 43 }
43 const res = await fetch("/api/ppt/createApiToken", { 44 const res = await fetch("/api/ppt/createApiToken", {
44 method: "POST", 45 method: "POST",
@@ -55,7 +56,6 @@ export function PowerPoint() { @@ -55,7 +56,6 @@ export function PowerPoint() {
55 } 56 }
56 return ""; 57 return "";
57 }; 58 };
58 -  
59 useEffect(() => { 59 useEffect(() => {
60 const initializeDocmee = async () => { 60 const initializeDocmee = async () => {
61 let token = localStorage.getItem("token"); 61 let token = localStorage.getItem("token");
@@ -64,13 +64,15 @@ export function PowerPoint() { @@ -64,13 +64,15 @@ export function PowerPoint() {
64 token = await getToken(); 64 token = await getToken();
65 if (!token) { 65 if (!token) {
66 message.error("无效token请检查登录密码!"); 66 message.error("无效token请检查登录密码!");
67 - return navigate(Path.Settings); // 跳转回聊天页 67 + return navigate(Path.Auth);
68 } 68 }
69 localStorage.setItem("token", token); 69 localStorage.setItem("token", token);
70 } 70 }
  71 +
71 if (!containerRef.current) { 72 if (!containerRef.current) {
72 throw new Error("Container element not found"); 73 throw new Error("Container element not found");
73 } 74 }
  75 +
74 const docmee = new DocmeeUI({ 76 const docmee = new DocmeeUI({
75 container: containerRef.current, 77 container: containerRef.current,
76 page: "creator-v2", 78 page: "creator-v2",
@@ -83,19 +85,10 @@ export function PowerPoint() { @@ -83,19 +85,10 @@ export function PowerPoint() {
83 subject: "Ai行业未来10年的发展预测", 85 subject: "Ai行业未来10年的发展预测",
84 }, 86 },
85 }); 87 });
86 - if (msg) {  
87 - docmee.on("mounted", (msg: generateOutline) => {  
88 - if (localData.pptMaxUses == "0") {  
89 - message.error("你的免费次数已用完");  
90 - return false;  
91 - }  
92 - docmee.changeCreatorData({ text: pptMessage }, true);  
93 - });  
94 - } 88 +
95 docmee.on("beforeGenerate", (msg: generateOutline) => { 89 docmee.on("beforeGenerate", (msg: generateOutline) => {
96 //生成大纲事件 90 //生成大纲事件
97 - console.log(localData.pptMaxUses);  
98 - if (localData.pptMaxUses == "0") { 91 + if (localDataRef.current.pptMaxUses == "0") {
99 message.error("你的免费次数已用完"); 92 message.error("你的免费次数已用完");
100 return false; 93 return false;
101 } 94 }
@@ -104,6 +97,16 @@ export function PowerPoint() { @@ -104,6 +97,16 @@ export function PowerPoint() {
104 }); 97 });
105 }); 98 });
106 99
  100 + docmee.on("mounted", (str: string) => {
  101 + if (msg) {
  102 + if (localDataRef.current.pptMaxUses == "0") {
  103 + message.error("你的免费次数已用完");
  104 + return false;
  105 + }
  106 + docmee.changeCreatorData({ text: pptMessage }, true);
  107 + }
  108 + });
  109 +
107 docmee.on("charge", (msg: string) => { 110 docmee.on("charge", (msg: string) => {
108 //PPT生成后扣费 111 //PPT生成后扣费
109 // 解析 pptMaxUses 为数字 112 // 解析 pptMaxUses 为数字
@@ -121,21 +124,37 @@ export function PowerPoint() { @@ -121,21 +124,37 @@ export function PowerPoint() {
121 124
122 docmee.on("error", async (msg: generateError) => { 125 docmee.on("error", async (msg: generateError) => {
123 //请求错误事件 126 //请求错误事件
124 - console.log(msg);  
125 if (msg.data.code == 98) { 127 if (msg.data.code == 98) {
126 // message.error('token失效,请重试') 128 // message.error('token失效,请重试')
127 const token = await getToken(); 129 const token = await getToken();
  130 + localStorage.setItem("token", token);
128 docmee.updateToken(token); 131 docmee.updateToken(token);
129 - return; 132 + if (msg) {
  133 + if (localDataRef.current.pptMaxUses == "0") {
  134 + message.error("你的免费次数已用完");
  135 + return false;
  136 + }
  137 + docmee.changeCreatorData({ text: pptMessage }, true);
  138 + return;
  139 + }
130 } 140 }
131 message.error( 141 message.error(
132 msg.data.code == 403 ? "请检查登录密码或网络" : msg.data.message, 142 msg.data.code == 403 ? "请检查登录密码或网络" : msg.data.message,
133 ); 143 );
134 }); 144 });
135 - return () => docmee.destroy();  
136 }; 145 };
137 - initializeDocmee().catch(console.error);  
138 - }, [navigate, isMobileScreen, msg, pptMessage]); 146 + initializeDocmee();
  147 + }, [
  148 + navigate,
  149 + isMobileScreen,
  150 + pptMessage,
  151 + config.theme,
  152 + accessStore.accessCode,
  153 + ]);
  154 +
  155 + useEffect(() => {
  156 + localDataRef.current = localData;
  157 + }, [localData]);
139 158
140 return ( 159 return (
141 <> 160 <>