Pārlūkot izejas kodu

修复项目启动白屏的问题

黎海 2 mēneši atpakaļ
vecāks
revīzija
1642eeacc5

+ 0 - 5
src/App.js

@@ -43,11 +43,6 @@ function App () {
43 43
     
44 44
     initDatabase();
45 45
     
46
-    let user_info = LocalDataService.load_user_data();
47
-    if (!user_info) {
48
-      userProfileRef.current.showLoginModal();
49
-    }
50
-    apiService.setUserProfileRef(userProfileRef);
51 46
   }, []);
52 47
 
53 48
 

+ 2 - 2
src/components/CozeWorkflowDemo.js

@@ -6,7 +6,7 @@ import {
6 6
   speechToText,
7 7
   translateText,
8 8
   summarizeText,
9
-  cozeService
9
+  getCozeService
10 10
 } from '../utils/cozeExample';
11 11
 
12 12
 /**
@@ -183,7 +183,7 @@ const CozeWorkflowDemo = () => {
183 183
         parameters = { text: inputText };
184 184
       }
185 185
       
186
-      const response = await cozeService.runWorkflow(workflowId, parameters);
186
+      const response = await getCozeService().runWorkflow(workflowId, parameters);
187 187
       
188 188
       // 在实际应用中,您可能需要根据工作流的不同输出格式进行不同的处理
189 189
       setOutputText(JSON.stringify(response, null, 2));

+ 8 - 0
src/pages/home/home.js

@@ -26,6 +26,14 @@ const Home = forwardRef((props, ref) => {
26 26
   const [currentVideo, setCurrentVideo] = useState(null);
27 27
   const history = useHistory();
28 28
 
29
+  // 检查Coze API Token
30
+  useEffect(() => {
31
+    // 用户首次访问时,如果没有配置Coze API Token,引导用户进行配置
32
+    if (!hasValidToken()) {
33
+      setShowApiSettings(true);
34
+    }
35
+  }, []);
36
+
29 37
   const handleAddProject = () => {
30 38
     setShowProject(true);
31 39
   };

+ 1 - 1
src/pages/projectDetail/projectDetail.js

@@ -6,7 +6,7 @@ import {
6 6
 } from 'react-bootstrap';
7 7
 import { toast } from 'react-toastify';
8 8
 import { bookService, bookInfoService, initDb } from '../../db';
9
-import { cozeService, initCozeService, getAvailableStyles } from '../../utils/cozeExample';
9
+import { getCozeService, initCozeService, getAvailableStyles } from '../../utils/cozeExample';
10 10
 import { WORKFLOW_IDS, hasValidToken } from '../../utils/cozeConfig';
11 11
 import { getUserSettings } from '../../utils/storageUtils';
12 12
 import taskQueueManager from '../../utils/taskQueueManager';

+ 5 - 5
src/utils/cozeConfig.js

@@ -24,12 +24,12 @@ const storedWorkflowIds = getWorkflowIds() || {};
24 24
 // 工作流ID映射表
25 25
 // 优先使用本地存储的ID,其次使用环境变量,最后使用默认值
26 26
 export const WORKFLOW_IDS = {
27
-  textToDescription: storedWorkflowIds.textToDescription || process.env.REACT_APP_COZE_WORKFLOW_TEXT_TO_DESC || '7485203690664837121',
27
+  textToDescription: storedWorkflowIds.textToDescription || '7485203690664837121',
28 28
   // 添加画风列表和画图工作流
29
-  getStyleList: storedWorkflowIds.getStyleList || process.env.REACT_APP_COZE_WORKFLOW_GET_STYLE_LIST || '7485203690664837122',
30
-  generateImage: storedWorkflowIds.generateImage || process.env.REACT_APP_COZE_WORKFLOW_GENERATE_IMAGE || '7485203690664837123',
29
+  getStyleList: storedWorkflowIds.getStyleList || '7485203690664837122',
30
+  generateImage: storedWorkflowIds.generateImage || '7485203690664837123',
31 31
   // 添加导出剪映草稿工作流
32
-  exportJianyingDraft: storedWorkflowIds.exportJianyingDraft || process.env.REACT_APP_COZE_WORKFLOW_EXPORT_JIANYING_DRAFT || '7485203690664837120',
32
+  exportJianyingDraft: storedWorkflowIds.exportJianyingDraft || '7485203690664837120',
33 33
 };
34 34
 
35 35
 // 工作流名称到显示名称的映射
@@ -62,7 +62,7 @@ export const PARAMETER_PRESETS = {
62 62
     language: 'zh',      // 输出语言:中文
63 63
     maxLength: 500,      // 最大长度
64 64
   },
65
-  
65
+
66 66
   // 图像生成参数预设
67 67
   generateImage: {
68 68
     width: 1024,         // 图像宽度

+ 9 - 7
src/utils/cozeExample.js

@@ -17,11 +17,6 @@ import {
17 17
   blobToBase64
18 18
 } from './cozeStreamHandler';
19 19
 
20
-// 检查是否有有效的API Token
21
-if (!hasValidToken()) {
22
-  console.warn('警告: 未配置有效的Coze API Token,API调用将会失败。请在环境变量中设置REACT_APP_COZE_API_TOKEN。');
23
-}
24
-
25 20
 /**
26 21
  * 初始化Coze服务
27 22
  * @param {Object} config - 配置对象,包含超时、并发请求等设置
@@ -287,5 +282,12 @@ export const createImage = async (prompt, style = '', options = {}) => {
287 282
   }
288 283
 };
289 284
 
290
-// 导出单例
291
-export const cozeService = initCozeService(); 
285
+// 修改导出方式,从预先初始化的单例改为获取单例的函数
286
+// 这样,只有当函数被调用时才会初始化
287
+let _cozeServiceInstance = null;
288
+export const getCozeService = () => {
289
+  if (!_cozeServiceInstance) {
290
+    _cozeServiceInstance = initCozeService();
291
+  }
292
+  return _cozeServiceInstance;
293
+}; 

+ 73 - 13
src/utils/cozeService.js

@@ -16,22 +16,61 @@ class CozeService {
16 16
    * @param {string} config.baseURL - Coze API的基础URL,默认为'https://api.coze.cn'
17 17
    */
