【代码优化】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 // AI API 密钥 API
export const ImageApi = { export const ImageApi = {
// 获取我的图片列表 // 获取【我的】绘图分页
getImagePageMy: async (params: ImagePageReqVO) => { getImagePageMy: async (params: ImagePageReqVO) => {
return await request.get({ url: `/ai/image/my-page`, params }) return await request.get({ url: `/ai/image/my-page`, params })
}, },
// 获取我的图片 // 获取【我的】绘图记录
getImageMy: async (id: number) => { getImageMy: async (id: number) => {
return await request.get({ url: `/ai/image/get-my?id=${id}` }) return await request.get({ url: `/ai/image/get-my?id=${id}` })
}, },
// 获取我的图片 // 获取【我的】绘图记录列表
getImageMyIds: async (params) => { getImageListMyByIds: async (ids: number[]) => {
return await request.get({ url: `/ai/image/get-my-ids`, params}) return await request.get({ url: `/ai/image/get-my-ids`, params: { ids: ids.join(',') } })
}, },
// 生成图片 // 生成图片
drawImage: async (data: ImageDrawReqVO) => { drawImage: async (data: ImageDrawReqVO) => {
return await request.post({ url: `/ai/image/draw`, data }) return await request.post({ url: `/ai/image/draw`, data })
}, },
// 删除我的图片 // 删除【我的】绘画记录
deleteImageMy: async (id: number) => { deleteImageMy: async (id: number) => {
return await request.delete({ url: `/ai/image/delete-my?id=${id}` }) 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 message = useMessage() //
const imageList = ref<ImageVO[]>([]) // image const imageList = ref<ImageVO[]>([]) // image
const watchImages = ref<{}>({}) // image list const inProgressImageMap = ref<{}>({}) // image key image value image
const imageListInterval = ref<any>() // image const imageListInterval = ref<any>() // image
const isShowImageDetail = ref<boolean>(false) // task const isShowImageDetail = ref<boolean>(false) // task
const showImageDetailId = ref<number>(0) // task const showImageDetailId = ref<number>(0) // task
@ -79,12 +79,12 @@ const getImageList = async (apply: boolean = false) => {
pageTotal.value = total pageTotal.value = total
// watch // watch
const newWatImages = {} const newWatImages = {}
imageList.value.forEach(item => { imageList.value.forEach((item) => {
if (item.status === 10) { if (item.status === 10) {
newWatImages[item.id] = item newWatImages[item.id] = item
} }
}) })
watchImages.value = newWatImages inProgressImageMap.value = newWatImages
} finally { } finally {
if (imageTaskLoadingInstance.value) { if (imageTaskLoadingInstance.value) {
imageTaskLoadingInstance.value.close() imageTaskLoadingInstance.value.close()
@ -93,34 +93,30 @@ const getImageList = async (apply: boolean = false) => {
} }
} }
/** /** 轮询生成中的 image 列表 */
* 获取 - image 列表
*/
const refreshWatchImages = async () => { const refreshWatchImages = async () => {
const imageIds = Object.keys(watchImages.value) const imageIds = Object.keys(inProgressImageMap.value).map(Number)
if (imageIds.length < 1) { if (imageIds.length == 0) {
console.log('refreshWatchImages 不刷新', imageIds)
return return
} }
const res = await ImageApi.getImageMyIds({ids: imageIds.join(',')}) as ImageVO[] const list = (await ImageApi.getImageListMyByIds(imageIds)) as ImageVO[]
const newWatchImages = {} const newWatchImages = {}
res.forEach(image => { list.forEach((image) => {
if (image.status === 10) { if (image.status === 10) {
newWatchImages[image.id] = image newWatchImages[image.id] = image
} else { } else {
const index = imageList.value.findIndex(oldImage => image.id === oldImage.id) const index = imageList.value.findIndex((oldImage) => image.id === oldImage.id)
if (index !== -1) { if (index >= 0) {
// imageList // imageList
imageList.value[index] = image imageList.value[index] = image
} }
} }
}) })
console.log('newWatchImages-done', newWatchImages) inProgressImageMap.value = newWatchImages
watchImages.value = newWatchImages
} }
/** 图片 - btn click */ /** 图片 - btn click */
const handlerImageBtnClick = async (type, imageDetail: ImageVO) => { const handlerImageBtnClick = async (type: string, imageDetail: ImageVO) => {
// image detail id // image detail id
showImageDetailId.value = imageDetail.id showImageDetailId.value = imageDetail.id
// btn // btn
@ -130,7 +126,7 @@ const handlerImageBtnClick = async (type, imageDetail: ImageVO) => {
await message.confirm(`是否删除照片?`) await message.confirm(`是否删除照片?`)
await ImageApi.deleteImageMy(imageDetail.id) await ImageApi.deleteImageMy(imageDetail.id)
await getImageList() await getImageList()
await message.success('删除成功!') message.success('删除成功!')
} else if (type === 'download') { } else if (type === 'download') {
await downloadImage(imageDetail.picUrl) await downloadImage(imageDetail.picUrl)
} }