hookehuyr

新增产品中心首页

1 +<!--
2 + * @Date: 2024-09-27 16:53:09
3 + * @LastEditors: hookehuyr hookehuyr@gmail.com
4 + * @LastEditTime: 2024-12-18 09:47:59
5 + * @FilePath: /hager/src/views/product/all.vue
6 + * @Description: 文件描述
7 +-->
8 +<template>
9 + <div class="product-index">
10 + <hager-box v-if="!is_search">
11 + <div style="margin-top: 1.5rem;">
12 + <el-breadcrumb v-if="!is_all_cate" separator="/">
13 + <el-breadcrumb-item :to="{ path: allPath }">所有产品</el-breadcrumb-item>
14 + <el-breadcrumb-item v-if="parent_name" :to="{ path: productPath }">{{ parent_name }}</el-breadcrumb-item>
15 + <el-breadcrumb-item>{{ category_name }}</el-breadcrumb-item>
16 + </el-breadcrumb>
17 + <el-breadcrumb v-else separator="/">
18 + <el-breadcrumb-item>所有产品</el-breadcrumb-item>
19 + </el-breadcrumb>
20 + </div>
21 + </hager-box>
22 + <hager-box v-if="!is_xs && !is_search" :class="['top-img', is_xs ? 'xs' : '']" :style="{ backgroundImage: `url('https://cdn.ipadbiz.cn/hager/img/product/banner-l@2x.png'), url(${banner})`, marginTop: '1rem' }">
23 + <div class="banner-text-wrapper" style="margin-top: 3%;">
24 + <div class="text">
25 + <p class="title" style="margin-bottom: 0; color: white; font-size: 1.6vw; font-weight: bold;">{{ category_name }}</p>
26 + <p class="sub" style="color: white; font-size: 1.2vw; font-weight: bold;">{{ category_name_en }}</p>
27 + <div style="margin-top: 1.5rem; font-weight: normal; font-size: 0.9vw; color: white; width: 45%;">
28 + <p v-html="category_description"></p>
29 + </div>
30 + </div>
31 + </div>
32 + </hager-box>
33 + <hager-box>
34 + <div v-if="!is_xs">
35 + <hager-h1 v-if="is_search" :title="'与“' + keyword + '”相关的搜索结果'" :sub="''" style="margin: 2rem 0;"></hager-h1>
36 + </div>
37 + <div v-else>
38 + <hager-h1 v-if="!is_search" :title="category_name" :sub="category_name_en" style="margin: 2rem 0;"></hager-h1>
39 + <hager-h1 v-else :title="'与“' + keyword + '”相关的搜索结果'" :sub="''" style="margin: 2rem 0;"></hager-h1>
40 + </div>
41 + </hager-box>
42 + <hager-box>
43 + <el-row v-if="!is_xs" :gutter="0" style="margin: 1rem 0;">
44 + <el-col :span="6">
45 + <div class="product-nav-wrapper">
46 + <div class="product-nav-title">按产品类别查找</div>
47 + <el-input style="margin-bottom: 0.5rem;" placeholder="请输入产品" prefix-icon="el-icon-search" v-model="search_keyword" @change="goToSearch"></el-input>
48 + <Accordion v-if="!is_all_cate" :items="cate_list" />
49 + <Accordion v-else :items="all_cate_list" />
50 + </div>
51 + </el-col>
52 + <el-col :span="18">
53 + <div class="product-list">
54 + <div class="product-item" v-for="(item, index) in product_list" :key="index">
55 + <div class="product-item-img" @click="goToDetail(item)">
56 + <img style="width: 100%; height: auto;" :src="item.cover">
57 + </div>
58 + <p @click="goToDetail(item)" class="product-item-title">{{ item.product_name }}</p>
59 + </div>
60 + </div>
61 + <div style="height: 5rem;"></div>
62 + </el-col>
63 + </el-row>
64 + <div v-else class="product-list">
65 + <div class="product-item xs" v-for="(item, index) in product_list" :key="index">
66 + <div @click="goToDetail(item)" class="product-item-img xs">
67 + <img style="width: 35vw; height: auto;" :src="item.cover">
68 + </div>
69 + <p @click="goToDetail(item)" class="product-item-title">{{ item.product_name }}</p>
70 + </div>
71 + </div>
72 + <div style="height: 5rem;"></div>
73 + </hager-box>
74 + </div>
75 +</template>
76 +
77 +<script>
78 +import mixin from 'common/mixin';
79 +import hagerBox from '@/components/common/hagerBox';
80 +import hagerH1 from '@/components/common/hagerH1.vue';
81 +import Accordion from '@/components/accordion/Accordion.vue';
82 +import { getProductCateAPI, getProductSearchAPI } from "@/api/hager.js";
83 +import $ from 'jquery';
84 +
85 +export default {
86 + components: { hagerBox, hagerH1, Accordion },
87 + mixins: [mixin.init],
88 + data () {
89 + return {
90 + search_keyword: '',
91 + keyword: '',
92 + activeNames: [],
93 + cate_id: '',
94 + parent_name: '',
95 + category_name: '所有产品',
96 + category_name_en: 'All Products',
97 + category_description: '',
98 + banner: 'https://cdn.ipadbiz.cn/hager/img/4-b-02.png',
99 + cate_list: [],
100 + product_list: [],
101 + no_product_img: 'https://cdn.ipadbiz.cn/hager/img/no-product-img.png',
102 + is_search: false,
103 + all_cate_list: [],
104 + is_all_cate: true
105 + }
106 + },
107 + async mounted () {
108 + if (!this.is_all_cate) {
109 + this.getMain();
110 + } else { // 没有ID显示所有产品
111 + this.getAllMain();
112 + }
113 + window.addEventListener('resize', this.changeImgHeight);
114 + },
115 + beforeDestroy() {
116 + // 在组件销毁前移除监听器,防止内存泄漏
117 + window.removeEventListener('resize', this.handleHeightResize);
118 + },
119 + computed: {
120 + allPath () {
121 + return `/product`;
122 + },
123 + productPath () {
124 + return `/product/index/${this.cate_id}/${Date.now()}`;
125 + },
126 + },
127 + watch: {
128 + // 监听路由参数变化时,更新输入框的值
129 + '$route.params.id' (val, old) {
130 + if (val) {
131 + if (old !== val) {
132 + this.is_search = false;
133 + this.getMain();
134 + }
135 + } else {
136 + // 所有产品
137 + this.getAllMain();
138 + }
139 + },
140 + '$route.params.timestamp' (val, old) {
141 + this.is_search = false;
142 + if (!this.is_all_cate) {
143 + this.getMain();
144 + } else {
145 + // 所有产品
146 + this.getAllMain();
147 + }
148 + },
149 + },
150 + methods: {
151 + changeImgHeight () {
152 + this.$nextTick(() => {
153 + // 动态计算图片高度
154 + let img_width = $('.product-item-img img').outerWidth();
155 + $('.product-item-img img').css('height', img_width * 1 + 'px');
156 + let banner_width = $('.top-img').outerWidth();
157 + $('.top-img').css('height', banner_width * 0.12 + 'px');
158 + })
159 + },
160 + handleChange(val) {
161 + },
162 + goToDetail (v) { // 跳转产品详情
163 + this.$router.push({
164 + path: `/product/detail/${v.id}/${this.$route.params.id}/${Date.now()}`
165 + });
166 + },
167 + async getAllMain () { // 查询所有产品列表数据
168 + this.getCurrentCate();
169 + const { code, data } = await getProductCateAPI({ cid: 0 });
170 + if (code) {
171 + let info = data[0];
172 + this.category_name = '所有产品';
173 + this.category_name_en = 'All Products';
174 + // this.category_description = '<p style="margin-bottom: 0.5rem;">海格为客户提供安全稳定的电气产品与智能化解决方案。</p><p>Hager provides safe and stable electrical products and intelligent solutions for customers.</p>';
175 + this.category_description = '<p style="margin-bottom: 0.5rem; font-weight: bold;">海格为客户提供安全稳定的电气产品与智能化解决方案。</p>';
176 + this.banner = 'https://cdn.ipadbiz.cn/hager/img/4-b-02.png';
177 + this.product_list = info.list;
178 + this.search_keyword = '';
179 + // 设置页面标题
180 + document.title = '所有产品 | Hager China';
181 + // 动态计算图片高度
182 + this.changeImgHeight();
183 + }
184 + },
185 + async getMain () {
186 + const { code, data } = await getProductCateAPI({cid: this.$route.params.id });
187 + if (code) {
188 + let info = data[0];
189 + this.parent_name = info.parent_name;
190 + this.category_name = info.category_name? info.category_name : '所有产品';
191 + this.category_name_en = info.category_name_en ? info.category_name_en : 'All Products';
192 + this.category_description = info.category_description ? info.category_description : '<p style="margin-bottom: 0.5rem; font-weight: bold;">海格为客户提供安全稳定的电气产品与智能化解决方案。</p>';
193 + this.banner = info.banner ? info.banner : 'https://cdn.ipadbiz.cn/hager/img/4-b-02.png';
194 + this.product_list = info.list;
195 +
196 + this.search_keyword = '';
197 + // 获取当前类别的内容
198 + this.cate_id = info.category_parent ? info.category_parent : info.id; // 同一个值,在不同层级下名字不一样
199 + this.getCurrentCate(this.cate_id);
200 + // 设置页面标题
201 + document.title = this.category_name + ' | Hager China';
202 + // 动态计算图片高度
203 + this.changeImgHeight();
204 + }
205 + },
206 + async getCurrentCate (cate_id) {
207 + const { code, data } = await getProductCateAPI();
208 + if (code) {
209 + if (cate_id) {
210 + data.forEach((item) => {
211 + if (item.id === cate_id) {
212 + this.cate_list = item.children;
213 + // 重构数据结构配合组件
214 + this.cate_list.forEach((c) => {
215 + c.children = c.list;
216 + c.style = {fontWeight: 'normal', height: '2.5rem'}
217 + c.children.forEach((d) => {
218 + d.category_name = d.product_name
219 + })
220 + })
221 + }
222 + });
223 + } else {
224 + // 所有分类列表
225 + this.all_cate_list = data;
226 + // 重构数据结构配合组件
227 + this.all_cate_list.forEach((item) => {
228 + item.children.forEach((c) => {
229 + c.children = c.list;
230 + c.style = {fontWeight: 'normal', height: '2.5rem'}
231 + c.children.forEach((d) => {
232 + d.category_name = d.product_name
233 + })
234 + })
235 + })
236 + }
237 + }
238 + },
239 + async goToSearch () {
240 + if (this.search_keyword) { // 产品有值时操作
241 + const { code, data } = await getProductSearchAPI({ keyword: this.search_keyword });
242 + if (code) {
243 + this.product_list = data;
244 + this.is_search = true;
245 + this.keyword = this.search_keyword;
246 + }
247 + } else { // 清空搜索值,还原
248 + if (!this.is_all_cate) {
249 + this.getMain();
250 + } else {
251 + // 所有产品
252 + this.getAllMain();
253 + }
254 + this.is_search = false;
255 + this.keyword = '';
256 + }
257 + },
258 + }
259 +}
260 +</script>
261 +
262 +<style lang="less">
263 + .product-index {
264 + .product-nav-wrapper {
265 + border: 1px solid #eee;
266 + border-radius: 5px;
267 + padding: 1.5rem;
268 + margin-right: 1rem;
269 + .product-nav-title {
270 + color: @secondary-color;
271 + font-weight: bold;
272 + font-size: 1.15rem;
273 + margin-bottom: 0.5rem;
274 + }
275 + }
276 +
277 + .top-img {
278 + background-size: 50% 115%; /* 每张背景图各占 50% 的宽度,100% 的高度 */
279 + background-position: left, right; /* 第一张图在左侧,第二张图在右侧 */
280 + background-repeat: no-repeat; /* 避免背景图重复 */
281 + position: relative;
282 + width: 100%;
283 + height: 25rem; /* 或根据需要指定高度 */
284 + &.xs {
285 + height: 12rem;
286 + }
287 + .top-center {
288 + position: absolute;
289 + transform: translateY(50%); /* 垂直与水平居中 */
290 + }
291 + }
292 +
293 + .product-list {
294 + display: flex;
295 + flex-wrap: wrap;
296 + }
297 + .product-item {
298 + width: calc(33.33% - 1rem);
299 + margin-bottom: 1rem;
300 + margin-right: 1rem;
301 + &.xs {
302 + width: calc(50% - 1rem);
303 + }
304 + }
305 + .product-item-img {
306 + background-color: #fff;
307 + display: flex;
308 + align-items: center;
309 + justify-content: center;
310 + height: 18rem;
311 + padding: 2vw;
312 + box-sizing: border-box;
313 + border-radius: 8px;
314 + border: 1px solid #eee;
315 + overflow: hidden;
316 + &:hover {
317 + cursor: pointer;
318 + }
319 + }
320 + .product-item-title {
321 + // text-align: center;
322 + margin-top: 0.75rem;
323 + color: @text-color;
324 + font-weight: bold;
325 + padding-left: 1rem;
326 + &:hover {
327 + cursor: pointer;
328 + text-decoration: underline;
329 + }
330 + }
331 + }
332 +
333 + .el-collapse {
334 + border-top: 0;
335 + }
336 + .el-collapse-item {
337 + .el-collapse-item__header {
338 + font-size: 0.9rem;
339 + }
340 + .p-item {
341 + &:hover {
342 + cursor: pointer;
343 + text-decoration: underline;
344 + }
345 + }
346 + }
347 +</style>