// pages/news-detail/news-detail.js let newsType = '' let newsid = '' //4.27 let collect = false //4.27 Page({ data: { }, onLoad: function (options) { // console.log('从主页传递来的id:', options.id) // console.log('从主页传递来的currentIndex:', options.currentIndex) newsid = options.id //4.27 newsType = options.currentIndex let currentIndex = options.currentIndex if (currentIndex == 0) { newsType = 'nd-news' } else if (currentIndex == 1) { newsType = 'xg-news' } else if (currentIndex == 2) { newsType = 'tw-news' } else if (currentIndex == 3) { newsType = 'gj-news' } else if (currentIndex == 4) { newsType = 'junshi-news' } else if (currentIndex == 5) { newsType = 'lunbotu-news' } // 查询新闻详情数据,按不同板块 wx.cloud.database().collection(newsType).doc(options.id) .get() .then(res => { console.log('新闻详情页请求成功', res) collect = res.data.collect //4.27 this.setData({ detail: res.data, news_image: res.data.news_img, collect_img: collect ? "../../images/collect-yes.png" : "../../images/collect-no.png" //4.27 }) }) }, // 收藏按钮 4.27 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 () { } })