18 18
   constructor(config) {
19
-    if (!config || !config.token) {
20
-      throw new Error('必须提供Coze API的访问令牌');
21
-    }
22
-
23
-    this.token = config.token;
24
-    this.baseURL = config.baseURL || 'https://api.coze.cn';
25
-    
26
-    // 初始化Coze API客户端
27
-    this.apiClient = new CozeAPI({
28
-      token: this.token,
29
-      baseURL: this.baseURL,
30
-      allowPersonalAccessTokenInBrowser: true // 允许在浏览器环境中使用个人访问令牌
31
-    });
19
+    this.token = config?.token || '';
20
+    this.baseURL = config?.baseURL || 'https://api.coze.cn';
21
+    
22
+    // 延迟初始化标志
23
+    this.apiClientInitialized = false;
32 24
     
33 25
     // 用于存储工作流ID与名称的映射
34 26
     this.workflows = {};
27
+    
28
+    // 如果有token,立即初始化API客户端
29
+    if (this.token) {
30
+      this.initApiClient();
31
+    }
32
+  }
33
+  
34
+  /**
35
+   * 初始化API客户端
36
+   * @returns {boolean} 初始化是否成功
37
+   */
38
+  initApiClient() {
39
+    try {
40
+      if (!this.token) {
41
+        return false;
42
+      }
43
+      
44
+      // 初始化Coze API客户端
45
+      this.apiClient = new CozeAPI({
46
+        token: this.token,
47
+        baseURL: this.baseURL,
48
+        allowPersonalAccessTokenInBrowser: true // 允许在浏览器环境中使用个人访问令牌
49
+      });
50
+      
51
+      this.apiClientInitialized = true;
52
+      return true;
53
+    } catch (error) {
54
+      console.error('初始化Coze API客户端失败:', error);
55
+      return false;
56
+    }
57
+  }
58
+  
59
+  /**
60
+   * 检查API客户端是否已初始化,如果未初始化则尝试初始化
61
+   * @throws {Error} 如果无法初始化API客户端则抛出错误
62
+   */
63
+  checkApiClient() {
64
+    if (!this.apiClientInitialized) {
65
+      if (!this.token) {
66
+        throw new Error('必须提供Coze API的访问令牌');
67
+      }
68
+      
69
+      const initialized = this.initApiClient();
70
+      if (!initialized) {
71
+        throw new Error('Coze API客户端初始化失败');
72
+      }
73
+    }
35 74
   }
