分类页面预览图:
分类页面主要代码
index.js
import {
request
} from "../../request/index.js"
Page({
data
: {
leftMenuList
: [],
rightContent
: [],
currentIndex
: 0,
scrollTop
:0,
},
Cates
: [],
onLoad
: function (options
) {
const Cates
= wx
.getStorageSync("cates");
if (!Cates
) {
this.getCates();
} else {
if (Date
.now() - Cates
.time
> 1000 * 10) {
this.getCates();
} else {
this.Cates
= Cates
.data
;
let leftMenuList
= this.Cates
.map(v
=> v
.cat_name
);
let rightContent
= this.Cates
[0].children
;
this.setData({
leftMenuList
,
rightContent
,
})
}
}
},
getCates() {
request({
url
: "/categories"
}).then(res
=> {
this.Cates
= res
.data
.message
;
wx
.setStorageSync('cates', {
time
: Date
.now(),
data
: this.Cates
});
let leftMenuList
= this.Cates
.map(v
=> v
.cat_name
);
let rightContent
= this.Cates
[0].children
;
this.setData({
leftMenuList
,
rightContent
,
})
})
},
handleItemTap(e
) {
const {
index
} = e
.currentTarget
.dataset
;
let rightContent
= this.Cates
[index
].children
;
this.setData({
currentIndex
: index
,
rightContent
,
scrollTop
:0,
});
},
onReady
: function () {
},
onShow
: function () {
},
onHide
: function () {
},
onUnload
: function () {
},
onPullDownRefresh
: function () {
},
onReachBottom
: function () {
},
onShareAppMessage
: function () {
}
})
index.json
{
"usingComponents": {
"SearchInput":"../../components/SearchInput/SearchInput"
},
"navigationBarTitleText": "商品分类"
}
index.less
vscode的easyless插件会自动生成index.wxss
page {
height: 100%
;
}
.cates {
height: 100%
;
.cates_container {
// less中使用calc注意
height: ~
'calc(100vh - 90rpx)';
display: flex
;
.left_menu {
flex: 2
;
.menu_item {
height: 80rpx
;
display: flex
;
justify-content: center
;
align-items: center
;
font-size: 30rpx
;
}
.active {
color: var(--themeColor
);
border-left: 5rpx solid currentColor
;
}
}
.right_content {
flex: 5
;
.good_group {
.good_title {
height: 80rpx
;
display: flex
;
justify-content: center
;
align-items: center
;
.delimiter {
color: #cccccc
;
padding: 0 10rpx
;
}
.title {}
}
.good_list {
display: flex
;
flex-wrap: wrap
;
navigator {
width: 33.33%
;
text-align: center
;
image {
width: 50%
;
}
.goods_name {}
}
}
}
}
}
}
index.wxml
<view class="cates">
<SearchInput></SearchInput>
<view class="cates_container">
<scroll-view scroll-y="{{true}}" class="left_menu">
<view class="menu_item {{index===currentIndex?'active':''}}"
wx:for="{{leftMenuList}}"
wx:key="*this"
bindtap="handleItemTap"
data-index="{{index}}"
>
{{item}}
</view>
</scroll-view>
<scroll-view scroll-top="{{scrollTop}}" scroll-y="{{true}}" class="right_content">
<view class="good_group"
wx:for="{{rightContent}}"
wx:for-index="index1"
wx:for-item="item1"
>
<view class="good_title">
<text class="delimiter">/
</text>
<text class="title">{{item1.cat_name}}
</text>
<text class="delimiter">/
</text>
</view>
<view class="good_list">
<navigator class="" target="" url="/pages/goods_list/index?cid={{item2.cat_id}}" hover-class="navigator-hover" open-type="navigate"
wx:for="{{item1.children}}"
wx:for-index="index2"
wx:for-item="item2"
wx:key="cat_id"
>
<image class="" src="{{item2.cat_icon}}" mode="widthFix" lazy-load="false" binderror="" bindload="" />
<view class="goods_name">{{item2.cat_name}}
</view>
</navigator>
</view>
</view>
</scroll-view>
</view>
</view>
分类页面难点记录
主要是布局文件index.less的编写,注意less语法。
商品列表页面预览
商品列表页功能:支持上拉加载更多,下拉刷新等
商品列表页主要代码:
index.js
import {
request
} from "../../request/index.js"
Page({
data
: {
tabs
: [{
id
: 0,
value
: "综合",
isActive
: true,
},
{
id
: 1,
value
: "销量",
isActive
: false,
},
{
id
: 0,
value
: "价格",
isActive
: false,
},
],
goodsList
: [],
},
QueryParams
: {
query
: "",
cid
: "",
pagenum
: 1,
pagesize
: 10
},
totalPages
: 1,
onLoad
: function (options
) {
this.QueryParams
.cid
= options
.cid
;
this.getGoodsList();
},
getGoodsList() {
request({
url
: "/goods/search",
data
: this.QueryParams
,
}).then(res
=> {
const total
= res
.data
.message
.total
;
this.totalPages
= Math
.ceil(total
/this.QueryParams
.pagesize
);
this.setData({
goodsList
:[...this.data
.goodsList
,... res
.data
.message
.goods
],
});
wx
.stopPullDownRefresh();
})
},
handleTabsItemChange(e
) {
const {
index
} = e
.detail
;
let {
tabs
} = this.data
;
tabs
.forEach((v
, i
) => i
=== index
? v
.isActive
= true : v
.isActive
= false);
this.setData({
tabs
});
},
onReady
: function () {
},
onShow
: function () {
},
onHide
: function () {
},
onUnload
: function () {
},
onPullDownRefresh
: function () {
this.setData({
goodsList
:[],
});
this.QueryParams
.pagenum
=1;
this.getGoodsList();
},
onReachBottom
: function () {
if(this.QueryParams
.pagenum
>=this.totalPages
){
wx
.showToast({
title
: '没有更多数据了',
});
}else{
this.QueryParams
.pagenum
++;
this.getGoodsList();
}
},
onShareAppMessage
: function () {
}
})
index.json
{
"usingComponents": {
"SearchInput":"../../components/SearchInput/SearchInput",
"Tabs":"../../components/Tabs/Tabs"
},
"navigationBarTitleText": "商品列表页",
"enablePullDownRefresh": true,
"backgroundTextStyle": "dark"
}
index.less
.first_tab {
.goods_item {
display: flex
;
border-bottom: 1px solid #cccccc
;
.goods_img_wrap {
flex: 2
;
display: flex
;
justify-content: center
;
align-items: center
;
image {
width: 70%
;
}
}
.goods_info_wrap {
flex: 3
;
display: flex
;
flex-direction: column
;
justify-content: space-around
;
.goods_name {
display: -webkit-box
;
overflow: hidden
;
-webkit-box-orient: vertical
;
-webkit-line-clamp: 2
;
}
.goods_price {
color: var(--themeColor
);
font-size: 32rpx
;
}
}
}
}
index.wxml
<SearchInput></SearchInput>
<Tabs tabs="{{tabs}}" bindtabsItemChange="handleTabsItemChange">
<block wx:if="{{tabs[0].isActive}}">
<view class="first_tab">
<navigator class="goods_item"
wx:for="{{goodsList}}"
wx:key="goods_id"
>
<view class="goods_img_wrap">
<image class="" src="{{item.goods_small_logo}}" mode="widthFix" lazy-load="false" binderror="" bindload="" />
</view>
<view class="goods_info_wrap">
<view class="goods_name">{{item.goods_name}}
</view>
<view class="goods_price">¥{{item.goods_price}}
</view>
</view>
</navigator>
</view>
</block>
<block wx:elif="{{tabs[1].isActive}}">1
</block>
<block wx:elif="{{tabs[2].isActive}}">2
</block>
</Tabs>
商品列表页引用的组件Tabs主要代码
Tabs.js
Component({
properties
: {
tabs
:{
type
:Array
,
value
:[],
}
},
data
: {
},
methods
: {
handleItemTap(e
){
const {index
} = e
.currentTarget
.dataset
;
this.triggerEvent("tabsItemChange",{index
})
},
}
})
Tabs.less
.tabs{
.tabs_title{
display: flex
;
.title_item{
display: flex
;
padding: 15rpx 0
;
justify-content: center
;
align-items: center
;
flex: 1
;
}
}
.tabs_content{}
}
.active{
color: var(--themeColor
);
border-bottom: 5rpx solid currentColor
;
}
Tabs.wxml
<view class="tabs">
<view class="tabs_title">
<view class="title_item {{item.isActive?'active':''}}" wx:for="{{tabs}}" wx:key="id" bindtap="handleItemTap" data-index="{{index}}">
{{item.value}}
</view>
</view>
<view class="tabs_content">
<slot></slot>
</view>
</view>
商品列表页及Tabs组件主要技术点记录
1,父组件(商品列表页)和子组件(Tabs组件)相互传递数据问题 2,上拉加载更多、下拉刷新实现的逻辑