// pages/index/index.js import { getProjectList } from "../../utils/getApiUrl"; import { Request } from "../../common"; Page({ /** * 页面的初始数据 */ data: { dataList: [], limit: 10, //条数 page: 0, //当前页 count: '', //总条数 keyName: '', //结构物名称 hidden: true, }, // 输入框 formInp(e) { let that = this; that.setData({ keyName: e.detail.value }) }, // 手机键盘点击完成按钮(回车事件) bindconfirm() { let that = this; that.setData({ page: 0, //当前页 count: '', //总条数 }) that.getProjectList(); }, // 搜索表单 goSearch() { let that = this; that.setData({ page: 0, //当前页 count: '', //总条数 }) that.getProjectList(); }, // 获取结构物列表 getProjectList: function () { let that = this; let { page, limit, keyName } = that.data; Request.get(getProjectList(), { limit, page, name: keyName }).then(res => { console.log(res); if (res.rows.length == 0) { that.setData({ dataList: res.rows, hidden: false }) return; } var arr1 = that.data.dataList; //从data获取当前dataList数组 var arr2 = res.rows; //从此次请求返回的数据中获取新数组 arr1 = arr1.concat(arr2); //合并数组 that.setData({ dataList: arr1, count: res.count, //总条数 hidden: true }) }) }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { const userInfo = wx.getStorageSync('userInfo'); if (!userInfo || !userInfo.id) { wx.reLaunch({ url: '/pages/login/login' }); } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { const userInfo = wx.getStorageSync('userInfo'); if (!userInfo || !userInfo.id) { wx.reLaunch({ url: '/pages/login/login' }); } else { this.getProjectList(); } }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { let that = this; that.setData({ dataList: [], keyName: '', page: 0, //当前页 count: '', //总条数 }) that.getProjectList() // 手动控制回弹 wx.stopPullDownRefresh(); }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { let _that = this; let page = _that.data.page + 1; //获取当前页数并+1 let { dataList, count } = _that.data; if (dataList.length == count) { showToast('没有更多数据了...'); return; } _that.setData({ page: page, //更新当前页数 }) _that.getProjectList() }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })