123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- // 用户图片同步
- // #ifdef APP-PLUS
- export const downloads = function(photos = [], callback) {
- if(!photos || photos.length <= 0) {
- return
- }
- const remotePhotos = photos.filter(item => {
- const nameIndex = item.photo.lastIndexOf('/')
- if(nameIndex < 0) {
- return false
- }
- const fileName = item.photo.substr(nameIndex + 1)
- const suffixIndex = fileName.lastIndexOf('.')
- if(suffixIndex < 0) {
- return false
- }
- return true
- }).map(item => {
- const params = item
- const nameIndex = item.photo.lastIndexOf('/')
- const fileName = item.photo.substr(nameIndex + 1)
- const suffixIndex = fileName.lastIndexOf('.')
- params.name = fileName.substr(0, suffixIndex)
- params.suffix = fileName.substr(suffixIndex)
- return params
- })
- plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, function(fs) {
- fs.root.getDirectory('photos', {create: true}, function(fileEntry) {
- const directoryReader = fileEntry.createReader()
- directoryReader.readEntries(
- function(entries) {
- const downlaodPhotos = []
- const dirPhotos = entries.filter(item => {
- if(!item.isFile) {
- return false
- }
- const suffixIndex = item.name.lastIndexOf('.')
- if(suffixIndex < 0) {
- return false
- }
- return true
- }).map(item => {
- const realName = item.name.substr(0, item.name.lastIndexOf('.'))
- const names = realName.split('--')
- const name = names[0]
- const safeThingsUserId = names[1] || null
- return {
- name,
- safeThingsUserId,
- file: item
- }
- })
- for(let i = 0; i < remotePhotos.length; i++) {
- const hasPhoto = dirPhotos.find(item => {
- // 判断是否同一个用户
- if(remotePhotos[i].safeThingsUserId == item.safeThingsUserId) {
- // 判断用户图片名称是否相同,不相同则说明有变化
- if(remotePhotos[i].name != item.name ) {
- downlaodPhotos.push(remotePhotos[i])
- // 删除本地图片
- item.file.remove()
- }
- return true
- }
- return false
- })
- if(!hasPhoto) {
- downlaodPhotos.push(remotePhotos[i])
- }
- }
- log('dirPhotos和downlaodPhotos', `大小:${dirPhotos.length}/${downlaodPhotos.length}`)
- if(downlaodPhotos && downlaodPhotos.length > 0) {
- let numbers = 0
- // 线上的图片url地址
- for(let i = 0; i < downlaodPhotos.length; i++) {
- const dask = plus.downloader.createDownload(downlaodPhotos[i].photo, {
- retry: 1,
- filename: `_downloads/photos/${downlaodPhotos[i].name}--${downlaodPhotos[i].safeThingsUserId}${downlaodPhotos[i].suffix}`
- }, function(download, status) {
- ++numbers
- if (status === 200) {}
- if(numbers === downlaodPhotos.length) {
- plus.downloader.clear()
- log('downloader', '下载完成1')
- if(callback) {
- callback({
- total: photos.length,
- available: remotePhotos.length,
- downlaods: downlaodPhotos.length
- })
- }
- }
- })
- dask.start()
- }
- } else {
- log('downloader', '下载完成2')
- if(callback) {
- callback({
- total: photos.length,
- available: remotePhotos.length,
- downlaods: downlaodPhotos.length
- })
- }
- }
- },
- function(err) {
- console.log("Read entries failed: " + err.message)
- log('entries failed', "Read entries failed: " + err.message)
- }
- )
- })
- })
- }
- const log = function(title, content) {
- // plus.nativeUI.alert(content, function(){}, title, "OK")
- }
- // #endif
|