261 行
5.4 KiB
Vue
261 行
5.4 KiB
Vue
<template>
|
||
<!-- 分类导航:统一蓝色圆形图标 -->
|
||
<view class="wrap" v-if="displayList.length">
|
||
<view class="category-grid" :class="{ 'category-grid--scroll': needScroll, 'category-grid--colorful': colorful }">
|
||
<scroll-view v-if="needScroll" class="category-scroll" scroll-x :show-scrollbar="false" enable-flex>
|
||
<view class="category-row">
|
||
<view
|
||
class="category-item u-flex-col u-col-center u-row-center"
|
||
v-for="(item, index) in displayList"
|
||
:key="index"
|
||
@tap="$tools.routerTo(item.path)"
|
||
>
|
||
<view
|
||
class="category-icon-wrap u-flex u-row-center u-col-center"
|
||
:class="colorful ? 'category-icon-wrap--colorful category-icon-wrap--c' + item.colorIndex : ''"
|
||
>
|
||
<text class="u-iconfont" :class="item.iconClass"></text>
|
||
</view>
|
||
<text class="category-name u-ellipsis-1">{{ item.name }}</text>
|
||
</view>
|
||
</view>
|
||
</scroll-view>
|
||
<view v-else class="category-row category-row--even">
|
||
<view
|
||
class="category-item category-item--even u-flex-col u-col-center u-row-center"
|
||
v-for="(item, index) in displayList"
|
||
:key="index"
|
||
@tap="$tools.routerTo(item.path)"
|
||
>
|
||
<view
|
||
class="category-icon-wrap u-flex u-row-center u-col-center"
|
||
:class="colorful ? 'category-icon-wrap--colorful category-icon-wrap--c' + item.colorIndex : ''"
|
||
>
|
||
<text class="u-iconfont" :class="item.iconClass"></text>
|
||
</view>
|
||
<text class="category-name u-ellipsis-1">{{ item.name }}</text>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
</template>
|
||
|
||
<script>
|
||
const CATEGORY_ICON_MAP = {
|
||
'雄戈系列': 'uicon-gift-fill',
|
||
'白酒': 'uicon-bag-fill',
|
||
'礼盒装': 'uicon-gift',
|
||
'单瓶装': 'uicon-bag',
|
||
'品鉴装': 'uicon-coupon-fill',
|
||
'热销': 'uicon-star-fill',
|
||
'热销推荐': 'uicon-star-fill',
|
||
'新品上市': 'uicon-tags-fill',
|
||
'商务宴请': 'uicon-calendar-fill',
|
||
'酱香型': 'uicon-map-fill',
|
||
'浓香型': 'uicon-map',
|
||
'中秋月饼': 'uicon-grid-fill',
|
||
'复购专区': 'uicon-reload',
|
||
'兑换专区': 'uicon-coupon-fill',
|
||
'平台商城': 'uicon-shopping-cart-fill'
|
||
};
|
||
|
||
function resolveCategoryIcon(name) {
|
||
if (name && CATEGORY_ICON_MAP[name]) {
|
||
return CATEGORY_ICON_MAP[name];
|
||
}
|
||
return 'uicon-grid';
|
||
}
|
||
|
||
function withCategoryIcon(list) {
|
||
return (list || []).map(item => ({
|
||
...item,
|
||
iconClass: resolveCategoryIcon(item.name)
|
||
}));
|
||
}
|
||
|
||
export default {
|
||
data() {
|
||
return {
|
||
fallbackList: []
|
||
};
|
||
},
|
||
props: {
|
||
list: {
|
||
type: Array,
|
||
default: () => []
|
||
},
|
||
oneRowNum: {
|
||
type: [Number, String],
|
||
default: 5
|
||
},
|
||
colorful: {
|
||
type: Boolean,
|
||
default: false
|
||
}
|
||
},
|
||
computed: {
|
||
displayList() {
|
||
const source = this.list && this.list.length ? this.list : this.fallbackList;
|
||
return withCategoryIcon(source).map((item, index) => ({
|
||
...item,
|
||
colorIndex: index % 5
|
||
}));
|
||
},
|
||
needScroll() {
|
||
const max = Number(this.oneRowNum) || 5;
|
||
return this.displayList.length > max;
|
||
}
|
||
},
|
||
created() {
|
||
if (!this.list || !this.list.length) {
|
||
this.loadCategoryMenu();
|
||
}
|
||
},
|
||
methods: {
|
||
loadCategoryMenu() {
|
||
this.$http('category.info').then(res => {
|
||
if (res.code !== 1 || !res.data || !res.data.id) {
|
||
return;
|
||
}
|
||
return this.$http('category.detail', { id: res.data.id });
|
||
}).then(res => {
|
||
if (res && res.code === 1 && res.data && res.data.children && res.data.children.length) {
|
||
this.fallbackList = res.data.children.map(item => ({
|
||
name: item.name,
|
||
image: item.image,
|
||
path: `/pages/goods/list?id=${item.id}`,
|
||
path_name: item.name,
|
||
path_type: 1
|
||
}));
|
||
}
|
||
});
|
||
}
|
||
}
|
||
};
|
||
</script>
|
||
|
||
<style lang="scss">
|
||
.wrap {
|
||
margin: 8rpx $brand-page-gutter 16rpx;
|
||
}
|
||
|
||
.category-grid {
|
||
background: #ffffff;
|
||
border-radius: 20rpx;
|
||
box-shadow: $brand-shadow;
|
||
overflow: hidden;
|
||
}
|
||
|
||
.category-grid--colorful {
|
||
background: transparent;
|
||
box-shadow: none;
|
||
}
|
||
|
||
.category-scroll {
|
||
width: 100%;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.category-row {
|
||
display: inline-flex;
|
||
flex-direction: row;
|
||
padding: 28rpx 8rpx 24rpx;
|
||
}
|
||
|
||
.category-row--even {
|
||
display: flex;
|
||
width: 100%;
|
||
flex-direction: row;
|
||
justify-content: space-around;
|
||
padding: 16rpx 12rpx 8rpx;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
.category-grid--colorful .category-row,
|
||
.category-grid--colorful .category-row--even {
|
||
padding-top: 16rpx;
|
||
padding-bottom: 8rpx;
|
||
}
|
||
|
||
.category-item {
|
||
flex-shrink: 0;
|
||
width: 140rpx;
|
||
padding: 0 4rpx;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
.category-item--even {
|
||
flex: 1;
|
||
width: auto;
|
||
min-width: 0;
|
||
max-width: 150rpx;
|
||
}
|
||
|
||
.category-icon-wrap {
|
||
width: 96rpx;
|
||
height: 96rpx;
|
||
border-radius: 50%;
|
||
background: $brand-primary-light;
|
||
margin-bottom: 12rpx;
|
||
box-shadow: inset 0 0 0 1rpx rgba(22, 119, 255, 0.08);
|
||
|
||
.u-iconfont {
|
||
font-size: 44rpx;
|
||
color: $brand-primary;
|
||
}
|
||
}
|
||
|
||
.category-icon-wrap--colorful {
|
||
box-shadow: none;
|
||
}
|
||
|
||
.category-icon-wrap--c0 {
|
||
background: $home-icon-bg-green;
|
||
|
||
.u-iconfont {
|
||
color: $home-icon-color-green;
|
||
}
|
||
}
|
||
|
||
.category-icon-wrap--c1 {
|
||
background: $home-icon-bg-orange;
|
||
|
||
.u-iconfont {
|
||
color: $home-icon-color-orange;
|
||
}
|
||
}
|
||
|
||
.category-icon-wrap--c2 {
|
||
background: $home-icon-bg-blue;
|
||
|
||
.u-iconfont {
|
||
color: $home-icon-color-blue;
|
||
}
|
||
}
|
||
|
||
.category-icon-wrap--c3 {
|
||
background: $home-icon-bg-purple;
|
||
|
||
.u-iconfont {
|
||
color: $home-icon-color-purple;
|
||
}
|
||
}
|
||
|
||
.category-icon-wrap--c4 {
|
||
background: $home-icon-bg-gold;
|
||
|
||
.u-iconfont {
|
||
color: $home-icon-color-gold;
|
||
}
|
||
}
|
||
|
||
.category-name {
|
||
width: 100%;
|
||
font-size: 24rpx;
|
||
font-weight: 500;
|
||
color: $brand-text;
|
||
text-align: center;
|
||
line-height: 1.3;
|
||
}
|
||
</style>
|