class Index { constructor() { this.index = 0; this.doTogetherData = [ { type: 1, title: '推荐', content: [], page: 1 }, { type: 2, title: '时令', content: [], page: 1 }, { type: 3, title: '食肉', content: [], page: 1 }, { type: 4, title: '素食', content: [], page: 1 }, { type: 5, title: '烘焙', content: [], page: 1 } ]; this.getBannerList(); this.renderMenu(); this.getThreeMeals(); this.renderDoTogether(); this.DoTogether(); this.scrollLoadData(); } /** * 获取轮播图列表 */ async getBannerList() { var banner = $(".banner_swiper .swiper-wrapper"); let res = await indexService.bannerIndex({ id: 1 }); res.forEach((val,index) => { banner.append(`
`) }); this.initBannerSwiper(); } // 初始化banner轮播图 initBannerSwiper() { var mySwiper = new Swiper('.banner_swiper', { direction: 'horizontal', loop: true, // 如果需要分页器 pagination: { el: '.swiper-pagination', } }) } async getThreeMeals() { var mrscUl = $(".mrsc ul"); var mrscSwiper = $(".mrsc_swiper .swiper-wrapper"); let res = await indexService.threeMeals({}); console.log(res) res.forEach((val,index)=>{ mrscUl.append(`
  • ${val.title}
  • `) mrscSwiper.append( `
    ` ); val.content.forEach((v,i)=>{ $(".mrsc_swiper .swiper-wrapper .mrsc_list") .eq(index).append( `

    ${v.title}

    ${v.des}

    ` ); }) }); this.initMrsc() } initMrsc() { var MrscUl = $(".mrsc ul li"); var MrscSwiper = new Swiper('.mrsc_swiper', { direction: 'horizontal', on: { slideChangeTransitionEnd: function(){ console.log(this.activeIndex); MrscUl.eq(this.activeIndex).addClass("active").siblings().removeClass("active") }, }, }); MrscUl.each(function(i,e) { $(this).on("click", function() { MrscSwiper.slideTo(i) }); }); } async renderMenu() { let res = await indexService.type({}); console.log(res) res.forEach((v,i)=>{ $(".menu").append(`
  • ${v.title}

  • `) }) } /** * 渲染内容的 * @returns {Promise} * @constructor */ async DoTogether() { let res = await indexService.indexClass({ type: this.doTogetherData[this.index].type, page: this.doTogetherData[this.index].page }); this.doTogetherData[this.index].content = [ ...this.doTogetherData[this.index].content, ...res.items ] $(".do_together_content .roll_father").eq(this.index).empty() // 渲染列表的 this.doTogetherData[this.index].content.forEach((v,i)=>{ $(".do_together_content .roll_father").eq(this.index).append(`
    ${ typeof v.img == 'string' ? `` : `` }
    ${v.title}
    ${ v.label == null || v.label.length == 0 ? '' : `

    ${v.label.desc}

    ` }
    ${ v.author.hasOwnProperty("avatar_url") ? `
    ${v.author.nickname}
    ${v.viewed_amount}
    ` : '' }
    `) }) // console.log("RenderDoTogether: ", res); } /** * 渲染标题 */ renderDoTogether() { this.doTogetherData.forEach((v,i)=>{ $(".do_together_title").append(`
  • ${v.title}
  • `); $(".do_together_content").append(`
    `) }); var titleLi = $(".do_together_title li"), RollFather = $(".do_together_content .roll_father"); var a = this; titleLi.on('click', function () { a.index = $(this).index(); $(this).addClass("active").siblings().removeClass("active") if (a.doTogetherData[a.index].content.length == 0) { a.DoTogether() } RollFather.eq($(this).index()).addClass("showRoll").siblings().removeClass("showRoll") }) } scrollLoadData() { var scroll = new auiScroll({ listen:true, distance: 10 //判断到达底部的距离,isToBottom为true }, (ret) => { if(ret.isToBottom){ console.log("滚动到底部") this.doTogetherData[this.index].page += 1; this.DoTogether() } }); } } new Index()