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.

index.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. function getLocalFilePath(path) {
  2. if (path.indexOf('_www') === 0 || path.indexOf('_doc') === 0 || path.indexOf('_documents') === 0 || path.indexOf(
  3. '_downloads') === 0) {
  4. return path
  5. }
  6. if (path.indexOf('file://') === 0) {
  7. return path
  8. }
  9. if (path.indexOf('/storage/emulated/0/') === 0) {
  10. return path
  11. }
  12. if (path.indexOf('/') === 0) {
  13. var localFilePath = plus.io.convertAbsoluteFileSystem(path)
  14. if (localFilePath !== path) {
  15. return localFilePath
  16. } else {
  17. path = path.substr(1)
  18. }
  19. }
  20. return '_www/' + path
  21. }
  22. function dataUrlToBase64(str) {
  23. var array = str.split(',')
  24. return array[array.length - 1]
  25. }
  26. var index = 0
  27. function getNewFileId() {
  28. return Date.now() + String(index++)
  29. }
  30. function biggerThan(v1, v2) {
  31. var v1Array = v1.split('.')
  32. var v2Array = v2.split('.')
  33. var update = false
  34. for (var index = 0; index < v2Array.length; index++) {
  35. var diff = v1Array[index] - v2Array[index]
  36. if (diff !== 0) {
  37. update = diff > 0
  38. break
  39. }
  40. }
  41. return update
  42. }
  43. export function pathToBase64(path) {
  44. return new Promise(function(resolve, reject) {
  45. if (typeof window === 'object' && 'document' in window) {
  46. if (typeof FileReader === 'function') {
  47. var xhr = new XMLHttpRequest()
  48. xhr.open('GET', path, true)
  49. xhr.responseType = 'blob'
  50. xhr.onload = function() {
  51. if (this.status === 200) {
  52. let fileReader = new FileReader()
  53. fileReader.onload = function(e) {
  54. resolve(e.target.result)
  55. }
  56. fileReader.onerror = reject
  57. fileReader.readAsDataURL(this.response)
  58. }
  59. }
  60. xhr.onerror = reject
  61. xhr.send()
  62. return
  63. }
  64. var canvas = document.createElement('canvas')
  65. var c2x = canvas.getContext('2d')
  66. var img = new Image
  67. img.onload = function() {
  68. canvas.width = img.width
  69. canvas.height = img.height
  70. c2x.drawImage(img, 0, 0)
  71. resolve(canvas.toDataURL())
  72. canvas.height = canvas.width = 0
  73. }
  74. img.onerror = reject
  75. img.src = path
  76. return
  77. }
  78. if (typeof plus === 'object') {
  79. plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), function(entry) {
  80. entry.file(function(file) {
  81. var fileReader = new plus.io.FileReader()
  82. fileReader.onload = function(data) {
  83. resolve(data.target.result)
  84. }
  85. fileReader.onerror = function(error) {
  86. reject(error)
  87. }
  88. fileReader.readAsDataURL(file)
  89. }, function(error) {
  90. reject(error)
  91. })
  92. }, function(error) {
  93. reject(error)
  94. })
  95. return
  96. }
  97. if (typeof wx === 'object' && uni.canIUse('getFileSystemManager')) {
  98. uni.getFileSystemManager().readFile({
  99. filePath: path,
  100. encoding: 'base64',
  101. success: function(res) {
  102. resolve('data:image/png;base64,' + res.data)
  103. },
  104. fail: function(error) {
  105. reject(error)
  106. }
  107. })
  108. return
  109. }
  110. if (typeof my === 'object' && uni.canIUse('getFileSystemManager')) {
  111. uni.getFileSystemManager().readFile({
  112. filePath: path,
  113. encoding: 'base64',
  114. success: function(res) {
  115. resolve('data:image/png;base64,' + res.data)
  116. },
  117. fail: function(error) {
  118. reject(error)
  119. }
  120. })
  121. return
  122. }
  123. reject(new Error('not support'))
  124. })
  125. }
  126. // export function pathToBase64(path) {
  127. // return new Promise(function(resolve, reject) {
  128. // u.getFileSystemManager().readFile({ //读取本地文件内容
  129. // filePath: tempFilePath, // 文件路径
  130. // encoding: 'base64', // 返回格式
  131. // success: ({
  132. // data
  133. // }) => {
  134. // return resolve('data:image/png;base64,' + data);
  135. // }
  136. // });
  137. // reject(new Error('not support'))
  138. // })
  139. // }
  140. export function base64ToPath(base64) {
  141. return new Promise(function(resolve, reject) {
  142. if (typeof window === 'object' && 'document' in window) {
  143. base64 = base64.split(',')
  144. var type = base64[0].match(/:(.*?);/)[1]
  145. var str = atob(base64[1])
  146. var n = str.length
  147. var array = new Uint8Array(n)
  148. while (n--) {
  149. array[n] = str.charCodeAt(n)
  150. }
  151. return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], {
  152. type: type
  153. })))
  154. }
  155. var extName = base64.split(',')[0].match(/data\:\S+\/(\S+);/)
  156. if (extName) {
  157. extName = extName[1]
  158. } else {
  159. reject(new Error('base64 error'))
  160. }
  161. var fileName = getNewFileId() + '.' + extName
  162. if (typeof plus === 'object') {
  163. var basePath = '_doc'
  164. var dirPath = 'uniapp_temp'
  165. var filePath = basePath + '/' + dirPath + '/' + fileName
  166. if (!biggerThan(plus.os.name === 'Android' ? '1.9.9.80627' : '1.9.9.80472', plus.runtime
  167. .innerVersion)) {
  168. plus.io.resolveLocalFileSystemURL(basePath, function(entry) {
  169. entry.getDirectory(dirPath, {
  170. create: true,
  171. exclusive: false,
  172. }, function(entry) {
  173. entry.getFile(fileName, {
  174. create: true,
  175. exclusive: false,
  176. }, function(entry) {
  177. entry.createWriter(function(writer) {
  178. writer.onwrite = function() {
  179. resolve(filePath)
  180. }
  181. writer.onerror = reject
  182. writer.seek(0)
  183. writer.writeAsBinary(dataUrlToBase64(base64))
  184. }, reject)
  185. }, reject)
  186. }, reject)
  187. }, reject)
  188. return
  189. }
  190. var bitmap = new plus.nativeObj.Bitmap(fileName)
  191. bitmap.loadBase64Data(base64, function() {
  192. bitmap.save(filePath, {}, function() {
  193. bitmap.clear()
  194. resolve(filePath)
  195. }, function(error) {
  196. bitmap.clear()
  197. reject(error)
  198. })
  199. }, function(error) {
  200. bitmap.clear()
  201. reject(error)
  202. })
  203. return
  204. }
  205. if (typeof wx === 'object' && uni.canIUse('getFileSystemManager')) {
  206. var filePath = uni.env.USER_DATA_PATH + '/' + fileName
  207. uni.getFileSystemManager().writeFile({
  208. filePath: filePath,
  209. data: dataUrlToBase64(base64),
  210. encoding: 'base64',
  211. success: function() {
  212. resolve(filePath)
  213. },
  214. fail: function(error) {
  215. reject(error)
  216. }
  217. })
  218. return
  219. }
  220. if (typeof my === 'object' && uni.canIUse('getFileSystemManager')) {
  221. var filePath = uni.env.USER_DATA_PATH + '/' + fileName
  222. uni.getFileSystemManager().writeFile({
  223. filePath: filePath,
  224. data: dataUrlToBase64(base64),
  225. encoding: 'base64',
  226. success: function() {
  227. resolve(filePath)
  228. },
  229. fail: function(error) {
  230. reject(error)
  231. }
  232. })
  233. return
  234. }
  235. reject(new Error('not support'))
  236. })
  237. }
  238. export function pathToBase64New(path) {
  239. return new Promise(function(resolve, reject) {
  240. if (typeof window === 'object' && 'document' in window) {
  241. if (typeof FileReader === 'function') {
  242. var xhr = new XMLHttpRequest()
  243. xhr.open('GET', path, true)
  244. xhr.responseType = 'blob'
  245. xhr.onload = function() {
  246. if (this.status === 200) {
  247. let fileReader = new FileReader()
  248. fileReader.onload = function(e) {
  249. resolve(e.target.result)
  250. }
  251. fileReader.onerror = reject
  252. fileReader.readAsDataURL(this.response)
  253. }
  254. }
  255. xhr.onerror = reject
  256. xhr.send()
  257. return
  258. }
  259. var canvas = document.createElement('canvas')
  260. var c2x = canvas.getContext('2d')
  261. var img = new Image
  262. img.onload = function() {
  263. canvas.width = img.width
  264. canvas.height = img.height
  265. c2x.drawImage(img, 0, 0)
  266. resolve(canvas.toDataURL())
  267. canvas.height = canvas.width = 0
  268. }
  269. img.onerror = reject
  270. img.src = path
  271. return
  272. }
  273. if (typeof plus === 'object') {
  274. plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), function(entry) {
  275. entry.file(function(file) {
  276. var fileReader = new plus.io.FileReader()
  277. fileReader.onload = function(data) {
  278. resolve(data.target.result)
  279. }
  280. fileReader.onerror = function(error) {
  281. reject(error)
  282. }
  283. fileReader.readAsDataURL(file)
  284. }, function(error) {
  285. reject(error)
  286. })
  287. }, function(error) {
  288. reject(error)
  289. })
  290. return
  291. }
  292. if (typeof wx === 'object' && uni.canIUse('getFileSystemManager')) {
  293. uni.getFileSystemManager().readFile({
  294. filePath: path,
  295. encoding: 'base64',
  296. success: function(res) {
  297. resolve(res.data)
  298. },
  299. fail: function(error) {
  300. reject(error)
  301. }
  302. })
  303. return
  304. }
  305. if (typeof my === 'object' && uni.canIUse('getFileSystemManager')) {
  306. uni.getFileSystemManager().readFile({
  307. filePath: path,
  308. encoding: 'base64',
  309. success: function(res) {
  310. resolve(res.data)
  311. },
  312. fail: function(error) {
  313. reject(error)
  314. }
  315. })
  316. return
  317. }
  318. reject(new Error('not support'))
  319. })
  320. }