index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. var index = 0
  22. function getNewFileId() {
  23. return Date.now() + String(index++)
  24. }
  25. function biggerThan(v1, v2) {
  26. var v1Array = v1.split('.')
  27. var v2Array = v2.split('.')
  28. var update = false
  29. for (var index = 0; index < v2Array.length; index++) {
  30. var diff = v1Array[index] - v2Array[index]
  31. if (diff !== 0) {
  32. update = diff > 0
  33. break
  34. }
  35. }
  36. return update
  37. }
  38. export function pathToBase64(path) {
  39. return new Promise(function(resolve, reject) {
  40. if (typeof window === 'object' && 'document' in window) {
  41. if (typeof FileReader === 'function') {
  42. var xhr = new XMLHttpRequest()
  43. xhr.open('GET', path, true)
  44. xhr.responseType = 'blob'
  45. xhr.onload = function() {
  46. if (this.status === 200) {
  47. let fileReader = new FileReader()
  48. fileReader.onload = function(e) {
  49. resolve(e.target.result)
  50. }
  51. fileReader.onerror = reject
  52. fileReader.readAsDataURL(this.response)
  53. }
  54. }
  55. xhr.onerror = reject
  56. xhr.send()
  57. return
  58. }
  59. var canvas = document.createElement('canvas')
  60. var c2x = canvas.getContext('2d')
  61. var img = new Image
  62. img.onload = function() {
  63. canvas.width = img.width
  64. canvas.height = img.height
  65. c2x.drawImage(img, 0, 0)
  66. resolve(canvas.toDataURL())
  67. canvas.height = canvas.width = 0
  68. }
  69. img.onerror = reject
  70. img.src = path
  71. return
  72. }
  73. if (typeof plus === 'object') {
  74. plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), function(entry) {
  75. entry.file(function(file) {
  76. var fileReader = new plus.io.FileReader()
  77. fileReader.onload = function(data) {
  78. resolve(data.target.result)
  79. }
  80. fileReader.onerror = function(error) {
  81. reject(error)
  82. }
  83. fileReader.readAsDataURL(file)
  84. }, function(error) {
  85. reject(error)
  86. })
  87. }, function(error) {
  88. reject(error)
  89. })
  90. return
  91. }
  92. if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
  93. wx.getFileSystemManager().readFile({
  94. filePath: path,
  95. encoding: 'base64',
  96. success: function(res) {
  97. resolve('data:image/png;base64,' + res.data)
  98. },
  99. fail: function(error) {
  100. reject(error)
  101. }
  102. })
  103. return
  104. }
  105. reject(new Error('not support'))
  106. })
  107. }
  108. export function base64ToPath(base64) {
  109. return new Promise(function(resolve, reject) {
  110. if (typeof window === 'object' && 'document' in window) {
  111. base64 = base64.split(',')
  112. var type = base64[0].match(/:(.*?);/)[1]
  113. var str = atob(base64[1])
  114. var n = str.length
  115. var array = new Uint8Array(n)
  116. while (n--) {
  117. array[n] = str.charCodeAt(n)
  118. }
  119. return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], { type: type })))
  120. }
  121. var extName = base64.match(/data\:\S+\/(\S+);/)
  122. if (extName) {
  123. extName = extName[1]
  124. } else {
  125. reject(new Error('base64 error'))
  126. }
  127. var fileName = getNewFileId() + '.' + extName
  128. if (typeof plus === 'object') {
  129. var basePath = '_doc'
  130. var dirPath = 'uniapp_temp'
  131. var filePath = basePath + '/' + dirPath + '/' + fileName
  132. if (!biggerThan(plus.os.name === 'Android' ? '1.9.9.80627' : '1.9.9.80472', plus.runtime.innerVersion)) {
  133. plus.io.resolveLocalFileSystemURL(basePath, function(entry) {
  134. entry.getDirectory(dirPath, {
  135. create: true,
  136. exclusive: false,
  137. }, function(entry) {
  138. entry.getFile(fileName, {
  139. create: true,
  140. exclusive: false,
  141. }, function(entry) {
  142. entry.createWriter(function(writer) {
  143. writer.onwrite = function() {
  144. resolve(filePath)
  145. }
  146. writer.onerror = reject
  147. writer.seek(0)
  148. writer.writeAsBinary(base64.replace(/^data:\S+\/\S+;base64,/, ''))
  149. }, reject)
  150. }, reject)
  151. }, reject)
  152. }, reject)
  153. return
  154. }
  155. var bitmap = new plus.nativeObj.Bitmap(fileName)
  156. bitmap.loadBase64Data(base64, function() {
  157. bitmap.save(filePath, {}, function() {
  158. bitmap.clear()
  159. resolve(filePath)
  160. }, function(error) {
  161. bitmap.clear()
  162. reject(error)
  163. })
  164. }, function(error) {
  165. bitmap.clear()
  166. reject(error)
  167. })
  168. return
  169. }
  170. if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
  171. var filePath = wx.env.USER_DATA_PATH + '/' + fileName
  172. wx.getFileSystemManager().writeFile({
  173. filePath: filePath,
  174. data: base64.replace(/^data:\S+\/\S+;base64,/, ''),
  175. encoding: 'base64',
  176. success: function() {
  177. resolve(filePath)
  178. },
  179. fail: function(error) {
  180. reject(error)
  181. }
  182. })
  183. return
  184. }
  185. reject(new Error('not support'))
  186. })
  187. }