101 lines
2.4 KiB
JavaScript
101 lines
2.4 KiB
JavaScript
function parseEditorImage (blocks = []) {
|
|
const images = []
|
|
|
|
if (!Array.isArray(blocks)) {
|
|
blocks = [blocks]
|
|
}
|
|
|
|
for (const block of blocks) {
|
|
const {insert = {}, attributes = {}} = block
|
|
const {'data-custom': custom = ""} = attributes
|
|
|
|
let parseCustom = custom.split('&').reduce((obj, item) => {
|
|
const [key, value] = item.split('=')
|
|
|
|
if (key && value) {
|
|
obj[key] = value
|
|
}
|
|
return obj
|
|
}, {})
|
|
|
|
images.push({
|
|
src: insert.image,
|
|
source: parseCustom.source ? parseCustom.source: insert.image
|
|
})
|
|
}
|
|
|
|
return images
|
|
}
|
|
|
|
/**
|
|
* 解析媒体库/编辑器中的图片
|
|
* @param images 图片地址
|
|
* @param type {string} 解析类型 media: 媒体库, editor: 编辑器
|
|
* @returns {Promise<{src: *, source: *}[]|{src, source: *}[]>}
|
|
*/
|
|
|
|
|
|
export async function parseImageUrl (images = [], type = "media") {
|
|
|
|
console.log(images)
|
|
if (type === "editor") {
|
|
images = parseEditorImage(images).map(item => item.source)
|
|
} else {
|
|
if (!Array.isArray(images)) {
|
|
images = [images]
|
|
}
|
|
images = await obj.getCloudImage(images)
|
|
}
|
|
|
|
if (!images) return null
|
|
|
|
const tcbFiles = images.filter(item => item.startsWith("cloud://"))
|
|
|
|
|
|
if (tcbFiles.length) {
|
|
const res = await uniCloud.getTempFileURL({
|
|
fileList: tcbFiles
|
|
})
|
|
|
|
return images.map(image => {
|
|
const file = res.fileList.find(item => item.fileID === image)
|
|
|
|
return {
|
|
src: file ? file.tempFileURL : image,
|
|
source: image
|
|
}
|
|
})
|
|
} else {
|
|
return images.map(image => ({
|
|
src: image,
|
|
source: image
|
|
}))
|
|
}
|
|
}
|
|
|
|
let obj = {
|
|
async getCloudImage(srcLst) {
|
|
console.log(srcLst)
|
|
for (let i = 0; i < srcLst.length ; i ++ ) {
|
|
if (srcLst[i]&&srcLst[i].substring(0, 8) == "cloud://") {
|
|
const res = await uniCloud.getTempFileURL({fileList: [srcLst[i]]});
|
|
console.log(res)
|
|
console.log(res.fileList.tempFileURL)
|
|
srcLst[i] = res.fileList[0].tempFileURL
|
|
|
|
} else if (srcLst[i]&&srcLst[i].substring(0, 8) == "qiniu://") {
|
|
console.log(srcLst[i])
|
|
const uniCloudStorageExtCo = uniCloud.importObject("ext-storage-co");
|
|
const res = await uniCloudStorageExtCo.getTempFileURL({src: srcLst[i]});
|
|
console.log(res.fileList[0].tempFileURL)
|
|
srcLst[i] = res.fileList[0].tempFileURL
|
|
|
|
} else {
|
|
srcLst[i] = srcLst[i]
|
|
}
|
|
}
|
|
console.log(srcLst)
|
|
return srcLst;
|
|
},
|
|
}
|