合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.0 KiB

3 months ago
  1. // @ts-nocheck
  2. // #ifdef APP-ANDROID || APP-IOS
  3. import { fileToDataURL } from '@/uni_modules/lime-file-utils'
  4. // #endif
  5. /**
  6. * base64
  7. * @param {string} path
  8. * @return SVG Data URL
  9. */
  10. export function pathToDataUrl(path : string) : Promise<string> {
  11. return new Promise((resolve, reject) => {
  12. // #ifdef MP
  13. uni.getFileSystemManager().readFile({
  14. filePath: path,
  15. encoding: 'base64',
  16. success: (res) => {
  17. resolve(`data:image/svg+xml;base64,${res.data}`)
  18. },
  19. fail: (error) => {
  20. console.error({ error, path })
  21. reject(error)
  22. }
  23. })
  24. // #endif
  25. // #ifdef APP-ANDROID || APP-IOS
  26. const url = fileToDataURL(path)
  27. if(url == null) {
  28. reject('路径错误')
  29. }
  30. resolve(url!.replace(/\s+/g,''))
  31. // #endif
  32. // #ifdef APP-NVUE
  33. let localFilePath = plus.io.convertAbsoluteFileSystem(path)
  34. if (localFilePath == path) {
  35. localFilePath = '_www/' + path.slice(1)
  36. }
  37. plus.io.resolveLocalFileSystemURL(localFilePath, (entry) => {
  38. entry.file((file : any) => {
  39. const fileReader = new plus.io.FileReader()
  40. fileReader.onload = (data) => {
  41. resolve(data.target.result)
  42. }
  43. fileReader.onerror = (error) => {
  44. console.error({ error, path })
  45. reject(error)
  46. }
  47. fileReader.readAsDataURL(file)
  48. }, reject)
  49. }, reject)
  50. // #endif
  51. // #ifndef APP-ANDROID || APP-IOS || MP || APP-NVUE
  52. reject('不支持')
  53. // #endif
  54. })
  55. }
  56. /**
  57. * SVG Data URL
  58. * @param {string} svg - SVG
  59. * @returns {string} SVG Data URL
  60. */
  61. export function svgToDataUrl(svgString : string) : string {
  62. // const encodedSvg = svgString.replace('<svg', (~svgString.indexOf('xmlns') != 0? '<svg' : '<svg xmlns="http://www.w3.org/2000/svg"'))
  63. // .replace(/data-(.*?=(['"]).*?\2)/g, '$1')
  64. // .replace(/xlink-href=/g, 'xlink:href=')
  65. // .replace(/view-box=/g, 'viewBox=')
  66. // .replace(/<(title|desc)>[\s\S]*?<\/\1>/g, '')
  67. // // .replace(/\d+\.\d+/g, (match) : number => parseFloat(parseFloat(match).toFixed(2)))
  68. // .replace(/<!--[\s\S]*?-->/g, '')
  69. // .replace(/\s+/g, ' ')
  70. // // .replace(/[{}\|\\\^~\[\]`"<>#%]/g, (match: string):string => {
  71. // // return `%${match[0].charCodeAt(0).toString(16).toUpperCase()}`
  72. // // }).replace(/'/g, "\\'")
  73. // .replace(/\s+/g, '%20') // 将空格替换为%20
  74. // .replace(/"/g, '%22') // 转义"
  75. // .replace(/</g, '%3C') // 转义<
  76. // .replace(/>/g, '%3E') // 转义>
  77. // .replace(/\//g, '%2F') // 转义/
  78. // .replace(/\?/g, '%3F') // 转义?
  79. // .replace(/#/g, '%23') // 转义#
  80. // .replace(/:/g, '%3A') // 转义:
  81. // .replace(/;/g, '%3B') // 转义;
  82. // .replace(/=/g, '%3D') // 转义=
  83. // .replace(/\$/g, '%24') // 转义$
  84. // .replace(/@/g, '%40') // 转义@
  85. // .replace(/#/g, '%23')
  86. // // .replace(/{/g, '%7B')
  87. // // .replace(/}/g, '%7D')
  88. // .trim(); // 移除首尾空格
  89. // #ifdef APP
  90. // #endif
  91. // #ifndef APP
  92. // const encodedSvg = encodeURIComponent(svgString);
  93. // #endif
  94. const encodedSvg = encodeURIComponent(svgString)!.replace(/\+/g, '%20');
  95. return `data:image/svg+xml,${encodedSvg}`
  96. }