synchronization-persons.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // 用户图片同步
  2. // #ifdef APP-PLUS
  3. export const downloads = function(photos = [], callback) {
  4. if(!photos || photos.length <= 0) {
  5. return
  6. }
  7. const remotePhotos = photos.filter(item => {
  8. const nameIndex = item.photo.lastIndexOf('/')
  9. if(nameIndex < 0) {
  10. return false
  11. }
  12. const fileName = item.photo.substr(nameIndex + 1)
  13. const suffixIndex = fileName.lastIndexOf('.')
  14. if(suffixIndex < 0) {
  15. return false
  16. }
  17. return true
  18. }).map(item => {
  19. const params = item
  20. const nameIndex = item.photo.lastIndexOf('/')
  21. const fileName = item.photo.substr(nameIndex + 1)
  22. const suffixIndex = fileName.lastIndexOf('.')
  23. params.name = fileName.substr(0, suffixIndex)
  24. params.suffix = fileName.substr(suffixIndex)
  25. return params
  26. })
  27. plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, function(fs) {
  28. fs.root.getDirectory('photos', {create: true}, function(fileEntry) {
  29. const directoryReader = fileEntry.createReader()
  30. directoryReader.readEntries(
  31. function(entries) {
  32. const downlaodPhotos = []
  33. const dirPhotos = entries.filter(item => {
  34. if(!item.isFile) {
  35. return false
  36. }
  37. const suffixIndex = item.name.lastIndexOf('.')
  38. if(suffixIndex < 0) {
  39. return false
  40. }
  41. return true
  42. }).map(item => {
  43. const realName = item.name.substr(0, item.name.lastIndexOf('.'))
  44. const names = realName.split('--')
  45. const name = names[0]
  46. const safeThingsUserId = names[1] || null
  47. return {
  48. name,
  49. safeThingsUserId,
  50. file: item
  51. }
  52. })
  53. for(let i = 0; i < remotePhotos.length; i++) {
  54. const hasPhoto = dirPhotos.find(item => {
  55. // 判断是否同一个用户
  56. if(remotePhotos[i].safeThingsUserId == item.safeThingsUserId) {
  57. // 判断用户图片名称是否相同,不相同则说明有变化
  58. if(remotePhotos[i].name != item.name ) {
  59. downlaodPhotos.push(remotePhotos[i])
  60. // 删除本地图片
  61. item.file.remove()
  62. }
  63. return true
  64. }
  65. return false
  66. })
  67. if(!hasPhoto) {
  68. downlaodPhotos.push(remotePhotos[i])
  69. }
  70. }
  71. log('dirPhotos和downlaodPhotos', `大小:${dirPhotos.length}/${downlaodPhotos.length}`)
  72. if(downlaodPhotos && downlaodPhotos.length > 0) {
  73. let numbers = 0
  74. // 线上的图片url地址
  75. for(let i = 0; i < downlaodPhotos.length; i++) {
  76. const dask = plus.downloader.createDownload(downlaodPhotos[i].photo, {
  77. retry: 1,
  78. filename: `_downloads/photos/${downlaodPhotos[i].name}--${downlaodPhotos[i].safeThingsUserId}${downlaodPhotos[i].suffix}`
  79. }, function(download, status) {
  80. ++numbers
  81. if (status === 200) {}
  82. if(numbers === downlaodPhotos.length) {
  83. plus.downloader.clear()
  84. log('downloader', '下载完成1')
  85. if(callback) {
  86. callback({
  87. total: photos.length,
  88. available: remotePhotos.length,
  89. downlaods: downlaodPhotos.length
  90. })
  91. }
  92. }
  93. })
  94. dask.start()
  95. }
  96. } else {
  97. log('downloader', '下载完成2')
  98. if(callback) {
  99. callback({
  100. total: photos.length,
  101. available: remotePhotos.length,
  102. downlaods: downlaodPhotos.length
  103. })
  104. }
  105. }
  106. },
  107. function(err) {
  108. console.log("Read entries failed: " + err.message)
  109. log('entries failed', "Read entries failed: " + err.message)
  110. }
  111. )
  112. })
  113. })
  114. }
  115. const log = function(title, content) {
  116. // plus.nativeUI.alert(content, function(){}, title, "OK")
  117. }
  118. // #endif