crawler-MiniProgram/新闻小程序/pages/home/home.js

234 lines
6.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

let currentIndex = 0;
Page({
data: {
//存放轮播图数据的列表
lunboList: [],
msgList: [
{ title: "上海今日全市进行抗原检测 明日全市进行核酸检测" },
{ title: "苏州发现奥密克戎新变异株 与全球已知毒株均不同源" },
{ title: "美工作组抵华 参与东航事故调查" },
{ title: "上海昨日新增本土确诊425例、无症状8581例" },
],
topMenuList: ["内地新闻", "香港新闻", "两岸新闻", "国际新闻", "军事新闻"],
news_list: [],
selectIndex: 0,
},
onLoad: function (options) {
// 获取轮播图数据
wx.cloud.database().collection('homepage').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('nd-news')
.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('xg-news')
.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('tw-news')
.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('gj-news')
.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('junshi-news')
.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)
})
}
},
handleItemTap(e) {
// 1获取被点击的标题身上的索引
// 2给data中的currentIndex赋值就可以了
// 3根据不同的索引来渲染右侧的商品内容
this.setData({
news_list: []
})
const {index} = e.currentTarget.dataset;
this.getList(index)
currentIndex = index
this.setData({
selectIndex: index
})
},
// 点击事件-去往详情页
gotoDetail(event) {
// console.log('用户点击的新闻id', event.currentTarget.dataset.id)
wx.navigateTo({
url: '/pages/news-detail/news-detail?id=' + event.currentTarget.dataset.id + '&currentIndex=' + currentIndex,
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
this.getList(currentIndex)
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})