!27 优化图库代码

Merge pull request !27 from sunjunxiong/master
This commit is contained in:
openharmony_ci 2021-10-08 02:38:40 +00:00 committed by Gitee
commit cec466c538
6 changed files with 248 additions and 88 deletions

View File

@ -26,7 +26,7 @@ let media = mediaLibrary.getMediaLibraryHelper();
const PROGRESS = 100; const PROGRESS = 100;
// 调用operations延时 // 调用operations延时
const OPERATION_TIME = 50; const OPERATION_TIME = 200;
// 照片类型 // 照片类型
const PHOTO_TYPE = 3; const PHOTO_TYPE = 3;
@ -86,7 +86,9 @@ export default {
let self = this; let self = this;
setTimeout(() => { setTimeout(() => {
self.$element('progress_dialog').show(); self.$element('progress_dialog').show();
this.operations(); setTimeout(() => {
self.operations();
}, OPERATION_TIME);
}, OPERATION_TIME); }, OPERATION_TIME);
}, },

View File

@ -54,6 +54,9 @@ const PAGE_SIZE = 10;
// 视频类型 // 视频类型
const VIDEO_TYPE = 4; const VIDEO_TYPE = 4;
// promise执行延时
const PROMISE_TIME = 100;
export default { export default {
data: { data: {
headTitle: '', headTitle: '',
@ -152,6 +155,8 @@ export default {
onShow() { onShow() {
this.utils.logDebug('main => onShow'); this.utils.logDebug('main => onShow');
if (this.$app.$def.dataManage.getRefreshed()) { if (this.$app.$def.dataManage.getRefreshed()) {
this.showEmptyDiv = true;
this.showEmptyDiv = false;
this.loadData(); this.loadData();
} }
}, },
@ -225,24 +230,7 @@ export default {
self.headSource = allRes; self.headSource = allRes;
} }
}); });
self.getAlbums().then(albumRes => { self.getAlbums();
if (albumRes.grid || albumRes.list) {
self.gridData = albumRes.grid;
self.cacheOtherList = albumRes.list;
if (albumRes.list && albumRes.list.length > PAGE_SIZE) {
self.listData = albumRes.list.slice(0, PAGE_SIZE);
} else {
self.listData = albumRes.list;
}
self.getVideoAlbum().then(videoRes => {
if (videoRes.src) {
self.cacheAlbums.push(videoRes);
self.gridData.push(videoRes);
}
});
self.utils.logDebug('main albumRes' + self.listData.length);
}
});
self.$app.$def.dataManage.isRefreshed(false); self.$app.$def.dataManage.isRefreshed(false);
}, },
@ -278,32 +266,105 @@ export default {
}, },
/** /**
* 获取所有相册数据 * 获取所有视频数据
* *
* @return {Promise} - 返回获取所有相册promise * @return {Promise} - 返回获取视频promise
*/ */
getAlbums() { getAllVideoAlbum() {
this.utils.logDebug('main => getAlbums => startTime');
let self = this; let self = this;
self.utils.logDebug('main => getAllVideoAlbum => startTime');
let args = { let args = {
selections: '', selections: '',
selectionArgs: ['imagealbum'], selectionArgs: ['imagealbum', 'videoalbum'],
}; };
return new Promise(function (resolve, reject) { return new Promise((resolve, reject) => {
media.getImageAlbums(args, (error, albums) => { media.getVideoAlbums(args, (error, albums) => {
self.utils.logDebug('main => getAlbums => endTime'); self.utils.logDebug('main => getAllVideoAlbum => endTime');
if (albums) { if (albums) {
self.cacheAlbums = albums; resolve(albums);
self.dealImageAlbums(albums).then(res => {
resolve(res);
});
} else { } else {
reject(new Error('get data error')); resolve([]);
} }
}); });
}); });
}, },
/**
* 获取所有照片数据
*
* @return {Promise} - 返回获取照片promise
*/
getAllImageAlbum() {
let self = this;
self.utils.logDebug('main => getAllImageAlbum => startTime');
let args = {
selections: '',
selectionArgs: ['imagealbum', 'videoalbum'],
};
return new Promise((resolve, reject) => {
media.getImageAlbums(args, (error, albums) => {
self.utils.logDebug('main => getAllImageAlbum => endTime');
if (albums) {
resolve(albums);
} else {
resolve([]);
}
});
});
},
/**
* 获取所有相册数据
*/
getAlbums() {
let self = this;
self.utils.logDebug('main => getAlbums => startTime');
let videoPromise = self.getAllVideoAlbum();
let imagePromise = self.getAllImageAlbum();
Promise.all([videoPromise, imagePromise]).then(result => {
let allAlbums = result[0].concat(result[1]) || [];
let array = [];
let obj = {};
allAlbums.forEach(item => {
if (!obj[item.albumName]) {
array.push(item);
obj[item.albumName] = 1;
} else {
obj[item.albumName]++;
}
});
allAlbums = array;
self.cacheAlbums = allAlbums;
self.dealImageAlbums(allAlbums).then(albumRes => {
if (albumRes.grid || albumRes.list) {
self.gridData = albumRes.grid;
self.cacheOtherList = albumRes.list;
if (albumRes.list && albumRes.list.length > PAGE_SIZE) {
self.listData = albumRes.list.slice(0, PAGE_SIZE);
} else {
self.listData = albumRes.list;
}
self.dealGetVideoAlbum();
}
});
});
},
/**
* 获取视频数据
*/
dealGetVideoAlbum() {
let self = this;
self.getVideoAlbum().then(videoRes => {
self.utils.logDebug('main => getAlbums => endTime');
if (videoRes.src && self.gridData.length < 2) {
self.cacheAlbums.push(videoRes);
self.gridData.push(videoRes);
}
});
},
/** /**
* 处理图片相册数据 * 处理图片相册数据
* *
@ -311,6 +372,7 @@ export default {
*/ */
dealImageAlbums(albums) { dealImageAlbums(albums) {
let self = this; let self = this;
self.utils.logDebug('main => dealImageAlbums => startTime');
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
let list = []; let list = [];
let grid = []; let grid = [];
@ -328,6 +390,7 @@ export default {
list: [] list: []
}; };
self.getAlbumImage(album).then((res) => { self.getAlbumImage(album).then((res) => {
self.utils.logDebug('main => dealImageAlbums => endTime');
if (res && res.length > 0) { if (res && res.length > 0) {
if (res[0].mediaType === VIDEO_TYPE) { if (res[0].mediaType === VIDEO_TYPE) {
gridObj.src = '/common/image/icon/video_poster.png'; gridObj.src = '/common/image/icon/video_poster.png';
@ -361,13 +424,15 @@ export default {
* @return {Promise} - 返回获取相册图片promise * @return {Promise} - 返回获取相册图片promise
*/ */
getAlbumImage(album) { getAlbumImage(album) {
this.utils.logDebug('main => getAlbumImage => startTime'); let self = this;
self.utils.logDebug('main => getAlbumImage => startTime');
let args = { let args = {
selections: album.albumName, selections: album.albumName,
selectionArgs: ['imagealbum', 'videoalbum'], selectionArgs: ['imagealbum', 'videoalbum'],
}; };
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
media.getMediaAssets(args, (error, images) => { media.getMediaAssets(args, (error, images) => {
self.utils.logDebug('main => getAlbumImage => endTime');
if (images !== null || images !== undefined) { if (images !== null || images !== undefined) {
resolve(images); resolve(images);
} else { } else {
@ -430,8 +495,8 @@ export default {
* @param {string} albumName - 新建相册名 * @param {string} albumName - 新建相册名
*/ */
createAlbum(albumName) { createAlbum(albumName) {
this.utils.logDebug('main => createAlbum => startTime');
let self = this; let self = this;
self.utils.logDebug('main => createAlbum => startTime');
self.$element('create_dialog').close(); self.$element('create_dialog').close();
media.createAlbum((err, album) => { media.createAlbum((err, album) => {
this.utils.logDebug('main => createAlbum => err' + err); this.utils.logDebug('main => createAlbum => err' + err);
@ -499,21 +564,31 @@ export default {
* @param {string} albumName - 相册名 * @param {string} albumName - 相册名
*/ */
modifyAlbumName(albumName) { modifyAlbumName(albumName) {
this.utils.logDebug('main => modifyAlbumName => startTime');
let self = this; let self = this;
self.utils.logDebug('main => modifyAlbumName => startTime');
self.$element('rename_dialog').close(); self.$element('rename_dialog').close();
let args = { let videoPromise = self.getAllVideoAlbum();
selections: '', let imagePromise = self.getAllImageAlbum();
selectionArgs: ['imagealbum'], setTimeout(() => {
}; Promise.all([videoPromise, imagePromise]).then(result => {
media.getImageAlbums(args, (error, albums) => { let allAlbums = result[0].concat(result[1]) || [];
if (albums) { let array = [];
for (let j = 0; j < albums.length; j++) { let obj = {};
let item = albums[j]; allAlbums.forEach(item => {
if (!obj[item.albumName]) {
array.push(item);
obj[item.albumName] = 1;
} else {
obj[item.albumName]++;
}
});
allAlbums = array;
for (let j = 0; j < allAlbums.length; j++) {
let item = allAlbums[j];
self.dealCommitModifyAlbum(item, albumName); self.dealCommitModifyAlbum(item, albumName);
} }
} });
}); }, PROMISE_TIME);
}, },
/** /**

View File

@ -261,7 +261,7 @@ export default {
self.utils.logDebug('photoDetail => getVideoAsset => endTime'); self.utils.logDebug('photoDetail => getVideoAsset => endTime');
self.cacheList = videos || []; self.cacheList = videos || [];
if (videos && videos.length > 0) { if (videos && videos.length > 0) {
let list = self.dealListData(videos || []); let list = self.utils.deepCopy(self.dealListData(videos || []));
if (self.refreshType === DELETE_REFRESH_TYPE) { if (self.refreshType === DELETE_REFRESH_TYPE) {
if (self.shareIndex > 0) { if (self.shareIndex > 0) {
self.shareIndex--; self.shareIndex--;
@ -330,7 +330,7 @@ export default {
media.getMediaAssets(args, (error, images) => { media.getMediaAssets(args, (error, images) => {
self.utils.logDebug('photoDetail => getAlbumAsset => endTime'); self.utils.logDebug('photoDetail => getAlbumAsset => endTime');
self.cacheList = images || []; self.cacheList = images || [];
let list = self.dealListData(images || []); let list = self.utils.deepCopy(self.dealListData(images || []));
if (self.refreshType === DELETE_REFRESH_TYPE) { if (self.refreshType === DELETE_REFRESH_TYPE) {
if (self.shareIndex > 0) { if (self.shareIndex > 0) {
self.shareIndex--; self.shareIndex--;
@ -384,7 +384,6 @@ export default {
if (self.list.length > 0) { if (self.list.length > 0) {
let item = self.list[self.shareIndex]; let item = self.list[self.shareIndex];
self.currentItem = item; self.currentItem = item;
self.utils.logDebug(' === > ' + JSON.stringify(self.currentItem) + ' ');
self.topBarSource.title = self.currentItem.name; self.topBarSource.title = self.currentItem.name;
if (self.currentItem.mediaType === VIDEO_TYPE) { if (self.currentItem.mediaType === VIDEO_TYPE) {
self.showScale = false; self.showScale = false;
@ -438,7 +437,6 @@ export default {
* 滑动回调 * 滑动回调
* *
* @param {Object} item - 滑动后当前显示项 * @param {Object} item - 滑动后当前显示项
* @return {boolean} Verify result
*/ */
swiperChange(item) { swiperChange(item) {
this.utils.logDebug('photoDetail => swiperChange'); this.utils.logDebug('photoDetail => swiperChange');
@ -461,6 +459,15 @@ export default {
} else { } else {
this.topBarSource.title = this.currentItem.name; this.topBarSource.title = this.currentItem.name;
} }
this.swiperBottom();
},
/**
* 滑动到最后
*
* @return {boolean} Verify result
*/
swiperBottom() {
let cacheLen = this.cacheSource.length; let cacheLen = this.cacheSource.length;
let listLen = this.list.length; let listLen = this.list.length;
if (this.shareIndex === listLen - 1) { if (this.shareIndex === listLen - 1) {
@ -886,7 +893,6 @@ export default {
* @param {Object} item - 列表点击当前项 * @param {Object} item - 列表点击当前项
*/ */
videoPause(item) { videoPause(item) {
item.isPause = true;
this.utils.logDebug('videoPause'); this.utils.logDebug('videoPause');
}, },

View File

@ -31,6 +31,9 @@ const PAGE_SIZE = 10;
// 视频类型 // 视频类型
const VIDEO_TYPE = 4; const VIDEO_TYPE = 4;
// promise执行延时
const PROMISE_TIME = 100;
export default { export default {
data: { data: {
listData: [], listData: [],
@ -85,51 +88,126 @@ export default {
}, },
/** /**
* 获取相册 * 获取所有视频数据
*
* @return {Promise} - 返回获取视频promise
*/ */
getAlbums() { getAllVideoAlbum() {
let self = this; let self = this;
self.utils.logDebug('selectAlbum => getAllVideoAlbum => startTime');
let args = { let args = {
selections: '', selections: '',
selectionArgs: ['imagealbum'], selectionArgs: ['imagealbum', 'videoalbum'],
}; };
self.utils.logDebug('selectAlbum => getImageAlbums => startTime'); return new Promise((resolve, reject) => {
media.getImageAlbums(args, (error, albums) => { media.getVideoAlbums(args, (error, albums) => {
self.utils.logDebug('selectAlbum => getImageAlbums => endTime'); self.utils.logDebug('selectAlbum => getAllVideoAlbum => endTime');
if (albums) { if (albums) {
let list = []; resolve(albums);
let others = []; } else {
for (let i = 0; i < albums.length; i++) { resolve([]);
let album = albums[i];
let gridObj = {
name: album.albumName,
id: album.albumId,
icon: '/common/image/svg/arrow-right.svg',
checked: false,
showNumber: true,
src: self.$app.$def.utils.getIcon('default'),
list: []
};
setTimeout(() => {
self.getMediaAssets(gridObj, album, list, others);
}, (i + 1) * LOAD_DATA_TIME);
} }
setTimeout(() => { });
if (others.length > PAGE_SIZE) { });
self.otherList = others.slice(0, PAGE_SIZE); },
} else {
self.otherList = others; /**
} * 获取所有照片数据
self.cacheList = others; *
self.listData = list; * @return {Promise} - 返回获取照片promise
}, (albums.length + 1) * LOAD_DATA_TIME); */
} getAllImageAlbum() {
let self = this;
self.utils.logDebug('selectAlbum => getAllImageAlbum => startTime');
let args = {
selections: '',
selectionArgs: ['imagealbum', 'videoalbum'],
};
return new Promise((resolve, reject) => {
media.getImageAlbums(args, (error, albums) => {
self.utils.logDebug('selectAlbum => getAllImageAlbum => endTime');
if (albums) {
resolve(albums);
} else {
resolve([]);
}
});
}); });
}, },
/** /**
* 获取相册 * 获取相册
*/ */
getAlbums() {
let self = this;
self.utils.logDebug('selectAlbum => getAlbums => startTime');
let videoPromise = self.getAllVideoAlbum();
let imagePromise = self.getAllImageAlbum();
setTimeout(() => {
Promise.all([videoPromise, imagePromise]).then(result => {
let allAlbums = result[0].concat(result[1]) || [];
let array = [];
let obj = {};
allAlbums.forEach(item => {
if (!obj[item.albumName]) {
array.push(item);
obj[item.albumName] = 1;
} else {
obj[item.albumName]++;
}
});
allAlbums = array;
self.setAlbumList(allAlbums);
self.utils.logDebug('selectAlbum => getAlbums => endTime');
});
}, PROMISE_TIME);
},
/**
* 获取相册
*
* @param {Array} allAlbums - 相册数据
*/
setAlbumList(allAlbums) {
let self = this;
if (allAlbums.length > 0) {
let list = [];
let others = [];
for (let i = 0; i < allAlbums.length; i++) {
let album = allAlbums[i];
let gridObj = {
name: album.albumName,
id: album.albumId,
icon: '/common/image/svg/arrow-right.svg',
checked: false,
showNumber: true,
src: self.$app.$def.utils.getIcon('default'),
list: []
};
setTimeout(() => {
self.getMediaAssets(gridObj, album, list, others);
}, (i + 1) * LOAD_DATA_TIME);
}
setTimeout(() => {
if (others.length > PAGE_SIZE) {
self.otherList = others.slice(0, PAGE_SIZE);
} else {
self.otherList = others;
}
self.cacheList = others;
self.listData = list;
}, (allAlbums.length + 1) * LOAD_DATA_TIME);
}
},
/**
* 获取相册
*
* @param {Object} gridObj - 数据对象
* @param {Object} album - 被操作对象
* @param {Array} list - 相册数据
* @param {Array} others - 其他相册数据
*/
getMediaAssets(gridObj, album, list, others) { getMediaAssets(gridObj, album, list, others) {
let self = this; let self = this;
let args = { let args = {
@ -141,7 +219,8 @@ export default {
self.utils.logDebug('selectAlbum => getMediaAssets => endTime =>'); self.utils.logDebug('selectAlbum => getMediaAssets => endTime =>');
if (images && images.length > 0) { if (images && images.length > 0) {
gridObj.src = images[0].mediaType === VIDEO_TYPE gridObj.src = images[0].mediaType === VIDEO_TYPE
? '/common/image/icon/video_poster.png' : 'file://' + images[0].URI; ? '/common/image/icon/video_poster.png'
: 'file://' + images[0].URI;
gridObj.list = images; gridObj.list = images;
} }
// 判断是新建选相册 还是操作选相册 // 判断是新建选相册 还是操作选相册

View File

@ -42,9 +42,6 @@
</image> </image>
<image if="{{ selectMode }}" class="select" src="{{ item.icon }}"> <image if="{{ selectMode }}" class="select" src="{{ item.icon }}">
</image> </image>
<text else class="video-duration" onclick="videoClick({{ item }},{{ index }})">
{{ item.duration }}
</text>
</stack> </stack>
</div> </div>
</list-item> </list-item>

View File

@ -541,6 +541,7 @@ export default {
if (commitFlag) { if (commitFlag) {
self.$app.$def.dataManage.isRefreshed(true); self.$app.$def.dataManage.isRefreshed(true);
self.selectMode = false; self.selectMode = false;
self.$app.$def.dataManage.setPhotoList([]);
setTimeout(() => { setTimeout(() => {
if (choose.length === self.listData.length) { if (choose.length === self.listData.length) {
self.listData = []; self.listData = [];