Skip to content

Let's use AI to Earn!

api模板

daoyianf648c9640e35
diff --git a/docs/api.json b/docs/api.json
index 0b321ad9..a7a093c4 100644
--- a/docs/api.json
+++ b/docs/api.json
@@ -11980,7 +11980,7 @@
"type": "string"
},
"description": {
- "type": "string"
+ "type": "string"
},
"inputModalities": {
"description": "输入模态",
diff --git a/src/api/ai.ts b/src/api/ai.ts
index a24d2a7a..d58d7c2b 100644
--- a/src/api/ai.ts
+++ b/src/api/ai.ts
@@ -1,6 +1,65 @@
import http from "@/utils/request";
-// 文生图
+// 文生图 - 新的接口
+export const generateImage = (data: {
+ prompt: string;
+ model?: string;
+ n?: number;
+ quality?: "standard" | "hd";
+ response_format?: "url" | "b64_json";
+ size?: string;
+ style?: "vivid" | "natural";
+ user?: string;
+}) => {
+ return http.post("/ai/image/generate", data);
+};
+
+// 流光卡片生成 - 新的接口
+export const generateFireflyCard = (data: {
+ content: string;
+ temp?: string;
+ title?: string;
+ style?: {
+ align?: string;
+ backgroundName?: string;
+ backShadow?: string;
+ font?: string;
+ width?: number;
+ ratio?: string;
+ height?: number;
+ fontScale?: number;
+ padding?: string;
+ borderRadius?: string;
+ color?: string;
+ opacity?: number;
+ blur?: number;
+ backgroundAngle?: string;
+ lineHeights?: {
+ content?: string;
+ title?: string;
+ };
+ };
+}) => {
+ return http.post("/ai/fireflycard", data);
+};
+
+// 获取图片生成模型参数
+export const getImageGenerationModels = () => {
+ return http.get("/ai/models/image/generation");
+};
+
+// 获取图片编辑模型参数
+export const getImageEditModels = () => {
+ return http.get("/ai/models/image/edit");
+};
+
+// 获取视频生成模型参数
+export const getVideoGenerationModels = () => {
+ return http.get("/ai/models/video/generation");
+};
+
+// 保留旧的接口以保持向后兼容性(可选)
+// 文生图 - 旧接口(已废弃)
export const textToImage = (data: {
prompt: string;
width: number;
@@ -10,7 +69,7 @@ export const textToImage = (data: {
return http.post("/tools/ai/jm/task", data);
};
-// 获取文生图任务结果
+// 获取文生图任务结果 - 旧接口(已废弃)
export const getTextToImageTaskResult = (id: string) => {
return http.get<{
imgList: string[];
@@ -19,7 +78,7 @@ export const getTextToImageTaskResult = (id: string) => {
}>(`/tools/ai/jm/task/${id}`);
};
-// 文生图文(流光卡片)
+// 文生图文(流光卡片)- 旧接口(已废弃)
export const textToFireflyCard = (data: {
content: string;
temp: string;
diff --git a/src/app/[lng]/material/ai-generate/ai-generate.module.scss b/src/app/[lng]/material/ai-generate/ai-generate.module.scss
index b92bb24f..2dcc43d9 100644
--- a/src/app/[lng]/material/ai-generate/ai-generate.module.scss
+++ b/src/app/[lng]/material/ai-generate/ai-generate.module.scss
@@ -183,6 +183,11 @@
gap: 16px;
}
+.options {
+ display: flex;
+ gap: 16px;
+}
+
.polling {
margin-top: 16px;
text-align: center;
@@ -253,4 +258,10 @@
.section {
padding: 16px;
}
+
+ .dimensions,
+ .options {
+ flex-direction: column;
+ gap: 12px;
+ }
}
\ No newline at end of file
diff --git a/src/app/[lng]/material/ai-generate/page.tsx b/src/app/[lng]/material/ai-generate/page.tsx
index 7d9b72af..3de52d5c 100644
--- a/src/app/[lng]/material/ai-generate/page.tsx
+++ b/src/app/[lng]/material/ai-generate/page.tsx
@@ -23,9 +23,9 @@ import {
} from "@ant-design/icons";
import styles from "./ai-generate.module.scss";
import {
- textToImage,
- getTextToImageTaskResult,
- textToFireflyCard,
+ generateImage,
+ generateFireflyCard,
+ getImageGenerationModels,
} from "@/api/ai";
import { getOssUrl } from "@/utils/oss";
import { getMediaGroupList, createMedia } from "@/api/media";
@@ -42,17 +42,13 @@ export default function AIGeneratePage() {
const albumId = params.id as string;
const [prompt, setPrompt] = useState("");
- const [width, setWidth] = useState(520);
- const [height, setHeight] = useState(520);
- // TODO: 由用户自行传入
- const [sessionIds, setSessionIds] = useState<string[]>([
- "0e2bdef17755a3f121b608ec8a763f6b",
- "7e90a4c9bb3c6c8b7056267f27395c78",
- ]);
+ const [size, setSize] = useState("1024x1024");
+ const [n, setN] = useState(1);
+ const [quality, setQuality] = useState<"standard" | "hd">("standard");
+ const [style, setStyle] = useState<"vivid" | "natural">("vivid");
+ const [model, setModel] = useState("");
const [loading, setLoading] = useState(false);
- const [taskId, setTaskId] = useState<string | null>(null);
const [result, setResult] = useState<string[] | null>(null);
- const [polling, setPolling] = useState(false);
const [content, setContent] = useState("");
const [title, setTitle] = useState("");
@@ -68,6 +64,27 @@ export default function AIGeneratePage() {
const [uploading, setUploading] = useState(false);
const [loadingMediaGroups, setLoadingMediaGroups] = useState(false);
+ const [imageModels, setImageModels] = useState<any[]>([]);
+
+ // 获取图片生成模型
+ const fetchImageModels = async () => {
+ try {
+ const response: any = await getImageGenerationModels();
+ if (response.data) {
+ setImageModels(response.data);
+ if (response.data.length > 0) {
+ setModel(response.data[0].name);
+ }
+ }
+ } catch (error) {
+ console.error("获取图片生成模型失败:", error);
+ }
+ };
+
+ useEffect(() => {
+ fetchImageModels();
+ }, []);
+
const fetchMediaGroups = async () => {
try {
setLoadingMediaGroups(true);
@@ -90,46 +107,29 @@ export default function AIGeneratePage() {
try {
setLoading(true);
- const response: any = await textToImage({
+ const response: any = await generateImage({
prompt,
- width,
- height,
- sessionIds,
+ n,
+ quality,
+ style,
+ size,
+ model,
+ response_format: "url",
});
- if (response.data) {
- setTaskId(response.data);
- setPolling(true);
- pollTaskResult(response.data);
+ if (response.data && response.data.list) {
+ const imageUrls = response.data.list.map((item: any) => item.url);
+ setResult(imageUrls);
} else {
- message.error("生成任务创建失败");
+ message.error("生成图片失败");
}
} catch (error) {
- message.error("生成任务创建失败");
+ message.error("生成图片失败");
} finally {
setLoading(false);
}
};
- const pollTaskResult = async (id: string) => {
- try {
- const response = await getTextToImageTaskResult(id);
- if (!response) return;
- if (response.data.status === "success") {
- setResult(response.data.imgList);
- setPolling(false);
- } else if (response.data.status === "failed") {
- message.error("生成任务失败");
- setPolling(false);
- } else {
- setTimeout(() => pollTaskResult(id), 2000);
- }
- } catch (error) {
- message.error("获取任务结果失败");
- setPolling(false);
- }
- };
-
const handleTextToFireflyCard = async () => {
if (!content || !title) {
message.error("请输入内容和标题");
@@ -138,14 +138,16 @@ export default function AIGeneratePage() {
try {
setLoadingFirefly(true);
- const response: any = await textToFireflyCard({
+ const response: any = await generateFireflyCard({
+
+
content,
temp,
title,
});
- if (response.data) {
- setFireflyResult(response.data);
+ if (response.data && response.data.image) {
+ setFireflyResult(response.data.image);
} else {
message.error("生成流光卡片失败");
}
@@ -170,9 +172,11 @@ export default function AIGeneratePage() {
try {
setUploading(true);
+ // 将base64数据转换为可用的URL格式
+ const imageUrl = `data:image/png;base64,${fireflyResult}`;
const response: any = await createMedia({
groupId: selectedMediaGroup,
- url: fireflyResult,
+ url: imageUrl,
type: "img",
title: title,
desc: "",
@@ -223,21 +227,61 @@ export default function AIGeneratePage() {
rows={4}
/>
<div className={styles.dimensions}>
- <Input
- type="number"
- placeholder={t('aiGenerate.width')}
- value={width}
- onChange={(e) => setWidth(Number(e.target.value))}
- min={520}
- />
- <Input
- type="number"
- placeholder={t('aiGenerate.height')}
- value={height}
- onChange={(e) => setHeight(Number(e.target.value))}
- min={520}
- />
+ <Select
+ placeholder="选择尺寸"
+ value={size}
+ onChange={setSize}
+ style={{ width: "100%" }}
+ >
+ <Option value="1024x1024">1024x1024</Option>
+ <Option value="1792x1024">1792x1024</Option>
+ <Option value="1024x1792">1024x1792</Option>
+ </Select>
+ <Select
+ placeholder="生成数量"
+ value={n}
+ onChange={setN}
+ style={{ width: "100%" }}
+ >
+ {[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(num => (
+ <Option key={num} value={num}>{num}</Option>
+ ))}
+ </Select>
</div>
+ <div className={styles.options}>
+ <Select
+ placeholder="图片质量"
+ value={quality}
+ onChange={setQuality}
+ style={{ width: "100%" }}
+ >
+ <Option value="standard">标准</Option>
+ <Option value="hd">高清</Option>
+ </Select>
+ <Select
+ placeholder="图片风格"
+ value={style}
+ onChange={setStyle}
+ style={{ width: "100%" }}
+ >
+ <Option value="vivid">生动</Option>
+ <Option value="natural">自然</Option>
+ </Select>
+ </div>
+ {imageModels.length > 0 && (
+ <Select
+ placeholder="选择模型"
+ value={model}
+ onChange={setModel}
+ style={{ width: "100%" }}
+ >
+ {imageModels.map((modelItem) => (
+ <Option key={modelItem.name} value={modelItem.name}>
+ {modelItem.description || modelItem.name}
+ </Option>
+ ))}
+ </Select>
+ )}
<Button
type="primary"
onClick={handleTextToImage}
@@ -248,11 +292,6 @@ export default function AIGeneratePage() {
{t('aiGenerate.generate')}
</Button>
</div>
- {polling && (
- <div className={styles.polling}>
- <Spin tip={t('aiGenerate.generating')} />
- </div>
- )}
{result && (
<div className={styles.result}>
<Row gutter={[16, 16]}>
@@ -300,6 +339,14 @@ export default function AIGeneratePage() {
<Option value="tempA">{t('aiGenerate.template')} A</Option>
<Option value="tempB">{t('aiGenerate.template')} B</Option>
<Option value="tempC">{t('aiGenerate.template')} C</Option>
+ <Option value="tempJin">金卡模板</Option>
+ <Option value="tempMemo">备忘录模板</Option>
+ <Option value="tempEasy">简约模板</Option>
+ <Option value="tempBlackSun">黑日模板</Option>
+ <Option value="tempE">模板 E</Option>
+ <Option value="tempWrite">写作模板</Option>
+ <Option value="code">代码模板</Option>
+ <Option value="tempD">模板 D</Option>
</Select>
<Button
type="primary"
@@ -321,7 +368,7 @@ export default function AIGeneratePage() {
}}
>
<img
- src={getOssUrl(fireflyResult)}
+ src={`data:image/png;base64,${fireflyResult}`}
alt={t('aiGenerate.fireflyCard')}
style={{ maxWidth: "100%", borderRadius: "8px" }}
/>
diff --git a/src/app/i18n/locales/en/material.json b/src/app/i18n/locales/en/material.json
index 85cd4524..9a2e9ff7 100644
--- a/src/app/i18n/locales/en/material.json
+++ b/src/app/i18n/locales/en/material.json
@@ -12,7 +12,6 @@
"generating": "Generating...",
"content": "Content",
"contentPlaceholder": "Enter content",
- "title": "Title",
"titlePlaceholder": "Enter title",
"template": "Template",
"uploadToMediaGroup": "Upload to Media Group",
@@ -26,6 +25,23 @@
"pleaseEnterPrompt": "Please enter prompt",
"pleaseSelectMediaGroup": "Please select media group",
"pleaseEnterContent": "Please enter content",
- "pleaseEnterTitle": "Please enter title"
+ "pleaseEnterTitle": "Please enter title",
+ "selectSize": "Select Size",
+ "generateCount": "Generate Count",
+ "imageQuality": "Image Quality",
+ "imageStyle": "Image Style",
+ "selectModel": "Select Model",
+ "standard": "Standard",
+ "hd": "HD",
+ "vivid": "Vivid",
+ "natural": "Natural",
+ "goldCardTemplate": "Gold Card Template",
+ "memoTemplate": "Memo Template",
+ "simpleTemplate": "Simple Template",
+ "blackSunTemplate": "Black Sun Template",
+ "templateE": "Template E",
+ "writingTemplate": "Writing Template",
+ "codeTemplate": "Code Template",
+ "templateD": "Template D"
}
}
\ No newline at end of file
diff --git a/src/app/i18n/locales/zh-CN/material.json b/src/app/i18n/locales/zh-CN/material.json
index d72cd67f..dd4860eb 100644
--- a/src/app/i18n/locales/zh-CN/material.json
+++ b/src/app/i18n/locales/zh-CN/material.json
@@ -12,7 +12,6 @@
"generating": "生成中...",
"content": "内容",
"contentPlaceholder": "请输入内容",
- "title": "标题",
"titlePlaceholder": "请输入标题",
"template": "模板",
"uploadToMediaGroup": "上传到媒体组",
@@ -26,6 +25,23 @@
"pleaseEnterPrompt": "请输入提示词",
"pleaseSelectMediaGroup": "请选择媒体组",
"pleaseEnterContent": "请输入内容",
- "pleaseEnterTitle": "请输入标题"
+ "pleaseEnterTitle": "请输入标题",
+ "selectSize": "选择尺寸",
+ "generateCount": "生成数量",
+ "imageQuality": "图片质量",
+ "imageStyle": "图片风格",
+ "selectModel": "选择模型",
+ "standard": "标准",
+ "hd": "高清",
+ "vivid": "生动",
+ "natural": "自然",
+ "goldCardTemplate": "金卡模板",
+ "memoTemplate": "备忘录模板",
+ "simpleTemplate": "简约模板",
+ "blackSunTemplate": "黑日模板",
+ "templateE": "模板 E",
+ "writingTemplate": "写作模板",
+ "codeTemplate": "代码模板",
+ "templateD": "模板 D"
}
}
\ No newline at end of file