powerpoint.tsx
6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import styles from "./powerpoint.module.scss";
import chatStyles from "@/app/components/chat.module.scss";
import clsx from "clsx";
import { IconButton } from "../button";
import Locale from "@/app/locales";
import { useNavigate, useLocation } from "react-router-dom";
import { useMobileScreen } from "@/app/utils";
import { Path } from "@/app/constant";
import { useRef, useEffect, useMemo } from "react";
import { CreatorType, DocmeeUI } from "@docmee/sdk-ui";
import { getClientConfig } from "@/app/config/client";
import { useAppConfig, useAccessStore } from "@/app/store";
import type { generateError, generateOutline } from "@/app/types/docmee";
import type { LocalData } from "@/app/types/zuotang";
import { useLocalStorage } from "../bgRemoval";
import axios from "axios";
import ReturnIcon from "@/app/icons/return.svg";
import MinIcon from "@/app/icons/min.svg";
import MaxIcon from "@/app/icons/max.svg";
import { message } from "antd";
export function PowerPoint() {
const accessStore = useAccessStore();
const containerRef = useRef<HTMLDivElement>(null);
const isPowerpoint = location.pathname === Path.Powerpoint;
const isMobileScreen = useMobileScreen();
const navigate = useNavigate();
const clientConfig = useMemo(() => getClientConfig(), []);
const showMaxIcon = !isMobileScreen && !clientConfig?.isApp;
const config = useAppConfig();
const scrollRef = useRef<HTMLDivElement>(null);
const { updateLocalUsage, getLocalData } = useLocalStorage();
const query = useLocation(); //获取路由参数
const { msg, pptMessage } = query.state || {}; //获取路由参数
const localData: LocalData = getLocalData(accessStore.accessCode); //获取限制次数
const localDataRef = useRef(localData);
const getToken = async () => {
if (!accessStore.accessCode) {
return navigate(Path.Auth);
}
const res = await fetch("/api/ppt/createApiToken", {
method: "POST",
body: JSON.stringify({
accessCode: accessStore.accessCode,
max_use: localData.pptMaxUses,
}),
});
const data = await res.json();
if (data.status == 200) {
return data.data.data.token;
} else {
message.error(data.error || "获取 token 失败");
}
return "";
};
useEffect(() => {
const initializeDocmee = async () => {
let token = localStorage.getItem("token");
// 如果本地没有token,则获取新token
if (!token) {
token = await getToken();
if (!token) {
message.error("无效token请检查登录密码!");
return navigate(Path.Auth);
}
localStorage.setItem("token", token);
}
if (!containerRef.current) {
throw new Error("Container element not found");
}
const docmee = new DocmeeUI({
container: containerRef.current,
page: "creator-v2",
token: token,
mode: config.theme as "dark" | "light",
lang: "zh",
isMobile: isMobileScreen,
creatorData: {
type: CreatorType.AI_GEN,
subject: "Ai行业未来10年的发展预测",
},
});
docmee.on("beforeGenerate", (msg: generateOutline) => {
//生成大纲事件
if (localDataRef.current.pptMaxUses == "0") {
message.error("你的免费次数已用完");
return false;
}
axios.post("https://docmee.cn/api/ppt/v2/generateContent", msg, {
headers: { token: token },
});
});
docmee.on("mounted", (str: string) => {
if (msg) {
if (localDataRef.current.pptMaxUses == "0") {
message.error("你的免费次数已用完");
return false;
}
docmee.changeCreatorData({ text: pptMessage }, true);
}
});
docmee.on("charge", (msg: string) => {
//PPT生成后扣费
// 解析 pptMaxUses 为数字
let max_use = parseInt(localData.pptMaxUses, 10);
// 校验解析结果是否为有效数字
if (isNaN(max_use)) {
max_use = 0; // 如果解析失败,设置默认值为 0
}
max_use = max_use - 1;
if (max_use < 0) {
max_use = 0;
}
updateLocalUsage(accessStore.accessCode, undefined, max_use);
});
docmee.on("error", async (msg: generateError) => {
//请求错误事件
if (msg.data.code == 98) {
// message.error('token失效,请重试')
const token = await getToken();
localStorage.setItem("token", token);
docmee.updateToken(token);
if (msg) {
if (localDataRef.current.pptMaxUses == "0") {
message.error("你的免费次数已用完");
return false;
}
docmee.changeCreatorData({ text: pptMessage }, true);
return;
}
}
message.error(
msg.data.code == 403 ? "请检查登录密码或网络" : msg.data.message,
);
});
};
initializeDocmee();
}, [
navigate,
isMobileScreen,
pptMessage,
config.theme,
accessStore.accessCode,
]);
useEffect(() => {
localDataRef.current = localData;
}, [localData]);
return (
<>
<div style={{ width: "100%", height: "100%" }}>
<div className={chatStyles.chat} key={"1"}>
<div className="window-header" data-tauri-drag-region>
<div
className={clsx(
"window-header-title",
chatStyles["chat-body-title"],
)}
>
<div className={`window-header-main-title`}>PPT制作</div>
</div>
<div className={chatStyles["chat-message-actions"]}>
<div className={chatStyles["chat-input-actions"]}></div>
</div>
<div className="window-actions">
<IconButton
aria="返回首页"
icon={<ReturnIcon />}
bordered
title={Locale.Chat.Actions.ChatList}
onClick={() => navigate(Path.Chat)}
/>
{showMaxIcon && (
<div className="window-action-button">
<IconButton
aria={Locale.Chat.Actions.FullScreen}
icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
bordered
onClick={() => {
config.update(
(config) => (config.tightBorder = !config.tightBorder),
);
}}
/>
</div>
)}
</div>
</div>
<div className={chatStyles["chat-body"]} ref={scrollRef}>
<div ref={containerRef} className={styles["container"]}></div>
</div>
</div>
</div>
</>
);
}