36 75
 
37 76
   /**
@@ -74,6 +113,9 @@ class CozeService {
74 113
    * @returns {Promise<Object>} - 返回API调用的响应
75 114
    */
76 115
   async runWorkflowStream(workflowNameOrId, parameters = {}, options = {}) {
116
+    // 检查API客户端初始化
117
+    this.checkApiClient();
118
+    
77 119
     // 获取工作流ID
78 120
     const workflowId = this.workflows[workflowNameOrId] || workflowNameOrId;
79 121
     
@@ -101,6 +143,9 @@ class CozeService {
101 143
    * @returns {Promise<Object>} - 返回API调用的响应
102 144
    */
103 145
   async runWorkflow(workflowNameOrId, parameters = {}, options = {}) {
146
+    // 检查API客户端初始化
147
+    this.checkApiClient();
148
+    
104 149
     // 获取工作流ID
105 150
     const workflowId = this.workflows[workflowNameOrId] || workflowNameOrId;
106 151
     
@@ -129,6 +174,9 @@ class CozeService {
129 174
    * @returns {Promise<Object>} - 返回工作流运行的状态
130 175
    */
131 176
   async getWorkflowRunStatus(runId) {
177
+    // 检查API客户端初始化
178
+    this.checkApiClient();
179
+    
132 180
     if (!runId) {
133 181
       throw new Error('必须提供工作流运行ID');
134 182
     }
@@ -149,6 +197,9 @@ class CozeService {
149 197
    * @returns {Promise<Object>} - 返回取消操作的结果
150 198
    */
151 199
   async cancelWorkflowRun(runId) {
200
+    // 检查API客户端初始化
201
+    this.checkApiClient();
202
+    
152 203
     if (!runId) {
153 204
       throw new Error('必须提供工作流运行ID');
154 205
     }
@@ -169,6 +220,9 @@ class CozeService {
169 220
    * @returns {Promise<Object>} - 返回工作流列表
170 221
    */
171 222
   async listWorkflows(options = {}) {
223
+    // 检查API客户端初始化
224
+    this.checkApiClient();
225
+    
172 226
     try {
173 227
       return await this.apiClient.workflows.list(options);
174 228
     } catch (error) {
@@ -183,6 +237,9 @@ class CozeService {
183 237
    * @returns {Promise<Array>} - 返回可用的画风列表
184 238
    */
185 239
   async getStyleList(options = {}) {
240
+    // 检查API客户端初始化
241
+    this.checkApiClient();
242
+    
186 243
     try {
187 244
       // 使用runWorkflow方法调用画风列表API
188 245
       console.log('获取画风列表...');
@@ -211,6 +268,9 @@ class CozeService {
211 268
    * @returns {Promise<Object>} - 返回生成的图片信息
212 269
    */
213 270
   async generateImage(prompt, style = '', options = {}) {
271
+    // 检查API客户端初始化
272
+    this.checkApiClient();
273
+    
214 274
     try {
215 275
       if (!prompt) {
216 276
         throw new Error('必须提供画图的文本提示');