Skip to content

Commit 749e440

Browse files
committed
【代码优化】AI:绘图 index.vue 代码梳理 40%(ImageCard)
1 parent 5162191 commit 749e440

File tree

3 files changed

+50
-49
lines changed

3 files changed

+50
-49
lines changed

src/views/ai/image/index/components/ImageCard.vue

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,53 @@
22
<el-card body-class="" class="image-card">
33
<div class="image-operation">
44
<div>
5-
<el-button
6-
type="primary"
7-
text
8-
bg
9-
v-if="imageDetail?.status === AiImageStatusEnum.IN_PROGRESS"
10-
>
5+
<el-button type="primary" text bg v-if="detail?.status === AiImageStatusEnum.IN_PROGRESS">
116
生成中
127
</el-button>
13-
<el-button text bg v-else-if="imageDetail?.status === AiImageStatusEnum.SUCCESS">
8+
<el-button text bg v-else-if="detail?.status === AiImageStatusEnum.SUCCESS">
149
已完成
1510
</el-button>
16-
<el-button type="danger" text bg v-else-if="imageDetail?.status === AiImageStatusEnum.FAIL">
11+
<el-button type="danger" text bg v-else-if="detail?.status === AiImageStatusEnum.FAIL">
1712
异常
1813
</el-button>
1914
</div>
15+
<!-- 操作区 -->
2016
<div>
2117
<el-button
2218
class="btn"
2319
text
2420
:icon="Download"
25-
@click="handleBtnClick('download', imageDetail)"
21+
@click="handleButtonClick('download', detail)"
2622
/>
2723
<el-button
2824
class="btn"
2925
text
3026
:icon="RefreshRight"
31-
@click="handleBtnClick('regeneration', imageDetail)"
27+
@click="handleButtonClick('regeneration', detail)"
3228
/>
33-
<el-button class="btn" text :icon="Delete" @click="handleBtnClick('delete', imageDetail)" />
34-
<el-button class="btn" text :icon="More" @click="handleBtnClick('more', imageDetail)" />
29+
<el-button class="btn" text :icon="Delete" @click="handleButtonClick('delete', detail)" />
30+
<el-button class="btn" text :icon="More" @click="handleButtonClick('more', detail)" />
3531
</div>
3632
</div>
3733
<div class="image-wrapper" ref="cardImageRef">
38-
<!-- TODO @fan:要不加个点击,大图预览? -->
39-
<img class="image" :src="imageDetail?.picUrl" />
40-
<div v-if="imageDetail?.status === AiImageStatusEnum.FAIL">
41-
{{ imageDetail?.errorMessage }}
34+
<el-image
35+
class="image"
36+
:src="detail?.picUrl"
37+
:preview-src-list="[detail.picUrl]"
38+
preview-teleported
39+
/>
40+
<div v-if="detail?.status === AiImageStatusEnum.FAIL">
41+
{{ detail?.errorMessage }}
4242
</div>
4343
</div>
44-
<!-- TODO @fan:style 使用 unocss 替代下 -->
44+
<!-- Midjourney 专属操作 -->
4545
<div class="image-mj-btns">
4646
<el-button
4747
size="small"
48-
v-for="button in imageDetail?.buttons"
48+
v-for="button in detail?.buttons"
4949
:key="button"
50-
style="min-width: 40px; margin-left: 0; margin-right: 10px; margin-top: 5px"
51-
@click="handleMjBtnClick(button)"
50+
class="min-w-40px ml-0 mr-10px mt-5px"
51+
@click="handleMidjourneyBtnClick(button)"
5252
>
5353
{{ button.label }}{{ button.emoji }}
5454
</el-button>
@@ -62,28 +62,47 @@ import { PropType } from 'vue'
6262
import { ElLoading, LoadingOptionsResolved } from 'element-plus'
6363
import { AiImageStatusEnum } from '@/views/ai/utils/constants'
6464
65-
const cardImageRef = ref<any>() // 卡片 image ref
66-
const cardImageLoadingInstance = ref<any>() // 卡片 image ref
67-
const message = useMessage()
65+
const message = useMessage() // 消息
66+
6867
const props = defineProps({
69-
imageDetail: {
68+
detail: {
7069
type: Object as PropType<ImageVO>,
7170
require: true
7271
}
7372
})
7473
75-
/** 按钮 - 点击事件 */
76-
const handleBtnClick = async (type, imageDetail: ImageVO) => {
77-
emits('onBtnClick', type, imageDetail)
74+
const cardImageRef = ref<any>() // 卡片 image ref
75+
const cardImageLoadingInstance = ref<any>() // 卡片 image ref
76+
77+
/** 处理点击事件 */
78+
const handleButtonClick = async (type, detail: ImageVO) => {
79+
emits('onBtnClick', type, detail)
80+
}
81+
82+
/** 处理 Midjourney 按钮点击事件 */
83+
const handleMidjourneyBtnClick = async (button: ImageMidjourneyButtonsVO) => {
84+
// 确认窗体
85+
await message.confirm(`确认操作 "${button.label} ${button.emoji}" ?`)
86+
emits('onMjBtnClick', button, props.detail)
7887
}
7988
89+
const emits = defineEmits(['onBtnClick', 'onMjBtnClick']) // emits
90+
91+
/** 监听详情 */
92+
const { detail } = toRefs(props)
93+
watch(detail, async (newVal, oldVal) => {
94+
await handleLoading(newVal.status as string)
95+
})
96+
97+
/** 处理加载状态 */
8098
const handleLoading = async (status: number) => {
81-
// TODO @芋艿:这个搞成 Loading 组件,然后通过数据驱动,这样搞可以哇?
99+
// 情况一:如果是生成中,则设置加载中的 loading
82100
if (status === AiImageStatusEnum.IN_PROGRESS) {
83101
cardImageLoadingInstance.value = ElLoading.service({
84102
target: cardImageRef.value,
85103
text: '生成中...'
86104
} as LoadingOptionsResolved)
105+
// 情况二:如果已经生成结束,则移除 loading
87106
} else {
88107
if (cardImageLoadingInstance.value) {
89108
cardImageLoadingInstance.value.close()
@@ -92,25 +111,9 @@ const handleLoading = async (status: number) => {
92111
}
93112
}
94113
95-
/** mj 按钮 click */
96-
const handleMjBtnClick = async (button: ImageMidjourneyButtonsVO) => {
97-
// 确认窗体
98-
await message.confirm(`确认操作 "${button.label} ${button.emoji}" ?`)
99-
emits('onMjBtnClick', button, props.imageDetail)
100-
}
101-
102-
// watch
103-
const { imageDetail } = toRefs(props)
104-
watch(imageDetail, async (newVal, oldVal) => {
105-
await handleLoading(newVal.status as string)
106-
})
107-
108-
// emits
109-
const emits = defineEmits(['onBtnClick', 'onMjBtnClick'])
110-
111-
//
114+
/** 初始化 */
112115
onMounted(async () => {
113-
await handleLoading(props.imageDetail.status as string)
116+
await handleLoading(props.detail.status as string)
114117
})
115118
</script>
116119

src/views/ai/image/index/components/ImageDetail.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ watch(id, async (newVal, oldVal) => {
107107
})
108108
//
109109
const emits = defineEmits(['handleDrawerClose'])
110-
//
111-
onMounted(async () => {})
112110
</script>
113111
<style scoped lang="scss">
114112
.item {

src/views/ai/image/index/components/ImageList.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<ImageCard
77
v-for="image in imageList"
88
:key="image.id"
9-
:image-detail="image"
9+
:detail="image"
1010
@on-btn-click="handleImageButtonClick"
1111
@on-mj-btn-click="handleImageMidjourneyButtonClick"
1212
/>

0 commit comments

Comments
 (0)