Files
QXTstore/QXTfront/uni_modules/lime-svg/components/l-svg/utils.ts

70 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-11-05 17:34:23 +08:00
// @ts-nocheck
// #ifdef APP-ANDROID || APP-IOS
// import { fileToDataURL } from '@/uni_modules/lime-file-utils'
// #endif
/**
* base64
* @param {string} path
* @return SVG Data URL
*/
export function pathToDataUrl(path : string) : Promise<string> {
return new Promise((resolve, reject) => {
// #ifdef MP
uni.getFileSystemManager().readFile({
filePath: path,
encoding: 'base64',
success: (res) => {
resolve(`data:image/svg+xml;base64,${res.data}`)
},
fail: (error) => {
console.error({ error, path })
reject(error)
}
})
// #endif
// #ifdef APP-ANDROID || APP-IOS
// const url = fileToDataURL(path)
const url = path
if(url == null) {
reject('路径错误')
}
resolve(url!.replace(/\s+/g,''))
// #endif
// #ifdef APP-NVUE
let localFilePath = plus.io.convertAbsoluteFileSystem(path)
if (localFilePath == path) {
localFilePath = '_www/' + path.slice(1)
}
plus.io.resolveLocalFileSystemURL(localFilePath, (entry) => {
entry.file((file : any) => {
const fileReader = new plus.io.FileReader()
fileReader.onload = (data) => {
resolve(data.target.result)
}
fileReader.onerror = (error) => {
console.error({ error, path })
reject(error)
}
fileReader.readAsDataURL(file)
}, reject)
}, reject)
// #endif
// #ifndef APP-ANDROID || APP-IOS || MP || APP-NVUE
reject('不支持')
// #endif
})
}
/**
* SVG Data URL
* @param {string} svg - SVG
* @returns {string} SVG Data URL
*/
export function svgToDataUrl(svgString : string) : string {
const encodedSvg = encodeURIComponent(svgString)!.replace(/\+/g, '%20');
return `data:image/svg+xml,${encodedSvg}`
}