1

很多页面除了接口地址和模板不一样外,几乎是相似的,比如用户的收益页面、提现记录页面,都是默认显示第一页数据,下拉刷新,加载更多,完全可以封装。

使用的时候这样就好了

const app = getApp();

var refreshLoad = app.refreshLoad(app).setApiName("getWithdrawList");

Page(refreshLoad);

库的封装代码

var x = {};

x.init = function(appInstance){
    this.app = appInstance;
    return this;
};

x.setApiName = function(name){
    this.apiName = name;
    return this;
};

x.data = {};

x.onLoad = function(option)
{
    this.setData(option);
    this.setPage(1);
    wx.startPullDownRefresh();
};

x.setPage = function(page)
{
    this.setData({page: page});
};

x.loadData = function()
{
    function success(response)
    {
        var dataTemp = response.data.data;
        var list = [];
        if(that.data.page > 1){
            list = that.data.list.concat(dataTemp.list);
        }
        else{
            list = dataTemp.list;
        }
        var data = {
            list: list,
            next: dataTemp.next,
            count: dataTemp.count
        };
        that.setData(data);
        wx.stopPullDownRefresh();
    }
    
    var that = this;
    var data = {
        token: this.app.dry.getToken(),
        device: this.app.config.device,
        page: this.data.page
    };
    this.app.r().setData(data).setSuccess(this.app.msg.ifFailStop).setSuccess(success).setLoadingText("正在加载数据").run(this.apiName);
};

x.onPullDownRefresh = function()
{
    this.setPage(1);
    this.loadData();
};

x.onReachBottom = function()
{
    if(this.data.next){
        this.setPage(this.data.page + 1);
        this.loadData();
    }
};

module.exports = x;

xxfaxy
1.6k 声望18 粉丝