//articleLists.js 文章列表
// 网络请求工具类
var networkUtil = require('../../utils/network_util.js')
var api = require('../../utils/apiFile.js')
// 事件监听
var event = require('../../utils/event.js')
// 获取应用实例
var app = getApp()
var page
// 是否有更多
var hasMore
var isRefresh
Page({
data: {
list: [],
id: 0,
category: '',
current_index: 0,
guideList: [{
id: 0,
introduction: "精准指导",
title: "健康指导"
},
{
id: 1,
introduction: "高危指导",
title: "高危指导"
}
]
},
titleClick: function(e) {
// console.log(e)
var self = this
var current_index = e.currentTarget.dataset.index
var categoryid = e.currentTarget.dataset.categoryid
this.setData({
category: categoryid,
current_index: current_index,
})
page = 1;
hasMore = true
if (current_index == 0) {
hasMore = false,
this.setData({
list: [],
})
this.requestData()
} else {
hasMore = false,
this.setData({
list: [],
})
this.requestGuideData()
}
},
onLoad: function(e) {
this.data.id = e.categoryId
this.data.category = e.category
console.log(e.categoryId)
// 数据初始化
page = 1
hasMore = true
isRefresh = false
wx.setNavigationBarTitle({
title: e.category,
success: function(res) {}
})
// this.setData({})
this.requestData()
// 监听事件
event.on('listLikeChanged', this, function(data) {
var artList = this.data.list
artList[parseInt(data.articleIdIndex)].likeCount++
this.setData({
list: artList
});
})
},
onUnload: function() {
// 移除监听
event.remove('listLikeChanged', this);
},
/*---------事件处理函数--------*/
bindViewTap: function() {
wx.navigateTo({
url: '../home/home'
})
},
// 加载更多
loadMore: function(e) {
if (!hasMore || isRefresh == true) {
return
}
page++
this.requestData()
},
// 下拉刷新回调接口
onPullDownRefresh: function() {
page = 1;
hasMore = true
// 网络请求,重新请求一遍数据
this.requestData();
},
myScroll: function(e) {
console.log("e:", e)
// console.log("aaaaaaaaaaaaaaaaaaaaaaaaa " + e.detail.scrollTop + " ==== " + e.detail.scrollHeight + " === " + wx.getSystemInfoSync().windowHeight)
// if (e.detail.scrollTop >= (e.detail.scrollHeight - wx.getSystemInfoSync().windowHeight - 3)) {
console.log("bbbbbbbb")
this.loadMore()
// }
},
/*---------自定义函数--------*/
requestGuideData(id) {
var self = this
isRefresh = true
hasMore = false
networkUtil.showLoading()
var param = {
patientId: app.globalData.currentId,
days: getApp().globalData.days
}
networkUtil._get(api.babyPrecise, param, function(res) {
console.log(res)
isRefresh = false
var arr = []
arr = arr.concat(res.data.data)
var temp = self.data.category
self.setData({
list: arr,
hasMore: hasMore,
category: temp
})
}, function(res) {
isRefresh = false
})
},
requestData(id) {
var self = this
isRefresh = true
networkUtil.showLoading()
var param = {
page: page,
limit: 15,
type: 2,
categoryId: self.data.id,
days: getApp().globalData.days
}
console.log(" categoryId ========= " + " ==== " + self.data.id)
if (!param.categoryId) {
param.categoryId = 4316
}
networkUtil._get(api.articleList, param, function(res) {
console.log(param)
console.log(res)
isRefresh = false
var arr = []
if (page > 0) { // 加载更多模式
arr = self.data.list
}
if (res.data.data.length < 15) {
hasMore = false
}
arr = arr.concat(res.data.data)
var temp = self.data.category
self.setData({
list: arr,
hasMore: hasMore,
category: temp
})
}, function(res) {
isRefresh = false
})
},
requestRisk() {
var self = this
isRefresh = true
networkUtil.showLoading()
var param = {
page: page,
limit: 15
}
networkUtil._get(api.riskGuideList, param, function(res) {
console.log(res)
isRefresh = false
var arr = []
if (page > 1) { // 加载更多模式
arr = self.data.list
}
if (res.data.list.length < 15) {
hasMore = false
}
arr = arr.concat(res.data.list)
var temp = self.data.category
self.setData({
list: arr,
hasMore: hasMore,
category: temp
})
}, function(res) {
isRefresh = false
})
},
// 文章点赞
articleLike(e) {
console.log(e)
var self = this
// 已经点赞
if (e.currentTarget.dataset.islike == 1) {
networkUtil.showErrorToast('您已经点过赞啦')
return;
}
networkUtil._post(api.articleLike, {
id: e.currentTarget.dataset.artid
}, function(res) {
var artList = self.data.list
artList[e.currentTarget.dataset.artindex].likeCount++
artList[e.currentTarget.dataset.artindex].isLike = 1
self.setData({
list: artList
});
}, function(res) {
console.log(res)
})
},
})