You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.6 KiB

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