新增 全局错误toast提示;

This commit is contained in:
2025-09-14 02:54:08 +08:00
parent 9f94e92eaf
commit 46e07cc5ac
7 changed files with 368 additions and 172 deletions

View File

@@ -47,17 +47,32 @@ export class GeminiService {
contents,
});
// 检查是否有被禁止的内容
if (response.candidates && response.candidates.length > 0) {
const candidate = response.candidates[0];
if (candidate.finishReason === 'PROHIBITED_CONTENT') {
throw new Error('内容被禁止:您的请求包含不允许的内容。请尝试其他提示。');
}
}
const images: string[] = [];
for (const part of response.candidates[0].content.parts) {
if (part.inlineData) {
images.push(part.inlineData.data);
// 检查响应是否存在以及是否有内容
if (response.candidates && response.candidates.length > 0 &&
response.candidates[0].content && response.candidates[0].content.parts) {
for (const part of response.candidates[0].content.parts) {
if (part.inlineData) {
images.push(part.inlineData.data);
}
}
}
return images;
} catch (error) {
console.error('生成图像时出错:', error);
if (error instanceof Error && error.message) {
throw error;
}
throw new Error('生成图像失败。请重试。');
}
}
@@ -100,17 +115,32 @@ export class GeminiService {
contents,
});
// 检查是否有被禁止的内容
if (response.candidates && response.candidates.length > 0) {
const candidate = response.candidates[0];
if (candidate.finishReason === 'PROHIBITED_CONTENT') {
throw new Error('内容被禁止:您的请求包含不允许的内容。请尝试其他提示。');
}
}
const images: string[] = [];
for (const part of response.candidates[0].content.parts) {
if (part.inlineData) {
images.push(part.inlineData.data);
// 检查响应是否存在以及是否有内容
if (response.candidates && response.candidates.length > 0 &&
response.candidates[0].content && response.candidates[0].content.parts) {
for (const part of response.candidates[0].content.parts) {
if (part.inlineData) {
images.push(part.inlineData.data);
}
}
}
return images;
} catch (error) {
console.error('编辑图像时出错:', error);
if (error instanceof Error && error.message) {
throw error;
}
throw new Error('编辑图像失败。请重试。');
}
}
@@ -145,10 +175,21 @@ export class GeminiService {
contents: prompt,
});
// 检查是否有被禁止的内容
if (response.candidates && response.candidates.length > 0) {
const candidate = response.candidates[0];
if (candidate.finishReason === 'PROHIBITED_CONTENT') {
throw new Error('内容被禁止:您的请求包含不允许的内容。请尝试其他提示。');
}
}
const responseText = response.candidates[0].content.parts[0].text;
return JSON.parse(responseText);
} catch (error) {
console.error('分割图像时出错:', error);
if (error instanceof Error && error.message) {
throw error;
}
throw new Error('分割图像失败。请重试。');
}
}