feat:【antd】【ai】image 的代码优化

This commit is contained in:
YunaiV
2025-11-15 13:50:48 +08:00
parent 51fb4b479e
commit 7c7a843e94
8 changed files with 31 additions and 50 deletions

View File

@@ -11,11 +11,11 @@ import { Segmented } from 'ant-design-vue';
import { getModelSimpleList } from '#/api/ai/model/model'; import { getModelSimpleList } from '#/api/ai/model/model';
import Common from './components/common/index.vue'; import Common from './modules/common/index.vue';
import Dall3 from './components/dall3/index.vue'; import Dall3 from './modules/dall3/index.vue';
import ImageList from './components/ImageList.vue'; import ImageList from './modules/list.vue';
import Midjourney from './components/midjourney/index.vue'; import Midjourney from './modules/midjourney/index.vue';
import StableDiffusion from './components/stableDiffusion/index.vue'; import StableDiffusion from './modules/stable-diffusion/index.vue';
const imageListRef = ref<any>(); // image 列表 ref const imageListRef = ref<any>(); // image 列表 ref
const dall3Ref = ref<any>(); // dall3(openai) ref const dall3Ref = ref<any>(); // dall3(openai) ref
@@ -23,7 +23,6 @@ const midjourneyRef = ref<any>(); // midjourney ref
const stableDiffusionRef = ref<any>(); // stable diffusion ref const stableDiffusionRef = ref<any>(); // stable diffusion ref
const commonRef = ref<any>(); // stable diffusion ref const commonRef = ref<any>(); // stable diffusion ref
// 定义属性
const selectPlatform = ref('common'); // 选中的平台 const selectPlatform = ref('common'); // 选中的平台
const platformOptions = [ const platformOptions = [
{ {
@@ -43,7 +42,6 @@ const platformOptions = [
value: AiPlatformEnum.STABLE_DIFFUSION, value: AiPlatformEnum.STABLE_DIFFUSION,
}, },
]; ];
const models = ref<AiModelModelApi.Model[]>([]); // 模型列表 const models = ref<AiModelModelApi.Model[]>([]); // 模型列表
/** 绘画 start */ /** 绘画 start */

View File

@@ -11,8 +11,6 @@ import { IconifyIcon } from '@vben/icons';
import { Button, Card, Image, message } from 'ant-design-vue'; import { Button, Card, Image, message } from 'ant-design-vue';
//
const props = defineProps({ const props = defineProps({
detail: { detail: {
type: Object as PropType<AiImageApi.Image>, type: Object as PropType<AiImageApi.Image>,
@@ -32,7 +30,6 @@ async function handleButtonClick(type: string, detail: AiImageApi.Image) {
async function handleMidjourneyBtnClick( async function handleMidjourneyBtnClick(
button: AiImageApi.ImageMidjourneyButtons, button: AiImageApi.ImageMidjourneyButtons,
) { ) {
//
await confirm(`确认操作 "${button.label} ${button.emoji}" ?`); await confirm(`确认操作 "${button.label} ${button.emoji}" ?`);
emits('onMjBtnClick', button, props.detail); emits('onMjBtnClick', button, props.detail);
} }
@@ -43,6 +40,7 @@ watch(detail, async (newVal) => {
await handleLoading(newVal.status); await handleLoading(newVal.status);
}); });
const loading = ref(); const loading = ref();
/** 处理加载状态 */ /** 处理加载状态 */
async function handleLoading(status: number) { async function handleLoading(status: number) {
// loading // loading
@@ -50,10 +48,11 @@ async function handleLoading(status: number) {
loading.value = message.loading({ loading.value = message.loading({
content: `生成中...`, content: `生成中...`,
}); });
// loading
} else { } else {
if (loading.value) setTimeout(loading.value, 100); // loading
if (loading.value) {
setTimeout(loading.value, 100);
}
} }
} }

View File

@@ -16,21 +16,17 @@ import { Button, InputNumber, Select, Space, Textarea } from 'ant-design-vue';
import { drawImage } from '#/api/ai/image'; import { drawImage } from '#/api/ai/image';
//
//
const props = defineProps({ const props = defineProps({
models: { models: {
type: Array<AiModelModelApi.Model>, type: Array<AiModelModelApi.Model>,
default: () => [] as AiModelModelApi.Model[], default: () => [] as AiModelModelApi.Model[],
}, },
}); }); //
const emits = defineEmits(['onDrawStart', 'onDrawComplete']); const emits = defineEmits(['onDrawStart', 'onDrawComplete']);
//
const drawIn = ref<boolean>(false); // const drawIn = ref<boolean>(false); //
const selectHotWord = ref<string>(''); // const selectHotWord = ref<string>(''); //
//
const prompt = ref<string>(''); // const prompt = ref<string>(''); //
const width = ref<number>(512); // const width = ref<number>(512); //
const height = ref<number>(512); // const height = ref<number>(512); //
@@ -45,7 +41,6 @@ async function handleHotWordClick(hotWord: string) {
selectHotWord.value = ''; selectHotWord.value = '';
return; return;
} }
// //
selectHotWord.value = hotWord; // selectHotWord.value = hotWord; //
prompt.value = hotWord; // prompt.value = hotWord; //
@@ -91,11 +86,11 @@ async function handlerPlatformChange(platform: any) {
platformModels.value = props.models.filter( platformModels.value = props.models.filter(
(item: AiModelModelApi.Model) => item.platform === platform, (item: AiModelModelApi.Model) => item.platform === platform,
); );
//
modelId.value = modelId.value =
platformModels.value.length > 0 && platformModels.value[0] platformModels.value.length > 0 && platformModels.value[0]
? platformModels.value[0].id ? platformModels.value[0].id
: undefined; : undefined;
//
} }
/** 监听 models 变化 */ /** 监听 models 变化 */

View File

@@ -20,16 +20,14 @@ import { Button, Image, message, Space, Textarea } from 'ant-design-vue';
import { drawImage } from '#/api/ai/image'; import { drawImage } from '#/api/ai/image';
//
const props = defineProps({ const props = defineProps({
models: { models: {
type: Array<AiModelModelApi.Model>, type: Array<AiModelModelApi.Model>,
default: () => [] as AiModelModelApi.Model[], default: () => [] as AiModelModelApi.Model[],
}, },
}); }); //
const emits = defineEmits(['onDrawStart', 'onDrawComplete']); const emits = defineEmits(['onDrawStart', 'onDrawComplete']);
//
const prompt = ref<string>(''); // const prompt = ref<string>(''); //
const drawIn = ref<boolean>(false); // const drawIn = ref<boolean>(false); //
const selectHotWord = ref<string>(''); // const selectHotWord = ref<string>(''); //
@@ -44,7 +42,6 @@ async function handleHotWordClick(hotWord: string) {
selectHotWord.value = ''; selectHotWord.value = '';
return; return;
} }
// //
selectHotWord.value = hotWord; selectHotWord.value = hotWord;
prompt.value = hotWord; prompt.value = hotWord;

View File

@@ -16,16 +16,16 @@ import { Image } from 'ant-design-vue';
import { getImageMy } from '#/api/ai/image'; import { getImageMy } from '#/api/ai/image';
//
const props = defineProps({ const props = defineProps({
id: { id: {
type: Number, type: Number,
required: true, required: true,
}, },
}); });
const detail = ref<AiImageApi.Image>({} as AiImageApi.Image);
/** 获取图片详情 */ const detail = ref<AiImageApi.Image>({} as AiImageApi.Image); //
/** 获取图片详情 */
async function getImageDetail(id: number) { async function getImageDetail(id: number) {
detail.value = await getImageMy(id); detail.value = await getImageMy(id);
} }

View File

@@ -18,10 +18,8 @@ import {
midjourneyAction, midjourneyAction,
} from '#/api/ai/image'; } from '#/api/ai/image';
import ImageCard from './ImageCard.vue'; import ImageCard from './card.vue';
import ImageDetail from './ImageDetail.vue'; import ImageDetail from './detail.vue';
//
const emits = defineEmits(['onRegeneration']); const emits = defineEmits(['onRegeneration']);
const router = useRouter(); const router = useRouter();
@@ -29,15 +27,14 @@ const [Drawer, drawerApi] = useVbenDrawer({
title: '图片详情', title: '图片详情',
footer: false, footer: false,
}); });
//
const queryParams = reactive({ const queryParams = reactive({
pageNo: 1, pageNo: 1,
pageSize: 10, pageSize: 10,
}); }); //
const pageTotal = ref<number>(0); // page size const pageTotal = ref<number>(0); // page size
const imageList = ref<AiImageApi.Image[]>([]); // image const imageList = ref<AiImageApi.Image[]>([]); // image
const imageListRef = ref<any>(); // ref const imageListRef = ref<any>(); // ref
//
const inProgressImageMap = ref<{}>({}); // image key image value image const inProgressImageMap = ref<{}>({}); // image key image value image
const inProgressTimer = ref<any>(); // image const inProgressTimer = ref<any>(); // image
const showImageDetailId = ref<number>(0); // const showImageDetailId = ref<number>(0); //
@@ -60,7 +57,6 @@ async function getImageList() {
}); });
try { try {
// 1. // 1.
const { list, total } = await getImagePageMy(queryParams); const { list, total } = await getImagePageMy(queryParams);
imageList.value = list; imageList.value = list;
pageTotal.value = total; pageTotal.value = total;
@@ -78,6 +74,7 @@ async function getImageList() {
loading(); loading();
} }
} }
const debounceGetImageList = useDebounceFn(getImageList, 80); const debounceGetImageList = useDebounceFn(getImageList, 80);
/** 轮询生成中的 image 列表 */ /** 轮询生成中的 image 列表 */
async function refreshWatchImages() { async function refreshWatchImages() {
@@ -132,7 +129,7 @@ async function handleImageButtonClick(
} }
// //
if (type === 'regeneration') { if (type === 'regeneration') {
await emits('onRegeneration', imageDetail); emits('onRegeneration', imageDetail);
} }
} }
@@ -152,7 +149,9 @@ async function handleImageMidjourneyButtonClick(
await getImageList(); await getImageList();
} }
defineExpose({ getImageList }); /** 组件挂在的时候 */ defineExpose({ getImageList });
/** 组件挂在的时候 */
onMounted(async () => { onMounted(async () => {
// image // image
await getImageList(); await getImageList();
@@ -189,6 +188,7 @@ onUnmounted(async () => {
<Button @click="handleViewPublic">绘画作品</Button> <Button @click="handleViewPublic">绘画作品</Button>
</template> </template>
<!-- TODO @AI一行应该有 3 目前只有两个 -->
<div <div
class="flex flex-1 flex-wrap content-start overflow-y-auto p-5 pb-28 pt-5" class="flex flex-1 flex-wrap content-start overflow-y-auto p-5 pb-28 pt-5"
ref="imageListRef" ref="imageListRef"

View File

@@ -29,21 +29,17 @@ import {
import { midjourneyImagine } from '#/api/ai/image'; import { midjourneyImagine } from '#/api/ai/image';
import { ImageUpload } from '#/components/upload'; import { ImageUpload } from '#/components/upload';
//
//
const props = defineProps({ const props = defineProps({
models: { models: {
type: Array<AiModelModelApi.Model>, type: Array<AiModelModelApi.Model>,
default: () => [] as AiModelModelApi.Model[], default: () => [] as AiModelModelApi.Model[],
}, },
}); }); //
const emits = defineEmits(['onDrawStart', 'onDrawComplete']); const emits = defineEmits(['onDrawStart', 'onDrawComplete']);
//
const drawIn = ref<boolean>(false); // const drawIn = ref<boolean>(false); //
const selectHotWord = ref<string>(''); // const selectHotWord = ref<string>(''); //
//
const prompt = ref<string>(''); // const prompt = ref<string>(''); //
const referImageUrl = ref<any>(); // const referImageUrl = ref<any>(); //
const selectModel = ref<string>('midjourney'); // const selectModel = ref<string>('midjourney'); //
@@ -58,7 +54,6 @@ async function handleHotWordClick(hotWord: string) {
selectHotWord.value = ''; selectHotWord.value = '';
return; return;
} }
// //
selectHotWord.value = hotWord; // selectHotWord.value = hotWord; //
prompt.value = hotWord; // prompt.value = hotWord; //

View File

@@ -25,14 +25,12 @@ import {
import { drawImage } from '#/api/ai/image'; import { drawImage } from '#/api/ai/image';
//
const props = defineProps({ const props = defineProps({
models: { models: {
type: Array<AiModelModelApi.Model>, type: Array<AiModelModelApi.Model>,
default: () => [] as AiModelModelApi.Model[], default: () => [] as AiModelModelApi.Model[],
}, },
}); }); //
const emits = defineEmits(['onDrawStart', 'onDrawComplete']); const emits = defineEmits(['onDrawStart', 'onDrawComplete']);
function hasChinese(str: string) { function hasChinese(str: string) {
@@ -60,7 +58,6 @@ async function handleHotWordClick(hotWord: string) {
selectHotWord.value = ''; selectHotWord.value = '';
return; return;
} }
// //
selectHotWord.value = hotWord; // selectHotWord.value = hotWord; //
prompt.value = hotWord; // prompt.value = hotWord; //
@@ -82,7 +79,7 @@ async function handleGenerateImage() {
// //
if (hasChinese(prompt.value)) { if (hasChinese(prompt.value)) {
alert('暂不支持中文!'); await alert('暂不支持中文!');
return; return;
} }
await confirm(`确认生成内容?`); await confirm(`确认生成内容?`);