let currentIndex = 0; Page({ data: { //存放轮播图数据的列表 lunboList: [], msgList: [ { title: "“奋斗的青春最美丽”————省政府奖学金获得者沈卓玥" }, { title: "学校召开“双高计划”专业群中期验收绩效评价工作会议" }, { title: "习近平勉励广大青年:牢记党的教诲 立志民族复兴" }, { title: "同心抗疫 “职”面未来——智慧学院举办线上宣讲会" }, ], topMenuList: [ "路桥学院", "汽车学院", "海运学院", "航空学院", "轨道学院", "智慧学院", "运输学院", "人文学院", ], news_list: [], selectIndex: 0, }, onLoad: function (options) { // 获取轮播图数据 wx.cloud.database().collection('zjjt_lbt').get() .then(res => { // console.log('轮播图数据:', res) this.setData({ lunboList: res.data }) }) .catch(res => { console.log('获取失败', res) }) // 获取新闻列表数据 this.getList(0) }, // 获取新闻列表数据 getList(index) { if (index == 0) { wx.showLoading({ title: '加载中...', }) let len = this.data.news_list.length console.log('当前list长度', len) wx.cloud.database().collection('lqxy') .skip(len) .get() .then(res => { wx.hideLoading() console.log('新闻列表请求成功:', res) let datalist = res.data if (datalist.length <= 0) { wx.showToast({ title: '数据全部加载完毕', icon: 'none' }) } this.setData({ news_list: this.data.news_list.concat(res.data), }) }) .catch(res => { console.log('新闻列表请求失败', res) }) } else if (index == 1) { wx.showLoading({ title: '加载中...', }) let len = this.data.news_list.length console.log('当前list长度', len) wx.cloud.database().collection('qcxy') .skip(len) .get() .then(res => { wx.hideLoading() console.log('新闻列表请求成功:', res) let datalist = res.data if (datalist.length <= 0) { wx.showToast({ title: '数据全部加载完毕', icon: 'none' }) } this.setData({ news_list: this.data.news_list.concat(res.data), }) }) .catch(res => { console.log('新闻列表请求失败', res) }) } else if (index == 2) { wx.showLoading({ title: '加载中...', }) let len = this.data.news_list.length console.log('当前list长度', len) wx.cloud.database().collection('hyxy') .skip(len) .get() .then(res => { wx.hideLoading() console.log('新闻列表请求成功:', res) let datalist = res.data if (datalist.length <= 0) { wx.showToast({ title: '数据全部加载完毕', icon: 'none' }) } this.setData({ news_list: this.data.news_list.concat(res.data), }) }) .catch(res => { console.log('新闻列表请求失败', res) }) } else if (index == 3) { wx.showLoading({ title: '加载中...', }) let len = this.data.news_list.length console.log('当前list长度', len) wx.cloud.database().collection('hkxy') .skip(len) .get() .then(res => { wx.hideLoading() console.log('新闻列表请求成功:', res) let datalist = res.data if (datalist.length <= 0) { wx.showToast({ title: '数据全部加载完毕', icon: 'none' }) } this.setData({ news_list: this.data.news_list.concat(res.data), }) }) .catch(res => { console.log('新闻列表请求失败', res) }) } else if (index == 4) { wx.showLoading({ title: '加载中...', }) let len = this.data.news_list.length console.log('当前list长度', len) wx.cloud.database().collection('gdxy') .skip(len) .get() .then(res => { wx.hideLoading() console.log('新闻列表请求成功:', res) let datalist = res.data if (datalist.length <= 0) { wx.showToast({ title: '数据全部加载完毕', icon: 'none' }) } this.setData({ news_list: this.data.news_list.concat(res.data), }) }) .catch(res => { console.log('新闻列表请求失败', res) }) } else if (index == 5) { wx.showLoading({ title: '加载中...', }) let len = this.data.news_list.length console.log('当前list长度', len) wx.cloud.database().collection('zhxy') .skip(len) .get() .then(res => { wx.hideLoading() console.log('新闻列表请求成功:', res) let datalist = res.data if (datalist.length <= 0) { wx.showToast({ title: '数据全部加载完毕', icon: 'none' }) } this.setData({ news_list: this.data.news_list.concat(res.data), }) }) .catch(res => { console.log('新闻列表请求失败', res) }) } else if (index == 6) { wx.showLoading({ title: '加载中...', }) let len = this.data.news_list.length console.log('当前list长度', len) wx.cloud.database().collection('ysxy') .skip(len) .get() .then(res => { wx.hideLoading() console.log('新闻列表请求成功:', res) let datalist = res.data if (datalist.length <= 0) { wx.showToast({ title: '数据全部加载完毕', icon: 'none' }) } this.setData({ news_list: this.data.news_list.concat(res.data), }) }) .catch(res => { console.log('新闻列表请求失败', res) }) } else if (index == 7) { wx.showLoading({ title: '加载中...', }) let len = this.data.news_list.length console.log('当前list长度', len) wx.cloud.database().collection('rwxy') .skip(len) .get() .then(res => { wx.hideLoading() console.log('新闻列表请求成功:', res) let datalist = res.data if (datalist.length <= 0) { wx.showToast({ title: '数据全部加载完毕', icon: 'none' }) } this.setData({ news_list: this.data.news_list.concat(res.data), }) }) .catch(res => { console.log('新闻列表请求失败', res) }) } }, // 1获取被点击的标题身上的索引 // 2给data中的currentIndex赋值就可以了 // 3根据不同的索引来渲染底部的内容 handleItemTap(e) { this.setData({ news_list: [] }) const {index} = e.currentTarget.dataset; this.getList(index) currentIndex = index this.setData({ selectIndex: index }) }, // lunboDetail(event) { // wx.navigateTo({ // url: '/pages/news-detail/news-detail?id=' + event.currentTarget.dataset.id + '¤tIndex=' + 9, // }) // }, // 点击事件-去往详情页 gotoDetail(event) { wx.navigateTo({ url: '/pages/news-detail/news-detail?id=' + event.currentTarget.dataset.id + '¤tIndex=' + currentIndex, }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { this.getList(currentIndex) }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })