市北人才赋能平台 --小程序端
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.

149 lines
3.3 KiB

6 years ago
//index.js
import dayjs from '../../utils/dayjs/index.js'
import relativeTime from '../../utils/dayjs/relativeTime.js'
dayjs.extend(relativeTime);
import { HomeModel } from '../../models/home.js'
6 years ago
let homeModel = new HomeModel()
6 years ago
Page({
data: {
loading: true,
currPage: 1,
banners: [],
hots: [],
6 years ago
list: []
6 years ago
},
onLoad: function () {
homeModel.fetchAuthToken(res => {
console.log('Home获取token成功后再请求数据')
this.fetchHomeList()
this.fetchHomeBanner()
this.fetchHomeHot()
})
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
this.setData({
currPage: 1,
})
this.fetchHomeList()
this.fetchHomeBanner()
this.fetchHomeHot()
wx.vibrateShort({
success () {
console.log('震动')
}
})
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
const page = this.data.currPage + 1
this.setData({
currPage: page
})
this.fetchHomeList()
},
// 获取首页列表
fetchHomeList () {
let page = this.data.currPage
homeModel.getHomeList(page, res => {
this.setData({
loading: false
})
console.log('啦啦啦')
6 years ago
const datas = res.list
let tempDatas = []
datas.forEach(item => {
tempDatas.push({
id: item.id,
title: item.title,
showTop: item.isTop == 1 ? true : false,
time: dayjs(item.createTime).toNow(),
6 years ago
readed: item.isRead == 0 ? false : true
})
})
if (page == 1) {
this.setData({
list: tempDatas
})
} else {
if (tempDatas.length > 0) {
const list = [...this.data.list, ...tempDatas]
this.setData({
list: list
})
} else {
const page = this.data.currPage - 1
this.setData({
currPage: page
})
wx.showToast({
title: '已加载全部',
icon: 'none'
})
}
}
console.log(res)
wx.stopPullDownRefresh()
})
},
fetchHomeBanner () {
homeModel.getHomeBanner(res => {
console.log('Banner')
console.log(res)
6 years ago
const datas = res.list
let tempBanners = []
datas.forEach(item => {
tempBanners.push({
id: item.id,
6 years ago
image: item.titlePic,
title: item.title
})
})
this.setData({
banners: tempBanners
})
})
},
fetchHomeHot () {
homeModel.getHomeHot(res => {
console.log('Hot')
6 years ago
console.log(res)
6 years ago
const datas = res.list
let tempHots = []
datas.forEach(item => {
tempHots.push({
id: item.id,
title: item.title
})
})
this.setData({
hots: tempHots
})
6 years ago
})
6 years ago
},
// Banner/Hot/List事件
tapSwiperItem (e) {
6 years ago
console.log(e.detail)
6 years ago
this.gotoArticePage(e.detail.id)
6 years ago
},
tapFocusItem (e) {
6 years ago
console.log(e.detail)
6 years ago
this.gotoArticePage(e.detail.id)
},
clickListItem (e) {
console.log(e.detail)
6 years ago
const {id, index} = e.detail
this.gotoArticePage(id)
6 years ago
},
gotoArticePage (id) {
6 years ago
wx.navigateTo({
url: `/pages/article/index?id=${id}`,
})
6 years ago
}
6 years ago
})