【代码优化】AI:图片轮询实现

pull/468/MERGE
YunaiV 2024-06-28 21:18:21 +08:00
parent 9983dc5bb9
commit d800143a88
2 changed files with 19 additions and 23 deletions

View File

@ -56,23 +56,23 @@ export interface ImageMjButtonsVO {
// AI API 密钥 API
export const ImageApi = {
// 获取我的图片列表
// 获取【我的】绘图分页
getImagePageMy: async (params: ImagePageReqVO) => {
return await request.get({ url: `/ai/image/my-page`, params })
},
// 获取我的图片
// 获取【我的】绘图记录
getImageMy: async (id: number) => {
return await request.get({ url: `/ai/image/get-my?id=${id}` })
},
// 获取我的图片
getImageMyIds: async (params) => {
return await request.get({ url: `/ai/image/get-my-ids`, params})
// 获取【我的】绘图记录列表
getImageListMyByIds: async (ids: number[]) => {
return await request.get({ url: `/ai/image/get-my-ids`, params: { ids: ids.join(',') } })
},
// 生成图片
drawImage: async (data: ImageDrawReqVO) => {
return await request.post({ url: `/ai/image/draw`, data })
},
// 删除我的图片
// 删除【我的】绘画记录
deleteImageMy: async (id: number) => {
return await request.delete({ url: `/ai/image/delete-my?id=${id}` })
},

View File

@ -36,7 +36,7 @@ import { ElLoading, LoadingOptionsResolved } from 'element-plus'
const message = useMessage() //
const imageList = ref<ImageVO[]>([]) // image
const watchImages = ref<{}>({}) // image list
const inProgressImageMap = ref<{}>({}) // image key image value image
const imageListInterval = ref<any>() // image
const isShowImageDetail = ref<boolean>(false) // task
const showImageDetailId = ref<number>(0) // task
@ -79,12 +79,12 @@ const getImageList = async (apply: boolean = false) => {
pageTotal.value = total
// watch
const newWatImages = {}
imageList.value.forEach(item => {
imageList.value.forEach((item) => {
if (item.status === 10) {
newWatImages[item.id] = item
}
})
watchImages.value = newWatImages
inProgressImageMap.value = newWatImages
} finally {
if (imageTaskLoadingInstance.value) {
imageTaskLoadingInstance.value.close()
@ -93,34 +93,30 @@ const getImageList = async (apply: boolean = false) => {
}
}
/**
* 获取 - image 列表
*/
/** 轮询生成中的 image 列表 */
const refreshWatchImages = async () => {
const imageIds = Object.keys(watchImages.value)
if (imageIds.length < 1) {
console.log('refreshWatchImages 不刷新', imageIds)
const imageIds = Object.keys(inProgressImageMap.value).map(Number)
if (imageIds.length == 0) {
return
}
const res = await ImageApi.getImageMyIds({ids: imageIds.join(',')}) as ImageVO[]
const list = (await ImageApi.getImageListMyByIds(imageIds)) as ImageVO[]
const newWatchImages = {}
res.forEach(image => {
list.forEach((image) => {
if (image.status === 10) {
newWatchImages[image.id] = image
} else {
const index = imageList.value.findIndex(oldImage => image.id === oldImage.id)
if (index !== -1) {
const index = imageList.value.findIndex((oldImage) => image.id === oldImage.id)
if (index >= 0) {
// imageList
imageList.value[index] = image
}
}
})
console.log('newWatchImages-done', newWatchImages)
watchImages.value = newWatchImages
inProgressImageMap.value = newWatchImages
}
/** 图片 - btn click */
const handlerImageBtnClick = async (type, imageDetail: ImageVO) => {
const handlerImageBtnClick = async (type: string, imageDetail: ImageVO) => {
// image detail id
showImageDetailId.value = imageDetail.id
// btn
@ -130,7 +126,7 @@ const handlerImageBtnClick = async (type, imageDetail: ImageVO) => {
await message.confirm(`是否删除照片?`)
await ImageApi.deleteImageMy(imageDetail.id)
await getImageList()
await message.success('删除成功!')
message.success('删除成功!')
} else if (type === 'download') {
await downloadImage(imageDetail.picUrl)
}