123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- // README WISEWOODS 重写requestData 返回分页数据即可 格式规定(code msg data:{total, rows实际列表数据})
- // 配合loadmore 可以实现 下拉刷新和加载更多的逻辑 + 视图显示
- const orgainPage = Page; // 保存原本的Page对象
- const basePage = function (page) {
- // 生成初始data,如果页面已经有该值不在重新赋值
- if (typeof page.data.rows === 'undefined') {
- page.data.rows = [];
- }
- if (typeof page.total === 'undefined') {
- page.total = 0;
- }
- if (typeof page.pageNum === 'undefined') {
- page.pageNum = 1;
- }
- if (typeof page.pageSize === 'undefined') {
- page.pageSize = 10;
- }
- if (typeof page.data.noMore === 'undefined') {
- page.data.noMore = false;
- }
- if (typeof page.data.isRefresh === 'undefined') {
- page.data.isRefresh = false;
- }
- if (typeof page.data.isLoading === 'undefined') {
- page.data.isLoading = false;
- }
- if (typeof page.data.disableLoadMore === 'undefined') {
- page.data.disableLoadMore = false;
- }
- const orgainOnLoad = page.onLoad;
- page.onLoad = async function (o) {
- if (orgainOnLoad) {
- await orgainOnLoad.call(this, o);
- }
- // 执行的初始事件 start
- await resetData(this);
- // 执行的初始事件 end
- }
- const orgainOnPullDownRefresh = page.onPullDownRefresh;
- page.onPullDownRefresh = async function (o) {
- if (orgainOnPullDownRefresh) {
- await orgainOnPullDownRefresh.call(this, o);
- }
- // 执行的初始事件 start
- await resetData(this);
- // 执行的初始事件 end
- }
- const orgainOnReachBottom = page.onReachBottom;
- page.onReachBottom = async function (o) {
- if (orgainOnReachBottom) {
- await orgainOnReachBottom.call(this, o);
- }
- if (this.data.disableLoadMore) {
- return;
- }
- // 执行的初始事件 start
- if (this.data.isRefresh || this.data.isLoading) {
- return;
- }
- if (this.data.noMore) {
- // toast('没有更多了')
- return;
- }
- this.setData({
- isLoading: true
- })
- // this.data.isLoading = true;
- this.pageNum++;
- await getData(this);
- // 执行的初始事件 end
- }
- page.getData = getData;
- page.resetData = resetData;
- page.changeResetData = changeResetData;
- return orgainPage(page);
- };
- async function getData(page) {
- try {
- const res = await page.requestData();
- if (page.pageNum == 1) {
- page.total = res.data.total;
- page.setData({
- rows: res.data.rows,
- })
- } else {
- const newArr = [...page.data.rows].concat(res.data.rows)
- page.setData({
- rows: newArr
- })
- }
- if (page.data.rows.length >= page.total || res.data.rows.length < page.pageSize) {
- // page.data.noMore = true;
- page.setData({
- noMore: true
- })
- // page.data.noMore = true;
- // if (page.pageNum != 1){
- // toast('没有更多了')
- // }
- }
- // page.data.isLoading = false;
- // page.data.isRefresh = false;
- page.setData({
- isLoading: false,
- isRefresh: false,
- })
- wx.stopPullDownRefresh();
- } catch (e) {
- console.log(e)
- page.setData({
- isLoading: false,
- isRefresh: false,
- })
- wx.stopPullDownRefresh();
- throw new Error(e.errMsg)
- }
- }
- async function resetData(page) {
- if (page.data.isRefresh || page.data.isLoading) {
- return;
- }
- page.setData({
- noMore: false,
- isRefresh: true,
- rows: []
- })
- // page.data.noMore = false;
- // page.data.isRefresh = true;
- // page.data.rows = [];
- page.pageNum = 1;
- await getData(page);
- }
- /**
- * 针对页面tab布局切换tab同享同一个区域列表 需要增加一个进度防止快速切换导致bug
- * @param page
- */
- async function changeResetData(page) {
- wx.showLoading({
- title: '加载中...',
- mask: true
- });
- await resetData(page);
- wx.hideLoading();
- }
- export default basePage;
|