纸通宝小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

203 lines
6.4 KiB

const request = require('../../../utils/request') //导入模块
const util = require('../../../utils/util')
const app = getApp()
// pages/moment/detail/index.js
Page({
/**
* 页面的初始数据
*/
data: {
id: null,
safeBottom: app.globalData.safeBottom,
height: app.globalData.safeFragmentHeight,
type: 0,
tabList: [
{id: 0, name: '评论', badge: 12},
{id: 1, name: '系统', badge: 0}
],
tabIndex: 0,
inputBottom: 0,
keyboardHeight: 0,
focus: false,
comment: '',
momentDetail: null,
commentList: [],
requesting: false,
finished: false,
form: {
pageNum: 1
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.data.keyboardHeight = wx.getStorageSync('keyboardHeight')
if(options.type){
this.data.type = Number(options.type)
}
this.setData({ type: this.data.type, safeBottom: app.globalData.safeBottom, height: app.globalData.safeFragmentHeight - 100 })
if(options.id){
this.data.id = options.id
this.data.form.articleId = options.id
wx.showLoading({ title: '加载中', mask: true })
// /article/get/Article-detail/{id}文章详情
request.get('/information-center/article/get/Article-detail/' + options.id).then(result => {
//成功回调
this.setData({ momentDetail: result.data })
this.fetchCommentList()
wx.hideLoading()
}).catch(err => {
//异常回调
wx.hideLoading()
util.showToast(err)
})
}
},
onRefreshList: function () {
this.setData({
commentList: [],
['form.pageNum']: 1,
finished: false
})
this.fetchCommentList()
},
fetchCommentList: function(){
if (this.data.requesting || this.data.finished) {
return
}
this.setData({ requesting: true })
// /article-reply/get/reply-list/in/{articleId} 帖子评论列表
request.get('/information-center/article-reply/get/reply-list/in/' + this.data.id, this.data.form).then(result => {
if (result.data.records.length) {
var respList = result.data.records
let nowList = `commentList[${this.data.commentList.length}]`
var num = this.data.form.pageNum
var finished = this.data.form.pageNum >= result.data.pages
if(num == 1){
this.setData({
commentList: [],
[nowList]: respList,
requesting: false,
['form.pageNum']: (num + 1),
['momentDetail.replyQuantity']: result.data.total,
finished
})
} else {
this.setData({
[nowList]: respList,
requesting: false,
['form.pageNum']: (num + 1),
['momentDetail.replyQuantity']: result.data.total,
finished
})
}
} else {
this.setData({ requesting: false, finished: true })
}
}).catch(err => {
//异常回调
this.setData({ requesting: false })
})
},
onTabChange: function (e) {
if (this.data.tabIndex == Number(e.detail.key)) {
return
}
this.data.tabIndex = Number(e.detail.key)
},
bindInput: function(e){
this.data.comment = e.detail.value
},
inputFocus: function(e) {
if(this.data.keyboardHeight){
return
}
this.data.keyboardHeight = Number(e.detail.height)
wx.setStorageSync('keyboardHeight', this.data.keyboardHeight)
this.setData({
inputBottom: e.detail.height
})
},
inputBlur: function(e) {
this.setData({
inputBottom: 0, focus: false
})
},
showComment: function(e) {
if(this.data.keyboardHeight){
this.setData({ focus: true,inputBottom: this.data.keyboardHeight })
} else {
this.setData({ focus: true })
}
},
submitComment: function(){
if(util.isEmpty(this.data.comment)){
util.showToast('请输入评论内容')
return
}
wx.showLoading({ title: '处理中', mask: true })
var form = { content: this.data.comment, articleId: this.data.id}
// /article-reply/reply 评论
request.post('/information-center/article-reply/reply', form).then(result => {
this.setData({ inputBottom: 0, focus: false, comment: '' })
wx.hideLoading()
util.showToast('评论成功')
this.onRefreshList()
}).catch(error => {
wx.hideLoading()
util.showToast(error)
})
},
likeComment: function(e){
var item = this.data.commentList[e.currentTarget.dataset.page][e.currentTarget.dataset.index]
if(item.isLiked){
util.showToast('请勿重复点赞')
return
}
// /article-reply/like-or-cancel/reply 点赞评论
request.post('/information-center/article-reply/like-or-cancel/reply', {replyId: item.id}).then(result => {
if(item.isLiked){
this.setData({
['commentList[' + e.currentTarget.dataset.page + '][' + e.currentTarget.dataset.index + '].likeQuantity']: result.data.likeQuantity,
['commentList[' + e.currentTarget.dataset.page + '][' + e.currentTarget.dataset.index + '].isLiked']: 0
})
} else {
this.setData({
['commentList[' + e.currentTarget.dataset.page + '][' + e.currentTarget.dataset.index + '].likeQuantity']: result.data.likeQuantity,
['commentList[' + e.currentTarget.dataset.page + '][' + e.currentTarget.dataset.index + '].isLiked']: 1
})
}
}).catch(error => {
util.showToast(error)
})
},
likePost: function(){
if(this.data.momentDetail.isLiked){
util.showToast('请勿重复点赞')
return
}
request.post('/information-center/article-reply/like-or-cancel/article', {articleId: this.data.id}).then(result => {
this.setData({
['momentDetail.likeQuantity']: result.data.likeQuantity,
['momentDetail.isLiked']: 1
})
}).catch(error => {
util.showToast(error)
})
},
toMill: function(){
wx.navigateTo({ url: '/pages/agent/factory/index?id=' + this.data.momentDetail.millPaperId })
},
onShareAppMessage: function () {
var imageUrl = '/assets/image/icon_logo.png'
if(this.data.momentDetail.imgUrlList && this.data.momentDetail.imgUrlList.length > 0){
imageUrl = this.data.momentDetail.imgUrlList[0]
}
return {
title: this.data.momentDetail.title,
path: 'pages/login/index?url=/pages/article/detail/index&key=id&value=' + this.data.momentDetail.id,
imageUrl: imageUrl
}
}
})