index.js 6.8 KB

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