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

108 lines
2.7 KiB
JavaScript

// pages/news-detail/news-detail.js
let newsType = ''
let newsid = ''
let collect = false
Page({
data: {
},
onLoad: function (options) {
console.log('从主页传递来的id:', options.id)
console.log('从主页传递来的currentIndex:', options.currentIndex)
newsid = options.id
// newsType = options.currentIndex
let currentIndex = options.currentIndex
if (currentIndex == 0) {
newsType = 'lqxy'
} else if (currentIndex == 1) {
newsType = 'qcxy'
} else if (currentIndex == 2) {
newsType = 'hyxy'
} else if (currentIndex == 3) {
newsType = 'hkxy'
} else if (currentIndex == 4) {
newsType = 'gdxy'
} else if (currentIndex == 5) {
newsType = 'zhxy'
} else if (currentIndex == 6) {
newsType = 'ysxy'
} else if (currentIndex == 7) {
newsType = 'rwxy'
} else if (currentIndex == 9) {
newsType = 'lunbotu-news'
}
// 查询新闻详情数据,按不同板块
wx.cloud.database().collection(newsType).doc(options.id)
.get()
.then(res => {
console.log('新闻详情页请求成功', res)
collect = res.data.collect
this.setData({
detail: res.data,
news_image: res.data.news_img,
collect_img: collect ? "../../images/collect-yes.png" : "../../images/collect-no.png"
})
})
},
// 收藏按钮
collect() {
this.setData({
collect_img: collect ? "../../images/collect-no.png" : "../../images/collect-yes.png"
})
collect = !collect
wx.cloud.database().collection(newsType).doc(newsid)
.update({
data: {
collect: collect
}
})
.then(res => {
console.log('collect状态改变成功')
if (collect) {
wx.cloud.database().collection('news_collect')
.add({
data: {
_id: newsid,
type: newsType,
news_img: this.data.detail.news_img,
news_title: this.data.detail.news_title,
news_author: this.data.detail.news_author,
news_date: this.data.detail.news_date,
}
})
.then(res => {
console.log('添加新闻到收藏页成功', res)
})
}
else if (!collect) {
wx.cloud.database().collection('news_collect')
.doc(newsid).remove()
.then(res => {
console.log('已取消收藏', res)
})
}
})
},
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
},
onReachBottom: function () {
},
onShareAppMessage: function () {
}
})