Add server and mini-program pagination for shared lists
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
function itemsOf(result) { return Array.isArray(result) ? result : (result && result.items) || []; }
|
||||
function totalOf(result) { return result && result.total != null ? Number(result.total) : itemsOf(result).length; }
|
||||
function startList(page, number) {
|
||||
number = Number(number) || 1;
|
||||
if (page._paginationFirst || (number > 1 && (page.data.loadingMore || page.data.loading || page.data.loadError || !page.data.hasMore))) return null;
|
||||
if (number === 1) { page._paginationFirst = true; page._paginationGeneration = (page._paginationGeneration || 0) + 1; }
|
||||
const ticket = { page: number, generation: page._paginationGeneration };
|
||||
page.setData(number === 1 ? {loading: !page._hasCachedData, loadError:false, loadingMore:false, moreError:false} : {loadingMore:true, moreError:false});
|
||||
return ticket;
|
||||
}
|
||||
function current(page, ticket) { return ticket.generation === page._paginationGeneration; }
|
||||
function mergeUnique(previous, added) {
|
||||
const seen=new Set();return previous.concat(added).filter(item=>{const id=String(item.id);if(seen.has(id))return false;seen.add(id);return true;});
|
||||
}
|
||||
function applyList(page, ticket, result, items, field, extra) {
|
||||
if (!current(page,ticket)) return;
|
||||
const list=ticket.page===1?items:mergeUnique(page.data[field]||[],items);
|
||||
const total=totalOf(result);
|
||||
page.setData(Object.assign({[field]:list,page:ticket.page,total,hasMore:!Array.isArray(result)&&(result.has_more != null ? !!result.has_more : ticket.page*20<total),moreError:false},extra));
|
||||
}
|
||||
function failList(page,ticket) { if(current(page,ticket))page.setData(ticket.page===1?{loadError:true}:{moreError:true}); }
|
||||
function finishList(page,ticket) { if(current(page,ticket)){if(ticket.page===1)page._paginationFirst=false;page.setData({loading:false,loadingMore:false});} }
|
||||
function withListPagination(definition, loader) {
|
||||
Object.assign(definition.data,{page:1,total:0,hasMore:false,loadingMore:false,moreError:false});
|
||||
const load=definition[loader];
|
||||
definition.loadMore=function(){return load.call(this,(this.data.page||1)+1);};
|
||||
return definition;
|
||||
}
|
||||
module.exports={itemsOf,totalOf,startList,current,mergeUnique,applyList,failList,finishList,withListPagination};
|
||||
@@ -263,21 +263,21 @@ const api = {
|
||||
updatePartnerProfile: (data) => request('updatePartnerProfile', data),
|
||||
|
||||
// 时间线
|
||||
timelineList: () => request('timelineList', null, 'GET'),
|
||||
timelineList: (data = {}) => request('timelineList', { page: 1, page_size: 20, ...data }, 'GET'),
|
||||
timelineAdd: (data) => request('timelineAdd', data),
|
||||
timelineUpdate: (data) => request('timelineUpdate', data),
|
||||
timelineDelete: (data) => request('timelineDelete', data),
|
||||
timelineDetail: (id) => request('timelineDetail', { id: id }, 'GET'),
|
||||
|
||||
// 相册
|
||||
albumList: (data, method) => request('albumList', data, method || 'GET'),
|
||||
albumList: (data, method) => request('albumList', { page: 1, page_size: 20, ...data }, method || 'GET'),
|
||||
albumAdd: (data) => request('albumAdd', data),
|
||||
albumUpdate: (data) => request('albumUpdate', data),
|
||||
albumDelete: (data) => request('albumDelete', data),
|
||||
photoDelete: (data) => request('photoDelete', data),
|
||||
photoAdd: (data) => request('photoAdd', data),
|
||||
photoUpdate: (data) => request('photoUpdate', data),
|
||||
photos: (albumId, data, method) => request('photos', { album_id: albumId, ...data }, method || 'GET'),
|
||||
photos: (albumId, data, method) => request('photos', { page: 1, page_size: 20, album_id: albumId, ...data }, method || 'GET'),
|
||||
photoUpload: async (filePath, albumId) => {
|
||||
wx.showLoading({ title: '上传中...' });
|
||||
try {
|
||||
@@ -351,21 +351,21 @@ const api = {
|
||||
diaryUpdate: (data) => request('diaryUpdate', data),
|
||||
diaryDelete: (data) => request('diaryDelete', data),
|
||||
diaryDetail: (id) => request('diaryDetail', { id: id }, 'GET'),
|
||||
diaryCommentList: (data) => request('diaryCommentList', data, 'GET'),
|
||||
diaryCommentList: (data) => request('diaryCommentList', { page: 1, page_size: 20, ...data }, 'GET'),
|
||||
diaryCommentAdd: (data) => request('diaryCommentAdd', data),
|
||||
diaryCommentDelete: (data) => request('diaryCommentDelete', data),
|
||||
// 点赞/收藏功能已移除
|
||||
|
||||
// 一起听过的歌
|
||||
songsList: () => request('songsList', null, 'GET'),
|
||||
songsList: (data = {}) => request('songsList', { page: 1, page_size: 20, ...data }, 'GET'),
|
||||
songsAdd: (data) => request('songsAdd', data),
|
||||
songsUpdate: (data) => request('songsUpdate', data),
|
||||
songsDelete: (data) => request('songsDelete', data),
|
||||
|
||||
// 打卡
|
||||
smokingInfo: (userId) => request("smokingInfo", { user_id: userId || 0 }, "GET"),
|
||||
smokingInfo: (userId, data = {}) => request("smokingInfo", { page: 1, page_size: 20, ...data, user_id: userId || 0 }, "GET"),
|
||||
smokingAction: (data) => request("smokingAction", data),
|
||||
checkinList: () => request('checkinList', null, 'GET'),
|
||||
checkinList: (data = {}) => request('checkinList', { page: 1, page_size: 20, ...data }, 'GET'),
|
||||
checkinAdd: (data) => request('checkinAdd', data),
|
||||
checkinDo: (data) => request('checkinDo', data),
|
||||
checkinStats: () => request('checkinStats', null, 'GET'),
|
||||
|
||||
Reference in New Issue
Block a user