index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const data = {
  2. name: 'imgcache',
  3. prefix: 'imgcache_'
  4. }
  5. function ImgCache (vm) {
  6. this.vm = vm // 保存实例在其他周期使用
  7. this.i = 0 // 用于标记第几张图
  8. vm.imgCache = {
  9. get list () {
  10. return uni
  11. .getStorageInfoSync()
  12. .keys.filter((key) => key.startsWith(data.prefix))
  13. .map((key) => key.split(data.prefix)[1])
  14. },
  15. get (url) {
  16. return uni.getStorageSync(data.prefix + url)
  17. },
  18. delete (url) {
  19. const path = uni.getStorageSync(data.prefix + url)
  20. if (!path) return false
  21. plus.io.resolveLocalFileSystemURL(path, (entry) => {
  22. entry.remove()
  23. })
  24. uni.removeStorageSync(data.prefix + url)
  25. return true
  26. },
  27. async add (url) {
  28. const filename = await download(url)
  29. if (filename) {
  30. uni.setStorageSync(data.prefix + url, filename)
  31. return 'file://' + plus.io.convertLocalFileSystemURL(filename)
  32. }
  33. return null
  34. },
  35. clear () {
  36. uni
  37. .getStorageInfoSync()
  38. .keys.filter((key) => key.startsWith(data.prefix))
  39. .forEach((key) => {
  40. uni.removeStorageSync(key)
  41. })
  42. plus.io.resolveLocalFileSystemURL(`_doc/${data.name}/`, (entry) => {
  43. entry.removeRecursively(
  44. (entry) => {
  45. console.log(`${data.name}缓存删除成功`, entry)
  46. },
  47. (e) => {
  48. console.log(`${data.name}缓存删除失败`, e)
  49. }
  50. )
  51. })
  52. }
  53. }
  54. }
  55. // #ifdef APP-PLUS
  56. ImgCache.prototype.onParse = function (node, parser) {
  57. // 启用本插件 && 解析图片标签 && 拥有src属性 && 是网络图片
  58. if (
  59. this.vm.ImgCache &&
  60. node.name === 'img' &&
  61. node.attrs.src &&
  62. /^https?:\/\//.test(node.attrs.src)
  63. ) {
  64. const src = node.attrs.src
  65. node.attrs.src = ''
  66. node.attrs.i = this.vm.imgList.length + this.i++
  67. parser.expose()
  68. async function getUrl (path) {
  69. if (await resolveFile(path)) return path
  70. const filename = await download(src)
  71. filename && uni.setStorageSync(data.prefix + src, filename)
  72. return filename
  73. }
  74. uni.getStorage({
  75. key: data.prefix + src,
  76. success: async (res) => {
  77. const path = await getUrl(res.data)
  78. const url = path
  79. ? 'file://' + plus.io.convertLocalFileSystemURL(path)
  80. : src
  81. node.attrs.src = url
  82. this.vm.imgList[node.attrs.i] = path || src
  83. },
  84. fail: async () => {
  85. const path = await getUrl()
  86. const url = path
  87. ? 'file://' + plus.io.convertLocalFileSystemURL(path)
  88. : src
  89. node.attrs.src = url
  90. this.vm.imgList[node.attrs.i] = path || src
  91. }
  92. })
  93. }
  94. }
  95. const taskQueue = new Set()
  96. function download (url) {
  97. return new Promise((resolve) => {
  98. if (taskQueue.has(url)) return
  99. taskQueue.add(url)
  100. const suffix = /.+\.(jpg|jpeg|png|bmp|gif|webp)/.exec(url)
  101. const name = `${makeid(8)}_${Date.now()}${suffix ? '.' + suffix[1] : ''}`
  102. const task = plus.downloader.createDownload(
  103. url,
  104. { filename: `_doc/${data.name}/${name}` },
  105. (download, status) => {
  106. taskQueue.delete(url)
  107. resolve(status === 200 ? download.filename : null)
  108. }
  109. )
  110. task.start()
  111. })
  112. }
  113. // 判断文件存在
  114. function resolveFile (url) {
  115. return new Promise((resolve) => {
  116. plus.io.resolveLocalFileSystemURL(url, resolve, () => resolve(null))
  117. })
  118. }
  119. // 生成uuid
  120. function makeid (length) {
  121. let result = ''
  122. const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  123. for (let i = 0; i < length; i++) {
  124. result += characters.charAt(Math.floor(Math.random() * characters.length))
  125. }
  126. return result
  127. }
  128. // #endif
  129. module.exports = ImgCache