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.
git/scr/miniprogram-2/pages/home/home.js

155 lines
2.7 KiB

import {
fetchHome
} from '../../services/home/home';
import {
fetchGoodsList
} from '../../services/good/fetchGoods';
import Toast from 'tdesign-miniprogram/toast/index';
const db=wx.cloud.database();
Page({
data: {
imgSrcs: [],
tabList: [],
goodsList: [],
goodsListLoadStatus: 0,
pageLoading: false,
current: 1,
autoplay: true,
duration: 500,
interval: 5000,
navigation: {
type: 'dots'
},
},
goodListPagination: {
index: 0,
num: 20,
},
privateData: {
tabIndex: 0,
},
onShow() {
this.getTabBar().init();
},
onLoad() {
this.init();
},
onReachBottom() {
if (this.data.goodsListLoadStatus === 0) {
this.loadGoodsList();
}
},
onPullDownRefresh() {
this.init();
},
init() {
this.loadHomePage();
},
loadHomePage() {
wx.stopPullDownRefresh();
this.setData({
pageLoading: true,
});
fetchHome().then(({
tabList
}) => {
this.setData({
tabList,
imgSrcs: swiper,
pageLoading: false,
});
this.loadGoodsList(true);
});
},
tabChangeHandle(e) {
this.privateData.tabIndex = e.detail;
this.loadGoodsList(true);
},
onReTry() {
this.loadGoodsList();
},
async loadGoodsList(fresh = false) {
if (fresh) {
wx.pageScrollTo({
scrollTop: 0,
});
}
this.setData({
goodsListLoadStatus: 1
});
const pageSize = this.goodListPagination.num;
let pageIndex =
this.privateData.tabIndex * pageSize + this.goodListPagination.index + 1;
if (fresh) {
pageIndex = 0;
}
try {
const nextList = await fetchGoodsList(pageIndex, pageSize);
this.setData({
goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
goodsListLoadStatus: 0,
});
this.goodListPagination.index = pageIndex;
this.goodListPagination.num = pageSize;
} catch (err) {
this.setData({
goodsListLoadStatus: 3
});
}
},
goodListClickHandle(e) {
const {
index
} = e.detail;
const {
spuId
} = this.data.goodsList[index];
wx.navigateTo({
url: `/pages/goods/details/index?spuId=${spuId}`,
});
},
goodListAddCartHandle() {
Toast({
context: this,
selector: '#t-toast',
message: '点击加入购物车',
});
},
navToSearchPage() {
wx.navigateTo({
url: '/pages/goods/search/index'
});
},
navToActivityDetail({
detail
}) {
const {
index: promotionID = 0
} = detail || {};
wx.navigateTo({
url: `/pages/promotion-detail/index?promotion_id=${promotionID}`,
});
},
});