//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 () { console.log(dayjs().toNow()) console.log(dayjs().from(dayjs('1990'))) this.fetchHomeList() this.fetchHomeBanner() this.fetchHomeHot() }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { this.setData({ currPage: 1, }) this.fetchHomeList() 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 => { console.log(item) tempBanners.push({ id: item.id, image: 'http://m.360buyimg.com/mobilecms/s700x280_jfs/t1/45210/40/7669/143615/5d53b19dEc9559d0a/cd9eade239533517.jpg!cr_1125x445_0_171!q70.jpg.dpg' }) }) 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) // this.gotoArticePage(e.detail.id) wx.redirectTo({ url: '/pages/register/index', }) }, gotoArticePage(id) { wx.navigateTo({ url: `/pages/article/index?id=${id}`, }) } })