Skip to content

Commit fdeedcf

Browse files
committed
【增加】Image card 增加图片下载
1 parent 2211618 commit fdeedcf

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/views/ai/image/ImageTask.vue

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,32 @@ const handlerImageBtnClick = async (type, imageDetail: ImageDetailVO) => {
6666
await ImageApi.deleteImage(imageDetail.id)
6767
await getImageList()
6868
await message.success("删除成功!")
69+
} else if (type === 'download') {
70+
downloadImage(imageDetail.picUrl)
6971
}
7072
}
73+
74+
/**
75+
* 下载 - image
76+
*/
77+
const downloadImage = async (imageUrl) => {
78+
const image = new Image()
79+
image.setAttribute('crossOrigin', 'anonymous')
80+
image.src = imageUrl
81+
image.onload = () => {
82+
const canvas = document.createElement('canvas')
83+
canvas.width = image.width
84+
canvas.height = image.height
85+
const ctx = canvas.getContext('2d')
86+
ctx.drawImage(image, 0, 0, image.width, image.height)
87+
const url = canvas.toDataURL('image/png')
88+
const a = document.createElement('a')
89+
a.href = url
90+
a.download = 'image.png'
91+
a.click()
92+
}
93+
}
94+
7195
//
7296
defineExpose({getImageList})
7397
//

src/views/ai/image/ImageTaskCard.vue

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
<template>
32
<el-card body-class="" class="image-card">
43
<div class="image-operation">
@@ -8,13 +7,14 @@
87
<el-button type="" text bg v-else-if="imageDetail.status === 'complete'">已完成</el-button>
98
</div>
109
<div>
11-
<el-button class="btn" text :icon="Download" @click="handlerBtnClick('download', imageDetail)" />
12-
<el-button class="btn" text :icon="Delete" @click="handlerBtnClick('delete', imageDetail)" />
13-
<el-button class="btn" text :icon="More" @click="handlerBtnClick('more', imageDetail)" />
10+
<el-button class="btn" text :icon="Download"
11+
@click="handlerBtnClick('download', imageDetail)"/>
12+
<el-button class="btn" text :icon="Delete" @click="handlerBtnClick('delete', imageDetail)"/>
13+
<el-button class="btn" text :icon="More" @click="handlerBtnClick('more', imageDetail)"/>
1414
</div>
1515
</div>
1616
<div class="image-wrapper" ref="cardImageRef">
17-
<img class="image" :src="imageDetail?.picUrl" />
17+
<img class="image" :src="imageDetail?.picUrl"/>
1818
</div>
1919
</el-card>
2020
</template>
@@ -37,22 +37,15 @@ const props = defineProps({
3737
/**
3838
* 按钮 - 点击事件
3939
*/
40-
const handlerBtnClick = async (type, imageDetail: ImageDetailVO ) => {
40+
const handlerBtnClick = async (type, imageDetail: ImageDetailVO) => {
4141
emits('onBtnClick', type, imageDetail)
4242
}
4343
44-
// 监听 imageDetail
45-
// const { imageDetail } = toRefs(props)
46-
// watch(imageDetail, async (newVal, oldValue) => {
47-
// console.log('首次 watch')
48-
//
49-
// })
50-
5144
// emits
5245
const emits = defineEmits(['onBtnClick'])
5346
5447
//
55-
onMounted( async () => {
48+
onMounted(async () => {
5649
if (props.imageDetail.status === 'in_progress') {
5750
cardImageLoadingInstance.value = ElLoading.service({
5851
target: cardImageRef.value,

0 commit comments

Comments
 (0)