//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' let homeModel = new HomeModel() Page({ data: { loading: true, currPage: 1, banners: [], hots: [], list: [] }, 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('啦啦啦') 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(), 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) const datas = res.list let tempBanners = [] datas.forEach(item => { tempBanners.push({ id: item.id, image: item.titlePic, title: item.title }) }) this.setData({ banners: tempBanners }) }) }, fetchHomeHot () { homeModel.getHomeHot(res => { console.log('Hot') console.log(res) const datas = res.list let tempHots = [] datas.forEach(item => { tempHots.push({ id: item.id, title: item.title }) }) this.setData({ hots: tempHots }) }) }, // Banner/Hot/List事件 tapSwiperItem (e) { console.log(e.detail) this.gotoArticePage(e.detail.id) }, tapFocusItem (e) { console.log(e.detail) this.gotoArticePage(e.detail.id) }, clickListItem (e) { console.log(e.detail) const {id, index} = e.detail this.gotoArticePage(id) }, gotoArticePage (id) { wx.navigateTo({ url: `/pages/article/index?id=${id}`, }) } })