🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ // pages/move/moremove/moremove.js import utils from "../../../utils/utils"; var http = utils.http; var star = utils.star; const app = getApp(); const douban = app.globalData.doubanUrl; Page({ /** * 页面的初始数据 */ data: { start: 0, // 定义movies的数据是否为空 isEmpty: true, huan: false }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { wx.showLoading({ title: '正在加载...', }); // console.log(options); var title = options.title; var type = options.type; var url = douban + type; var huan = wx.getStorageSync(type).huan; // 没有数据 缓存为空 类型不一致 if (this.data.isEmpty == true && huan == false || huan == undefined) { // http(url, this.handleData); } else { // 获取缓存 设置缓存 var movies = wx.getStorageSync(type).movies; var title = wx.getStorageSync(type).title; this.setData({ movies, title }) wx.hideLoading(); } /* 设置标题 */ wx.setNavigationBarTitle({ title }); this.setData({ type, }) }, handleData(res) { var title = res.data.title; var subjects = res.data.subjects; var moviess = []; var type = this.data.type subjects.forEach(ele => { var average = ele.rating.average; var stars = star(ele.rating.stars); var title = ele.title; var imgUrl = ele.images.small; var id = ele.id; var temp = { average, stars, title, imgUrl, id }; // console.log(temp) moviess.push(temp); }); // isEmpty定义movies是否为空 if (this.data.isEmpty) { this.setData({ movies: moviess, title, isEmpty: false, huan: true }); // 第一次加载了数据将数据设置在缓存 var huan = this.data.huan; var movies = this.data.movies; var title = this.data.title; wx.setStorageSync(type, { movies, title, huan }); } else { this.setData({ movies: this.data.movies.concat(moviess) }) var huan = this.data.huan; var movies = this.data.movies; var title = this.data.title; wx.setStorageSync(type, { movies, title, huan }); } wx.hideLoading(); }, onReachBottom: function () { var type = this.data.type; this.data.start += 20; var start = this.data.start; var url = `${douban}${type}?start=${start}&count=20`; console.log(url) http(url, this.handleData); //下拉触发加载 wx.showLoading({ title: "加载数据" }); }, moveDetails(res) { // console.log(res) var id = res.currentTarget.dataset.id; var title = res.currentTarget.dataset.title; // console.log(title) wx.navigateTo({ url: '/pages/move/movedetails/movedetails?id=' + id + "&title=" + title, }) } }) ~~~