295 lines
6.5 KiB
JavaScript
295 lines
6.5 KiB
JavaScript
|
// pages/news/newsDetail.js
|
||
|
const app = getApp()
|
||
|
Page({
|
||
|
|
||
|
/**
|
||
|
* 页面的初始数据
|
||
|
*/
|
||
|
data: {
|
||
|
token: app.globalData.token,
|
||
|
sourceUrl: app.baseUrls.sourceUrl,
|
||
|
newsId: '',
|
||
|
newsDetail: {},
|
||
|
reply: '',
|
||
|
userIcon: '',
|
||
|
userName: '',
|
||
|
replyList: [],
|
||
|
isCollect: 'no',
|
||
|
isGetFocus: false,
|
||
|
placeholder: '写评论',
|
||
|
replyTo: '',
|
||
|
replyUserName: '',
|
||
|
isLoading: false
|
||
|
},
|
||
|
// 获取新闻详情
|
||
|
getNewsDetail: function () {
|
||
|
var self = this
|
||
|
app.restAjax.get(app.restAjax.path(app.apis.getNewsDetail, [app.baseUrls.requestUrl, self.data.newsId]), {}, {
|
||
|
headers: {
|
||
|
token: self.data.token
|
||
|
}
|
||
|
}, function (code, data) {
|
||
|
self.setData({
|
||
|
newsDetail: data
|
||
|
})
|
||
|
}, function (code, data) {
|
||
|
console.log(data)
|
||
|
})
|
||
|
},
|
||
|
// 发布评论
|
||
|
doReplyNews: function () {
|
||
|
var self = this
|
||
|
if (!self.data.isLoading) {
|
||
|
if (!self.data.reply) {
|
||
|
wx.showToast({
|
||
|
title: '评论不能为空',
|
||
|
icon: 'error'
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
var info = {
|
||
|
content: self.data.reply,
|
||
|
headPortrait: self.data.userIcon,
|
||
|
userName: self.data.userName,
|
||
|
newsId: self.data.newsId
|
||
|
}
|
||
|
self.setData({
|
||
|
isLoading: true
|
||
|
})
|
||
|
app.restAjax.post(app.restAjax.path(app.apis.submitNewsReply, [app.baseUrls.requestUrl]), info, {
|
||
|
headers: {
|
||
|
token: self.data.token
|
||
|
}
|
||
|
}, function (code, data) {
|
||
|
if (code == '200') {
|
||
|
self.getReplyList()
|
||
|
self.setData({
|
||
|
reply: '',
|
||
|
isLoading: false
|
||
|
})
|
||
|
}
|
||
|
}, function (code, data) {
|
||
|
console.log(data)
|
||
|
self.setData({
|
||
|
isLoading: false
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
},
|
||
|
// 获取评论列表
|
||
|
getReplyList: function () {
|
||
|
var self = this
|
||
|
app.restAjax.get(app.restAjax.path(app.apis.getNewsReplyList, [app.baseUrls.requestUrl]), {
|
||
|
newsId: self.data.newsId
|
||
|
}, {
|
||
|
headers: {
|
||
|
token: self.data.token
|
||
|
}
|
||
|
}, function (code, data) {
|
||
|
self.setData({
|
||
|
replyList: data
|
||
|
})
|
||
|
}, function (code, data) {
|
||
|
console.log(data)
|
||
|
})
|
||
|
},
|
||
|
// 获取我是否收藏此新闻
|
||
|
getIsCollect: function () {
|
||
|
var self = this
|
||
|
app.restAjax.get(app.restAjax.path(app.apis.isCollectNews, [app.baseUrls.requestUrl, self.data.newsId]), {}, {
|
||
|
headers: {
|
||
|
token: self.data.token
|
||
|
}
|
||
|
}, function (code, data) {
|
||
|
self.setData({
|
||
|
isCollect: data
|
||
|
})
|
||
|
}, function (code, data) {
|
||
|
console.log(data)
|
||
|
})
|
||
|
},
|
||
|
// 收藏/取消收藏
|
||
|
toggleCollect: function () {
|
||
|
if (this.data.isCollect == 'no') {
|
||
|
this.collectNews()
|
||
|
} else {
|
||
|
this.cancelCollect()
|
||
|
}
|
||
|
},
|
||
|
// 收藏
|
||
|
collectNews: function () {
|
||
|
var self = this
|
||
|
app.restAjax.post(app.restAjax.path(app.apis.doCollectNews, [app.baseUrls.requestUrl]), {
|
||
|
newsId: self.data.newsId
|
||
|
}, {
|
||
|
headers: {
|
||
|
token: self.data.token
|
||
|
}
|
||
|
}, function (code, data) {
|
||
|
if (code == '200') {
|
||
|
wx.showToast({
|
||
|
title: '收藏成功!',
|
||
|
icon: 'success'
|
||
|
})
|
||
|
self.getIsCollect()
|
||
|
}
|
||
|
}, function (code, data) {
|
||
|
console.log(data)
|
||
|
})
|
||
|
},
|
||
|
// 取消收藏
|
||
|
cancelCollect: function () {
|
||
|
var self = this
|
||
|
app.restAjax.delete(app.restAjax.path(app.apis.cancelCollectNews, [app.baseUrls.requestUrl, self.data.newsId]), {}, {
|
||
|
headers: {
|
||
|
token: self.data.token
|
||
|
}
|
||
|
}, function (code, data) {
|
||
|
if (code == '200') {
|
||
|
wx.showToast({
|
||
|
title: '已取消收藏!',
|
||
|
icon: 'success'
|
||
|
})
|
||
|
self.getIsCollect()
|
||
|
}
|
||
|
}, function (code, data) {
|
||
|
console.log(data)
|
||
|
})
|
||
|
},
|
||
|
// 输入评论
|
||
|
getFocus: function (e) {
|
||
|
var user = e.currentTarget.dataset.user
|
||
|
var replyTo = e.currentTarget.dataset.id
|
||
|
var name = e.currentTarget.dataset.name
|
||
|
this.setData({
|
||
|
isGetFocus: true,
|
||
|
placeholder: '回复' + user + ':',
|
||
|
replyTo: replyTo,
|
||
|
replyUserName: name
|
||
|
})
|
||
|
},
|
||
|
// 输入框失去焦点判断文字
|
||
|
inputBlur: function () {
|
||
|
if (!this.data.reply) {
|
||
|
this.setData({
|
||
|
placeholder: '讨论一下',
|
||
|
isGetFocus: false
|
||
|
})
|
||
|
}
|
||
|
},
|
||
|
// 确认回复
|
||
|
doReply: function () {
|
||
|
if (!this.data.replyTo) {
|
||
|
this.doReplyNews()
|
||
|
} else {
|
||
|
this.doReplyReply()
|
||
|
}
|
||
|
},
|
||
|
// 回复
|
||
|
doReplyReply: function () {
|
||
|
var self = this
|
||
|
if (!self.data.isLoading) {
|
||
|
var info = {
|
||
|
content: self.data.reply,
|
||
|
headPortrait: self.data.userIcon,
|
||
|
userName: self.data.userName,
|
||
|
// trifleMyselfId: self.data.trifleMyselfId,
|
||
|
newsId: self.data.newsId,
|
||
|
commentId: self.data.replyTo,
|
||
|
replyCommentId: self.data.replyTo,
|
||
|
replyNewsCommentId: self.data.replyTo,
|
||
|
replyUserName: self.data.replyUserName
|
||
|
}
|
||
|
self.setData({
|
||
|
isLoading: true
|
||
|
})
|
||
|
app.restAjax.post(app.restAjax.path(app.apis.submitNewsReply, [app.baseUrls.requestUrl]), info, {
|
||
|
headers: {
|
||
|
token: self.data.token
|
||
|
}
|
||
|
}, function (code, data) {
|
||
|
if (code == '200') {
|
||
|
wx.showToast({
|
||
|
title: '评论成功!',
|
||
|
icon: 'success'
|
||
|
})
|
||
|
self.getReplyList()
|
||
|
self.setData({
|
||
|
reply: '',
|
||
|
replyTo: '',
|
||
|
replyUserName: '',
|
||
|
placeholder: '讨论一下',
|
||
|
isLoading: false
|
||
|
})
|
||
|
}
|
||
|
}, function (code, data) {
|
||
|
console.log(data)
|
||
|
self.setData({
|
||
|
isLoading: false
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
},
|
||
|
/**
|
||
|
* 生命周期函数--监听页面加载
|
||
|
*/
|
||
|
onLoad: function (options) {
|
||
|
this.setData({
|
||
|
newsId: options.newsId,
|
||
|
userIcon: wx.getStorageSync('userIcon'),
|
||
|
userName: wx.getStorageSync('name')
|
||
|
})
|
||
|
this.getNewsDetail()
|
||
|
this.getReplyList()
|
||
|
this.getIsCollect()
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* 生命周期函数--监听页面初次渲染完成
|
||
|
*/
|
||
|
onReady: function () {
|
||
|
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* 生命周期函数--监听页面显示
|
||
|
*/
|
||
|
onShow: function () {
|
||
|
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* 生命周期函数--监听页面隐藏
|
||
|
*/
|
||
|
onHide: function () {
|
||
|
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* 生命周期函数--监听页面卸载
|
||
|
*/
|
||
|
onUnload: function () {
|
||
|
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* 页面相关事件处理函数--监听用户下拉动作
|
||
|
*/
|
||
|
onPullDownRefresh: function () {
|
||
|
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* 页面上拉触底事件的处理函数
|
||
|
*/
|
||
|
onReachBottom: function () {
|
||
|
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* 用户点击右上角分享
|
||
|
*/
|
||
|
onShareAppMessage: function () {
|
||
|
|
||
|
}
|
||
|
})
|