// pages/topics/interactive/article/index.js import dayjs from '../../../../utils/dayjs/index.js' import relativeTime from '../../../../utils/dayjs/relativeTime.js' dayjs.extend(relativeTime); import { TopicModel } from '../../../../models/topic.js' let topicModel = new TopicModel() Page({ /** * 页面的初始数据 */ data: { title:String, time: String, groupAvator: String, author:String, company:String, position:String, detail: String, imgArr:[], comments:[], unStar: '/images/common/star.png', star: '/images/common/star_light.png', isStar:false, topicId:String, collectionId:String, isCollect:Number, currPage: 1, commentList: [], unPraise: '/images/common/zan.png', praise: '/images/common/star_light.png' }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { console.log(options.topicId) this.setData({ topicId: options.topicId }) this.fetchDetail() this.fetchDetailComment() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { const page = this.data.currPage + 1 this.setData({ currPage: page }) this.fetchDetailComment() }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, fetchDetail(){ let topicId = this.data.topicId topicModel.getTopicDetail(topicId,res => { console.log('议题详情') console.log(res) let data = res.result this.setData({ collectionId: data.collectionId, isCollect: data.isCollect, title:data.title, time: data.createTime, groupAvator: data.groupAvator, author: data.author, company: data.company, position: data.position, detail:data.content, imgArr: data.images.split(";").slice(0, -1) }) console.log(this.data.imgArr) }) }, fetchDetailComment(){ let topicId = this.data.topicId let page = this.data.currPage topicModel.getTopicDetailComment(topicId,page,res => { console.log('议题评论') console.log(res) const datas = res.result.list let tempDatas = [] datas.forEach(item => { tempDatas.push({ isPraise: false, id:item.id, userIcon: item.commentAvator, userName: item.username, detail: item.comment, time: item.createTime, praiseNum: item.supportNum }) }) if (page == 1) { this.setData({ commentList: tempDatas }) } else { if (tempDatas.length > 0) { const list = [...this.data.commentList, ...tempDatas] this.setData({ commentList: list }) } else { const page = this.data.currPage - 1 this.setData({ currPage: page }) wx.showToast({ title: '已加载全部', icon: 'none' }) } } console.log(this.data.commentList) wx.stopPullDownRefresh() }) }, fetchTopicCollect(){ let topicId = this.data.topicId topicModel.topicCollect(topicId,res =>{ console.log('收藏') console.log(res) if(res.code === 200){ this.fetchDetail() wx.showToast({ title: '收藏成功', icon: 'none' }) } }) }, fetchTopicUnCollect(){ let collectionId = this.data.collectionId topicModel.topicUnCollect(collectionId, res => { console.log('取消收藏') console.log(res) if (res.code === 200) { this.fetchDetail() wx.showToast({ title: '取消收藏', icon: 'none' }) } }) }, fetchTopicCommentPraise(commentId){ topicModel.topicCommentPraise(commentId,res =>{ console.log(res) if (res.code === 200) { wx.showToast({ title: res.message, icon: 'none' }) this.fetchDetailComment() } }) }, onClickCollect() { this.setData({ isCollect: !this.data.isCollect }) this.data.isCollect === true ? this.fetchTopicCollect() : this.fetchTopicUnCollect() // 收藏功能 }, onClickPraise(e){ console.log(e) const sIndex = e.currentTarget.dataset.index var selectItem = e.currentTarget.dataset.item const commentList = this.data.commentList const tempList = [] commentList.forEach( (item,index)=>{ if (sIndex === index){ item.isPraise = !item.isPraise } tempList.push(item) }) this.setData({ commentList:tempList, }) this.fetchTopicCommentPraise(selectItem.id) // 评论点赞 }, onTapComment(){ }, previewImg: function (e) { console.log(e.currentTarget.dataset.index); var index = e.currentTarget.dataset.index; var imgArr = this.data.imgArr; wx.previewImage({ current: imgArr[index], //当前图片地址 urls: imgArr, //所有要预览的图片的地址集合 数组形式 success: function (res) { }, fail: function (res) { }, complete: function (res) { }, }) } })