index.vue
8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<!--
* @Date: 2024-09-27 16:53:09
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-11-06 15:52:49
* @FilePath: /hager/src/views/product/index.vue
* @Description: 文件描述
-->
<template>
<div class="product-index">
<hager-box>
<div v-if="!is_search" style="margin-top: 1.5rem;">
<el-breadcrumb v-if="!is_all_cate" separator="/">
<el-breadcrumb-item :to="{ path: allPath }">所有产品</el-breadcrumb-item>
<el-breadcrumb-item v-if="parent_name" :to="{ path: productPath }">{{ parent_name }}</el-breadcrumb-item>
<el-breadcrumb-item>{{ category_name }}</el-breadcrumb-item>
</el-breadcrumb>
<el-breadcrumb v-else separator="/">
<el-breadcrumb-item>所有产品</el-breadcrumb-item>
</el-breadcrumb>
</div>
<hager-h1 v-if="!is_search" :title="category_name" :sub="category_name_en" style="margin: 2rem 0;"></hager-h1>
<hager-h1 v-else :title="'与“' + keyword + '”相关的搜索结果'" :sub="''" style="margin: 2rem 0;"></hager-h1>
<el-row v-if="!is_xs" :gutter="0" style="margin: 1rem 0;">
<el-col :span="6">
<div class="product-nav-wrapper">
<div class="product-nav-title">按产品类别查找</div>
<el-input style="margin-bottom: 0.5rem;" placeholder="请输入产品" prefix-icon="el-icon-search" v-model="search_keyword" @change="goToSearch"></el-input>
<el-collapse v-if="!is_all_cate" v-model="activeNames" @change="handleChange">
<el-collapse-item v-for="(item, index) in cate_list" :key="index" :title="item.category_name" :name="item.category_name">
<div @click="goToDetail(c)" v-for="(c, idx) in item.list" :key="idx" class="p-item">{{ c.product_name }}</div>
</el-collapse-item>
</el-collapse>
<Accordion v-else :items="all_cate_list" />
</div>
</el-col>
<el-col :span="18">
<div class="product-list">
<div class="product-item" v-for="(item, index) in product_list" :key="index">
<div class="product-item-img">
<img style="width: 100%; height: auto;" :src="item.cover">
</div>
<p @click="goToDetail(item)" class="product-item-title">{{ item.product_name }}</p>
</div>
</div>
<div style="height: 5rem;"></div>
</el-col>
</el-row>
<div v-else class="product-list">
<div class="product-item xs" v-for="(item, index) in product_list" :key="index">
<div @click="goToDetail(item)" class="product-item-img xs">
<img style="width: 35vw; height: auto;" :src="item.cover">
</div>
<p @click="goToDetail(item)" class="product-item-title">{{ item.product_name }}</p>
</div>
</div>
<div style="height: 5rem;"></div>
</hager-box>
</div>
</template>
<script>
import mixin from 'common/mixin';
import hagerBox from '@/components/common/hagerBox';
import hagerH1 from '@/components/common/hagerH1.vue';
import Accordion from '@/components/accordion/Accordion.vue';
import { getProductCateAPI, getProductSearchAPI } from "@/api/hager.js";
export default {
components: { hagerBox, hagerH1, Accordion },
mixins: [mixin.init],
data () {
return {
search_keyword: '',
keyword: '',
activeNames: [],
cate_id: '',
parent_name: '',
category_name: '所有产品',
category_name_en: 'All Products',
cate_list: [],
product_list: [],
no_product_img: 'https://cdn.ipadbiz.cn/hager/img/no-product-img.png',
is_search: false,
all_cate_list: [],
}
},
async mounted () {
if (!this.is_all_cate) {
this.getMain();
} else { // 没有ID显示所有产品
this.getAllMain();
}
},
computed: {
allPath () {
return `/product/index?timestamp=${Date.now()}`;
},
productPath () {
return `/product/index?id=${this.cate_id}×tamp=${Date.now()}`;
},
is_all_cate () {
return this.$route.query.id === undefined;
}
},
watch: {
// 监听路由参数变化时,更新输入框的值
'$route.query.id' (val, old) {
if (val) {
if (old !== val) {
this.is_search = false;
this.getMain();
}
} else {
// 所有产品
this.getAllMain();
}
},
'$route.query.timestamp' (val, old) {
this.is_search = false;
if (!this.is_all_cate) {
this.getMain();
} else {
// 所有产品
this.getAllMain();
}
},
},
methods: {
handleChange(val) {
},
goToDetail (v) { // 跳转产品详情
this.$router.push({
path: '/product/detail',
query: {
id: v.id,
category_id: this.$route.query.id
}
});
},
async getAllMain () { // 查询所有产品列表数据
this.getCurrentCate();
const { code, data } = await getProductCateAPI({ cid: 0 });
if (code) {
let info = data[0];
this.category_name = '所有产品';
this.category_name_en = 'All Products';
this.product_list = info.list;
this.search_keyword = '';
// 设置页面标题
document.title = '所有产品 | Hager China';
}
},
async getMain () {
const { code, data } = await getProductCateAPI({cid: this.$route.query.id });
if (code) {
let info = data[0];
this.parent_name = info.parent_name;
this.category_name = info.category_name;
this.category_name_en = info.category_name_en;
this.product_list = info.list;
this.search_keyword = '';
// 获取当前类别的内容
this.cate_id = info.category_parent ? info.category_parent : info.id; // 同一个值,在不同层级下名字不一样
this.getCurrentCate(this.cate_id);
// 设置页面标题
document.title = this.category_name + ' | Hager China';
}
},
async getCurrentCate (cate_id) {
const { code, data } = await getProductCateAPI();
if (code) {
if (cate_id) {
data.forEach((item) => {
if (item.id === cate_id) {
this.cate_list = item.children;
}
});
} else {
// 所有分类列表
this.all_cate_list = data;
// 重构数据结构配合组件
this.all_cate_list.forEach((item) => {
item.children.forEach((c) => {
c.children = c.list;
c.style = {fontWeight: 'normal', height: '2.5rem'}
c.children.forEach((d) => {
d.category_name = d.product_name
})
})
})
}
}
},
async goToSearch () {
if (this.search_keyword) { // 产品有值时操作
const { code, data } = await getProductSearchAPI({ keyword: this.search_keyword });
if (code) {
this.product_list = data;
this.is_search = true;
this.keyword = this.search_keyword;
}
} else { // 清空搜索值,还原
if (!this.is_all_cate) {
this.getMain();
} else {
// 所有产品
this.getAllMain();
}
this.is_search = false;
this.keyword = '';
}
},
}
}
</script>
<style lang="less">
.product-index {
.product-nav-wrapper {
border: 1px solid #eee;
border-radius: 5px;
padding: 1.5rem;
margin-right: 1rem;
.product-nav-title {
color: @secondary-color;
font-weight: bold;
font-size: 1.15rem;
margin-bottom: 0.5rem;
}
}
.product-list {
display: flex;
flex-wrap: wrap;
}
.product-item {
width: calc(33.33% - 1rem);
margin-bottom: 1rem;
margin-right: 1rem;
&.xs {
width: calc(50% - 1rem);
}
}
.product-item-img {
background-color: #fff;
display: flex;
align-items: center;
justify-content: center;
height: 18rem;
padding: 2vw;
box-sizing: border-box;
border-radius: 8px;
border: 1px solid #eee;
}
.product-item-title {
// text-align: center;
margin-top: 0.75rem;
color: @text-color;
font-weight: bold;
padding-left: 1rem;
&:hover {
cursor: pointer;
text-decoration: underline;
}
}
}
.el-collapse {
border-top: 0;
}
.el-collapse-item {
.el-collapse-item__header {
font-size: 0.9rem;
}
.p-item {
&:hover {
cursor: pointer;
text-decoration: underline;
}
}
}
</style>