index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. function getLocalFilePath(path) {
  2. if (path.indexOf('_www') === 0 || path.indexOf('_doc') === 0 || path.indexOf('_documents') === 0 || path.indexOf('_downloads') === 0) {
  3. return path
  4. }
  5. if (path.indexOf('file://') === 0) {
  6. return path
  7. }
  8. if (path.indexOf('/storage/emulated/0/') === 0) {
  9. return path
  10. }
  11. if (path.indexOf('/') === 0) {
  12. var localFilePath = plus.io.convertAbsoluteFileSystem(path)
  13. if (localFilePath !== path) {
  14. return localFilePath
  15. } else {
  16. path = path.substr(1)
  17. }
  18. }
  19. return '_www/' + path
  20. }
  21. export function pathToBase64(path) {
  22. return new Promise(function(resolve, reject) {
  23. if (typeof window === 'object' && 'document' in window) {
  24. if (typeof FileReader === 'function') {
  25. var xhr = new XMLHttpRequest()
  26. xhr.open('GET', path, true)
  27. xhr.responseType = 'blob'
  28. xhr.onload = function() {
  29. if (this.status === 200) {
  30. let fileReader = new FileReader()
  31. fileReader.onload = function(e) {
  32. resolve(e.target.result)
  33. }
  34. fileReader.onerror = reject
  35. fileReader.readAsDataURL(this.response)
  36. }
  37. }
  38. xhr.onerror = reject
  39. xhr.send()
  40. return
  41. }
  42. var canvas = document.createElement('canvas')
  43. var c2x = canvas.getContext('2d')
  44. var img = new Image
  45. img.onload = function() {
  46. canvas.width = img.width
  47. canvas.height = img.height
  48. c2x.drawImage(img, 0, 0)
  49. resolve(canvas.toDataURL())
  50. canvas.height = canvas.width = 0
  51. }
  52. img.onerror = reject
  53. img.src = path
  54. return
  55. }
  56. if (typeof plus === 'object') {
  57. plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), function(entry) {
  58. entry.file(function(file) {
  59. var fileReader = new plus.io.FileReader()
  60. fileReader.onload = function(data) {
  61. resolve(data.target.result)
  62. }
  63. fileReader.onerror = function(error) {
  64. reject(error)
  65. }
  66. fileReader.readAsDataURL(file)
  67. }, function(error) {
  68. reject(error)
  69. })
  70. }, function(error) {
  71. reject(error)
  72. })
  73. return
  74. }
  75. if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
  76. wx.getFileSystemManager().readFile({
  77. filePath: path,
  78. encoding: 'base64',
  79. success: function(res) {
  80. resolve('data:image/png;base64,' + res.data)
  81. },
  82. fail: function(error) {
  83. reject(error)
  84. }
  85. })
  86. return
  87. }
  88. reject(new Error('not support'))
  89. })
  90. }
  91. export function base64ToPath(base64) {
  92. return new Promise(function(resolve, reject) {
  93. if (typeof window === 'object' && 'document' in window) {
  94. base64 = base64.split(',')
  95. var type = base64[0].match(/:(.*?);/)[1]
  96. var str = atob(base64[1])
  97. var n = str.length
  98. var array = new Uint8Array(n)
  99. while (n--) {
  100. array[n] = str.charCodeAt(n)
  101. }
  102. return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], { type: type })))
  103. }
  104. var extName = base64.match(/data\:\S+\/(\S+);/)
  105. if (extName) {
  106. extName = extName[1]
  107. } else {
  108. reject(new Error('base64 error'))
  109. }
  110. var fileName = Date.now() + '.' + extName
  111. if (typeof plus === 'object') {
  112. var bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
  113. bitmap.loadBase64Data(base64, function() {
  114. var filePath = '_doc/uniapp_temp/' + fileName
  115. bitmap.save(filePath, {}, function() {
  116. bitmap.clear()
  117. resolve(filePath)
  118. }, function(error) {
  119. bitmap.clear()
  120. reject(error)
  121. })
  122. }, function(error) {
  123. bitmap.clear()
  124. reject(error)
  125. })
  126. return
  127. }
  128. if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
  129. var filePath = wx.env.USER_DATA_PATH + '/' + fileName
  130. wx.getFileSystemManager().writeFile({
  131. filePath: filePath,
  132. data: base64.replace(/^data:\S+\/\S+;base64,/, ''),
  133. encoding: 'base64',
  134. success: function() {
  135. resolve(filePath)
  136. },
  137. fail: function(error) {
  138. reject(error)
  139. }
  140. })
  141. return
  142. }
  143. reject(new Error('not support'))
  144. })
  145. }