【代码优化】AI:绘图 index.vue 代码梳理 40%(ImageCard)

pull/476/MERGE
YunaiV 2024-07-09 08:39:37 +08:00
parent 5162191340
commit 749e44081e
3 changed files with 50 additions and 49 deletions

View File

@ -2,53 +2,53 @@
<el-card body-class="" class="image-card"> <el-card body-class="" class="image-card">
<div class="image-operation"> <div class="image-operation">
<div> <div>
<el-button <el-button type="primary" text bg v-if="detail?.status === AiImageStatusEnum.IN_PROGRESS">
type="primary"
text
bg
v-if="imageDetail?.status === AiImageStatusEnum.IN_PROGRESS"
>
生成中 生成中
</el-button> </el-button>
<el-button text bg v-else-if="imageDetail?.status === AiImageStatusEnum.SUCCESS"> <el-button text bg v-else-if="detail?.status === AiImageStatusEnum.SUCCESS">
已完成 已完成
</el-button> </el-button>
<el-button type="danger" text bg v-else-if="imageDetail?.status === AiImageStatusEnum.FAIL"> <el-button type="danger" text bg v-else-if="detail?.status === AiImageStatusEnum.FAIL">
异常 异常
</el-button> </el-button>
</div> </div>
<!-- 操作区 -->
<div> <div>
<el-button <el-button
class="btn" class="btn"
text text
:icon="Download" :icon="Download"
@click="handleBtnClick('download', imageDetail)" @click="handleButtonClick('download', detail)"
/> />
<el-button <el-button
class="btn" class="btn"
text text
:icon="RefreshRight" :icon="RefreshRight"
@click="handleBtnClick('regeneration', imageDetail)" @click="handleButtonClick('regeneration', detail)"
/> />
<el-button class="btn" text :icon="Delete" @click="handleBtnClick('delete', imageDetail)" /> <el-button class="btn" text :icon="Delete" @click="handleButtonClick('delete', detail)" />
<el-button class="btn" text :icon="More" @click="handleBtnClick('more', imageDetail)" /> <el-button class="btn" text :icon="More" @click="handleButtonClick('more', detail)" />
</div> </div>
</div> </div>
<div class="image-wrapper" ref="cardImageRef"> <div class="image-wrapper" ref="cardImageRef">
<!-- TODO @fan要不加个点击大图预览 --> <el-image
<img class="image" :src="imageDetail?.picUrl" /> class="image"
<div v-if="imageDetail?.status === AiImageStatusEnum.FAIL"> :src="detail?.picUrl"
{{ imageDetail?.errorMessage }} :preview-src-list="[detail.picUrl]"
preview-teleported
/>
<div v-if="detail?.status === AiImageStatusEnum.FAIL">
{{ detail?.errorMessage }}
</div> </div>
</div> </div>
<!-- TODO @fanstyle 使用 unocss 替代下 --> <!-- Midjourney 专属操作 -->
<div class="image-mj-btns"> <div class="image-mj-btns">
<el-button <el-button
size="small" size="small"
v-for="button in imageDetail?.buttons" v-for="button in detail?.buttons"
:key="button" :key="button"
style="min-width: 40px; margin-left: 0; margin-right: 10px; margin-top: 5px" class="min-w-40px ml-0 mr-10px mt-5px"
@click="handleMjBtnClick(button)" @click="handleMidjourneyBtnClick(button)"
> >
{{ button.label }}{{ button.emoji }} {{ button.label }}{{ button.emoji }}
</el-button> </el-button>
@ -62,28 +62,47 @@ import { PropType } from 'vue'
import { ElLoading, LoadingOptionsResolved } from 'element-plus' import { ElLoading, LoadingOptionsResolved } from 'element-plus'
import { AiImageStatusEnum } from '@/views/ai/utils/constants' import { AiImageStatusEnum } from '@/views/ai/utils/constants'
const cardImageRef = ref<any>() // image ref const message = useMessage() //
const cardImageLoadingInstance = ref<any>() // image ref
const message = useMessage()
const props = defineProps({ const props = defineProps({
imageDetail: { detail: {
type: Object as PropType<ImageVO>, type: Object as PropType<ImageVO>,
require: true require: true
} }
}) })
/** 按钮 - 点击事件 */ const cardImageRef = ref<any>() // image ref
const handleBtnClick = async (type, imageDetail: ImageVO) => { const cardImageLoadingInstance = ref<any>() // image ref
emits('onBtnClick', type, imageDetail)
/** 处理点击事件 */
const handleButtonClick = async (type, detail: ImageVO) => {
emits('onBtnClick', type, detail)
} }
/** 处理 Midjourney 按钮点击事件 */
const handleMidjourneyBtnClick = async (button: ImageMidjourneyButtonsVO) => {
//
await message.confirm(`确认操作 "${button.label} ${button.emoji}" ?`)
emits('onMjBtnClick', button, props.detail)
}
const emits = defineEmits(['onBtnClick', 'onMjBtnClick']) // emits
/** 监听详情 */
const { detail } = toRefs(props)
watch(detail, async (newVal, oldVal) => {
await handleLoading(newVal.status as string)
})
/** 处理加载状态 */
const handleLoading = async (status: number) => { const handleLoading = async (status: number) => {
// TODO @ Loading // loading
if (status === AiImageStatusEnum.IN_PROGRESS) { if (status === AiImageStatusEnum.IN_PROGRESS) {
cardImageLoadingInstance.value = ElLoading.service({ cardImageLoadingInstance.value = ElLoading.service({
target: cardImageRef.value, target: cardImageRef.value,
text: '生成中...' text: '生成中...'
} as LoadingOptionsResolved) } as LoadingOptionsResolved)
// loading
} else { } else {
if (cardImageLoadingInstance.value) { if (cardImageLoadingInstance.value) {
cardImageLoadingInstance.value.close() cardImageLoadingInstance.value.close()
@ -92,25 +111,9 @@ const handleLoading = async (status: number) => {
} }
} }
/** mj 按钮 click */ /** 初始化 */
const handleMjBtnClick = async (button: ImageMidjourneyButtonsVO) => {
//
await message.confirm(`确认操作 "${button.label} ${button.emoji}" ?`)
emits('onMjBtnClick', button, props.imageDetail)
}
// watch
const { imageDetail } = toRefs(props)
watch(imageDetail, async (newVal, oldVal) => {
await handleLoading(newVal.status as string)
})
// emits
const emits = defineEmits(['onBtnClick', 'onMjBtnClick'])
//
onMounted(async () => { onMounted(async () => {
await handleLoading(props.imageDetail.status as string) await handleLoading(props.detail.status as string)
}) })
</script> </script>

View File

@ -107,8 +107,6 @@ watch(id, async (newVal, oldVal) => {
}) })
// //
const emits = defineEmits(['handleDrawerClose']) const emits = defineEmits(['handleDrawerClose'])
//
onMounted(async () => {})
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
.item { .item {

View File

@ -6,7 +6,7 @@
<ImageCard <ImageCard
v-for="image in imageList" v-for="image in imageList"
:key="image.id" :key="image.id"
:image-detail="image" :detail="image"
@on-btn-click="handleImageButtonClick" @on-btn-click="handleImageButtonClick"
@on-mj-btn-click="handleImageMidjourneyButtonClick" @on-mj-btn-click="handleImageMidjourneyButtonClick"
/> />