micro_mall_xcx/pages/home/home.js
2023-10-23 16:13:53 +08:00

216 lines
4.0 KiB
JavaScript

import {
fetchHome
} from '~/services/home/home';
import {
fetchGoodsList
} from '~/services/good/fetchGoodsList';
import {
getbannerList
} from '~/services/home/getbannerList';
import {
getHomeCategoryListApi
} from '~/services/home/getHomeCategoryListApi';
import {
getNoticeList
} from '~/services/home/getNoticeList';
import Toast from 'tdesign-miniprogram/toast/index';
Page({
data: {
imgSrcs: [],
tabList: [],
noticeList: [],
marquee: {
speed: 40,
loop: -1,
delay: 0,
},
goodsList: [],
goodsListLoadStatus: 0,
pageLoading: false,
current: 0,
autoplay: true,
duration: '500',
interval: 5000,
navigation: {
type: 'dots',
},
swiperImageProps: {
mode: 'scaleToFill',
},
categoryGuid: 0,
// 加入购物车
buyType: 0,
outOperateStatus: false, // 是否外层加入购物车
isSpuSelectPopupShow: false,
},
goodListPagination: {
index: 1,
num: 6,
},
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,
});
// 获取轮播图列表
getbannerList().then((res) => {
if (res.code == 200) {
this.setData({
imgSrcs: res.data.map((v) => v.bannerImg),
});
}
});
// 获取首页推荐类目
getHomeCategoryListApi().then((res) => {
this.setData({
tabList: res.data,
});
this.data.tabList.unshift({
text: "精选推荐",
key: 0,
categoryGuid: 1,
})
this.setData({
tabList: res.data,
pageLoading: false,
});
this.loadGoodsList(true);
});
// 获取公告列表
getNoticeList().then((res) => {
if (res.code == 200) {
const _noticeData = res.data.map((v) => {
return {
...v,
visible: true
};
});
this.setData({
noticeList: _noticeData,
});
}
});
},
tabChangeHandle(e) {
this.privateData.tabIndex = e.detail;
this.setData({
categoryGuid: e.detail.value,
});
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.goodListPagination.index + 1;
if (fresh) {
pageIndex = 1;
}
let params = {
pageNum: pageIndex,
pageSize: pageSize,
goodsCategoryGuid: this.data.categoryGuid
}
console.log(params, '看看参数');
try {
const nextList = await fetchGoodsList(params);
this.setData({
goodsList: fresh ? nextList.result : this.data.goodsList.concat(nextList.result),
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(type) {
console.log(type);
this.setData({
buyType: 0,
outOperateStatus: 1,
isSpuSelectPopupShow: true,
});
},
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}`,
});
},
});