微信小程序数据请求汇总

get请求

function fetchGet(url, callback) {

// return callback(null, top250)

wx.request({

url: url,

header: { 'Content-Type': 'application/json' },

success (res) {

callback(null, res.data)

},

fail (e) {

console.error(e)

callback(e)

}

})

}

post请求

function fetchPost(url, data, callback) {

wx.request({

method: 'POST',

url: url,

data: data,

success (res) {

callback(null, res.data)

},

fail (e) {

console.error(e)

callback(e)

}

})

}


下拉刷新:

getData: function() {

var that = this;

var tab = that.data.tab;

var page = that.data.page;

var limit = that.data.limit;

var ApiUrl = Api.topics +'?tab='+ tab +'&page='+ page +'&limit='+ limit;

that.setData({ hidden: false });

Api.fetchGet(ApiUrl, (err, res) => {

//更新数据

that.setData({

postsList: that.data.postsList.concat(res.data.map(function (item) {

item.last_reply_at = util.getDateDiff(new Date(item.last_reply_at));

return item;

}))

});

setTimeout(function () {

that.setData({ hidden: true });

}, 300);

})

}

上拉加载:

lower: function() {

console.log('滑动底部加载', new Date());

var that = this;

that.setData({

page: that.data.page + 1

});

if (that.data.tab !== 'all') {

this.getData({tab: that.data.tab, page: that.data.page});

} else {

this.getData({page: that.data.page});

}

}

})

推荐阅读更多精彩内容