// 引入api模块中的getBanner和getGoods函数 const { getBanner, getGoods } = require("../../api/index.js") // 定义页面对象 Page({ // 页面数据 data: { value: "", // 搜索框的值,初始为空字符串 swiperOptions: { indicatorDots: true, // 是否显示面板指示点 autoplay: true, // 是否自动切换 interval: 3000, // 自动切换时间间隔,单位为毫秒 duration: 1000, // 滑动动画时长,单位为毫秒 swiperData: [] // 轮播图的数据源,初始为空数组 }, navData: [ // 导航栏数据 { text: "数码", // 文本内容 icon: "like", // 图标名称 color: "#ff0000" // 图标颜色 }, { text: "生鲜", icon: "star", color: "#ff0000" }, { text: "会员", icon: "fire", color: "#ff0000" }, { text: "优惠", icon: "gift", color: "#ff0000" }, { text: "充值", icon: "phone", color: "#ff0000" }, { text: "领券", icon: "gem", color: "#ff0000" }, { text: "外卖", icon: "gift-card", color: "#ff0000" }, { text: "美食", icon: "smile", color: "#ff0000" } ], page: 1, // 当前页码,初始为1 goodsData: [] // 商品数据,初始为空数组 }, // 页面加载时执行的函数 onLoad() { // 获取轮播图数据 getBanner().then(res => { this.setData({ indicatorDots: true, // 设置是否显示面板指示点 autoplay: true, // 设置是否自动切换 interval: 3000, // 设置自动切换时间间隔 duration: 1000, // 设置滑动动画时长 swiperData: res.data.data.result // 设置轮播图数据 }) }) // 加载第一页商品数据 this.http(this.data.page) }, // 获取商品数据的函数 http(page) { getGoods({ page }).then(res => { if (!res.data.msg) { // 如果没有错误消息,合并新数据到现有商品数据中 this.setData({ goodsData: this.data.goodsData.concat(res.data.data.result) // 累加新数据 }) } else { // 如果有错误消息,显示提示信息 wx.showToast({ title: res.data.msg, // 提示信息内容 icon: "success", // 提示图标类型 duration: 2000 // 提示显示时长,单位为毫秒 }) } }) }, // 页面滚动到底部时触发的函数 onReachBottom() { // 更新页码 this.setData({ page: this.data.page + 1 // 页码加1 }) // 加载下一页商品数据 this.http(this.data.page) }, /** * 点击搜索框获取焦点时触发的函数 */ clickSearch() { // 跳转到搜索页面 wx.navigateTo({ url: '/pages/search/search', }) } })