纸通宝小程序
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.
 

111 lines
3.5 KiB

// pages/stock/index.js
const request = require('../../../utils/request') //导入模块
const event = require('../../../utils/event.js')
const util = require('../../../utils/util')
const app = getApp()
Component({
options: {
addGlobalClass: true,
multipleSlots: true
},
/**
* 页面的初始数据
*/
data: {
height: app.globalData.safeFragmentHeight - 190,
vip: false,
momentList: [],
requesting: false,
finished: false,
form: {
categoryId: 3,
pageNum: 1
}
},
lifetimes: {
// 生命周期函数,可以为函数,或一个在methods段中定义的方法名
attached: function () {
this.setData({ height: app.globalData.safeFragmentHeight - 190, vip: app.globalData.userInfo.isVIP })
event.on('EventMessage', this, this.onEvent)
},
detached: function () {
event.remove('EventMessage', this)
}
},
methods: {
onRestart: function () {
if (!this.data.firstShow || this.data.momentList.length <= 0) {
this.onRefreshList()
}
this.data.firstShow = true
},
onEvent: function (message) {
if (message.what == 573) {
// 帖子发布刷新
this.onRefreshList()
} else if(message.what == 200){
// Vip的变化
this.setData({ vip: app.globalData.userInfo.isVIP })
this.onRefreshList()
}
},
//*************************************************momentList************************************************//
onRefreshList: function () {
if(!this.data.vip){
this.setData({ requesting: false })
return
}
this.setData({ momentList: [], ['form.pageNum']: 1, finished: false })
this.fetchMomentList()
},
fetchMomentList: function(){
if (this.data.requesting || this.data.finished) {
return
}
this.data.requesting = true
request.get('/information-center/article/get/Article-list/', this.data.form).then(result => {
//成功回调
if (result.data.records.length) {
var respList = result.data.records
let nowList = `momentList[${this.data.momentList.length}]`
var num = this.data.form.pageNum
var finished = this.data.form.pageNum >= result.data.pages
this.setData({
[nowList]: respList,
['form.pageNum']: (num + 1),
finished,
requesting: false
})
} else {
this.setData({ requesting: false })
}
}).catch(err => {
//异常回调
this.setData({ requesting: false })
})
},
lookItem: function(e){
var item = this.data.momentList[e.currentTarget.dataset.page][e.currentTarget.dataset.index]
wx.navigateTo({ url: '/pages/article/detail/index?id=' + item.id })
},
likeItem: function(e){
var item = this.data.momentList[e.currentTarget.dataset.page][e.currentTarget.dataset.index]
if(item.isLiked){
util.showToast('请勿重复点赞')
return
}
request.post('/information-center/like-or-cancel/post', {postId: item.id}).then(result => {
this.setData({
['momentList[' + e.currentTarget.dataset.page + '][' + e.currentTarget.dataset.index + '].likeQuantity']: result.data.likeQuantity,
['momentList[' + e.currentTarget.dataset.page + '][' + e.currentTarget.dataset.index + '].isLiked']: 1
})
}).catch(error => {
util.showToast(error)
})
},
toVip: function(){
wx.navigateTo({ url: '/pages/ztbvip/index' })
}
}
})