hagerHeader.vue
22.9 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
<!--
* @Date: 2024-09-26 13:42:11
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-12-16 15:20:41
* @FilePath: /hager/src/components/common/hagerHeader.vue
* @Description: 文件描述
-->
<template>
<div class="hager-header">
<el-row class="hidden-xs-only" style="background-color: #fff;">
<el-col :sm="1" :md="1" :lg="3" :xl="4"> </el-col>
<el-col :sm="22" :md="22" :lg="18" :xl="16" style="position: relative;height: 5rem;">
<el-row>
<el-col :sm="6" :md="6" :lg="6" :xl="6">
<div @click="goToHome" class="nav-logo">
<el-image style="width: 8rem; height: 2.5rem" src="https://cdn.ipadbiz.cn/hager/icon/logo@2x.png" fit="fit"></el-image>
</div>
</el-col>
<el-col :sm="10" :md="10" :lg="10" :xl="10">
<el-row class="nav-right">
<el-col :span="24">
<div class="search">
<i class="el-icon-search" style="color: #00477E;"></i>
<input placeholder="搜索" v-model="keyword" @blur="goToSearch" @keydown.enter="goToSearch" />
</div>
</el-col>
</el-row>
</el-col>
<el-col :sm="8" :md="8" :lg="8" :xl="8">
<div class="nav-right" style="float: right;">
<div @click="goToWeb" class="tooltip">
<!-- <el-tooltip class="item" effect="dark" content="国际站" placement="bottom"> -->
<!-- <i class=el-icon-orange style="font-size: 1.25rem;"></i> -->
<el-image style="width: 1.25rem; height: 1.25rem" src="https://cdn.ipadbiz.cn/hager/icon/%E8%AF%AD%E8%A8%80@2x.png" fit="fit"></el-image>
<!-- </el-tooltip> -->
</div>
<div @click="goToLogin" class="tooltip">
<!-- <el-tooltip class="item" effect="dark" content="用户登录" placement="bottom"> -->
<el-image v-if="!is_login" style="width: 1.25rem; height: 1.25rem" src="https://cdn.ipadbiz.cn/hager/icon/%E7%99%BB%E9%99%86@2x.png" fit="fit"></el-image>
<el-image v-else style="width: 1.25rem; height: 1.25rem" src="https://cdn.ipadbiz.cn/hager/icon/%E5%B7%B2%E7%99%BB%E5%BD%95@2x.png" fit="fit"></el-image>
<!-- <i class=el-icon-user style="font-size: 1.25rem;"></i> -->
<!-- </el-tooltip> -->
</div>
</div>
</el-col>
</el-row>
</el-col>
<el-col :sm="1" :md="1" :lg="3" :xl="4"> </el-col>
</el-row>
<el-row class="hidden-xs-only" :style="{backgroundColor: '#fff', borderTop: '1px solid rgba(218, 218, 218, 0.4)', borderBottom: show_border ? '1px solid rgba(218, 218, 218, 0.4)' : ''}">
<el-col :sm="1" :md="1" :lg="3" :xl="4"> </el-col>
<el-col :sm="22" :md="22" :lg="18" :xl="16" style="position: relative;height: 5rem;">
<el-row>
<!-- <el-col :sm="16" :md="16" :lg="16" :xl="16"> -->
<el-col :span="24">
<div class="nav-wrapper">
<div :class="[is_active === 'product' ? 'active' : '', 'item']" @click="onClickProduct">
<el-image style="width: auto; height: 0.65rem" src="https://cdn.ipadbiz.cn/hager/icon/%E8%8F%9C%E5%8D%95@2x.png" fit="fit"></el-image>
<span @mouseenter="onClickProduct">产品中心</span>
</div>
<!-- <div :class="[is_active === 'solution' ? 'active' : '', 'item']" @click="goTo('/solution/index')">解决方案</div>
<div :class="[is_active === 'news' ? 'active' : '', 'item']" @click="goTo('/news')">新闻中心</div>
<div :class="[is_active === 'recruit' ? 'active' : '', 'item']" @click="goTo('/recruit')">招聘信息</div>
<div :class="[is_active === 'about' ? 'active' : '', 'item']" @click="goTo('/about')">关于海格</div>
<div :class="[is_active === 'contact' ? 'active' : '', 'item']" @click="goTo('/contact')">联系我们</div>
<div :class="[is_active === 'index' ? 'active' : '', 'item']" @click="goTo('/')">首页</div> -->
<a :class="[is_active === 'solution' ? 'active' : '', 'item']" :href="`${baseUrl}/solution/index`" @click.prevent="goTo('/solution/index')">解决方案</a>
<a :class="[is_active === 'news' ? 'active' : '', 'item']" :href="`${baseUrl}/news`" @click.prevent="goTo('/news')">新闻中心</a>
<a :class="[is_active === 'recruit' ? 'active' : '', 'item']" :href="`${baseUrl}/recruit`" @click.prevent="goTo('/recruit')">招聘信息</a>
<a :class="[is_active === 'about' ? 'active' : '', 'item']" :href="`${baseUrl}/about`" @click.prevent="goTo('/about')">关于海格</a>
<a :class="[is_active === 'contact' ? 'active' : '', 'item']" :href="`${baseUrl}/contact`" @click.prevent="goTo('/contact')">联系我们</a>
<a :class="[is_active === 'index' ? 'active' : '', 'item']" :href="`${baseUrl}/`" @click.prevent="goTo('/')">首页</a>
</div>
</el-col>
</el-row>
<el-collapse-transition>
<div v-show="show" class="product-center" v-click-outside="closeMenu">
<div style="display: flex; width: 100%;border: 1px solid rgba(218, 218, 218, 0.4); border-top: 0;">
<div class="product-left-wrapper">
<div :class="['product-left-item', p_index === index ? 'active' : '']" v-for="(item, index) in product_menu" :key="index" @click="onClick(item, index)">
<div class="icon-text">
<img :src="item.icon" style="width: auto; height: 1.45rem; object-fit: cover;" />
{{ item.category_name }}
</div>
<div>
<i class="el-icon-arrow-right"></i>
</div>
</div>
</div>
<div class="product-right-wrapper">
<!-- <div class="product-right-title">
<div class="text">{{ product_info.category_name }}</div>
<div @click="closeMenu"><i class="el-icon-close" style="font-size: 1.25rem;"></i></div>
</div>
<div class="product-right-desc">{{ product_info.category_name_en }}</div> -->
<div class="closeBtn">
<div @click="closeMenu"><i class="el-icon-close" style="font-size: 1.25rem;"></i></div>
</div>
<div class="product-right-center">
<div v-for="(item, index) in product_info" :key="index" class="product-right-center-item">
<div @click="goToC(item)" class="header-text">{{ item.category_name }} </div>
<div>
<div class="item-text" @click="goToP(p)" v-for="(p, idx) in item.list" :key="idx">{{ p.product_name }}</div>
</div>
</div>
</div>
</div>
</div>
</div>
</el-collapse-transition>
</el-col>
<el-col :sm="1" :md="1" :lg="3" :xl="4"> </el-col>
</el-row>
<div class="mobile-menu-wrapper" v-if="is_xs">
<div class="mobile-menu">
<div @click="openMenu" style="width: 3rem;">
<el-image v-if="!show_menu" style="width: auto; height: 0.65rem" src="https://cdn.ipadbiz.cn/hager/icon/%E8%8F%9C%E5%8D%95@2x.png" fit="fit"></el-image>
<i v-else class="el-icon-close" style="font-size: 1.15rem;"></i>
</div>
<div @click="goToHome" style="display: flex; align-items: center;">
<img style="width: 5.3rem; height: 1.7rem" src="https://cdn.ipadbiz.cn/hager/icon/logo@2x.png">
</div>
<div>
<el-image @click="goToWeb" style="width: 1rem; height: 1rem" src="https://cdn.ipadbiz.cn/hager/icon/%E8%AF%AD%E8%A8%80@2x.png" fit="fit"></el-image>
<el-image v-if="!is_login" @click="goToLogin" style="width: 1rem; height: 1rem" src="https://cdn.ipadbiz.cn/hager/icon/%E7%99%BB%E9%99%86@2x.png" fit="fit"></el-image>
<el-image v-else @click="goToLogin" style="width: 1rem; height: 1rem" src="https://cdn.ipadbiz.cn/hager/icon/%E5%B7%B2%E7%99%BB%E5%BD%95@2x.png" fit="fit"></el-image>
</div>
</div>
<div style="display: flex; align-items: center; border-radius: 1rem; border: 1px solid #f5f5f5; background-color: #e3f1f7; padding: 0.5rem 1rem; margin-top: 1rem;">
<i class=el-icon-search style="color: #00477E;"></i>
<input style="border: 0;background-color: #e3f1f7; font-size: 1rem;" placeholder="搜索" v-model="keyword" @blur="goToSearch" @keydown.enter="goToSearch" />
</div>
<el-collapse-transition>
<div v-show="show_menu" class="menu-mini">
<hager-menu :menuData="product_menu" @close="closeMiniMenu"></hager-menu>
</div>
</el-collapse-transition>
</div>
</div>
</template>
<script>
import mixin from 'common/mixin';
import { parseQueryString } from '@/utils/tools';
import $ from 'jquery';
import hagerMenu from '../hagerMenu.vue';
import { getProductMenuAPI, getUserInfoAPI } from "@/api/hager.js";
// 处理菜单高度被挡住的问题
function isElementVisible($element) {
var windowHeight = $(window).height(); // 获取窗口高度
var windowScrollTop = $(window).scrollTop(); // 获取窗口的滚动高度
var elementOffsetTop = $element.offset().top; // 获取元素的顶部偏移
var elementHeight = $element.outerHeight(); // 获取元素高度
// 判断元素是否完全在可视区域内
if (elementOffsetTop >= windowScrollTop &&
elementOffsetTop + elementHeight <= windowScrollTop + windowHeight) {
return true; // 元素完全可见
} else if (elementOffsetTop < windowScrollTop ||
elementOffsetTop + elementHeight > windowScrollTop + windowHeight) {
return false; // 元素被挡住或部分被遮挡
}
}
export default {
components: { hagerMenu },
mixins: [mixin.init],
data () {
return {
show: false,
product_menu: [],
product_info: [],
is_active: '',
p_index: 0,
show_menu: false,
margin_top: 0,
// choose_country: false, // 主动选择了国家地区
keyword: '',
is_login: false,
show_border: false,
}
},
watch: {
// 监听路由参数变化时,更新输入框的值
'$route.params.keyword': function(newKeyword) {
this.keyword = newKeyword || '';
},
'$route' (to, from) {
this.is_active = to.meta.tag;
this.show = false;
if (to.path.indexOf('/product') !== -1 || to.path.indexOf('/about') !== -1) {
this.show_border = true;
} else {
this.show_border = false;
}
// 检查登录状态
this.getUserInfo();
},
'is_xs' (val) {
if (!val) {
$('body').css('overflow', 'auto');
}
},
'show' (val) {
this.$emit('onShowMenu', val)
}
},
computed: {
baseUrl () {
return location.origin;
}
},
async mounted () {
const { code, data } = await getProductMenuAPI();
if (code) {
this.product_menu = data;
// this.product_info = this.product_menu[0]['info'];
this.product_info = this.product_menu[0]['children'];
// 修改数据结构显示
// this.product_info = this.splitProductList(this.product_info);
}
// 获取用户信息
this.getUserInfo();
// let margin_top = 5; // 默认高度
// let once_header_height = 5; // 选择国家地区高度
// // let hidden_header_height = 2; // 可隐藏高度s
// if (this.choose_country) { // 没有点击国家地区
// margin_top += once_header_height;
// }
// this.$nextTick(() => {
// $('.wrapper').css('marginTop', margin_top + 'rem');
// })
// 监听屏幕滚动
// $('.wrapper').css('marginTop', margin_top + hidden_header_height + 'rem');
// window.addEventListener('scroll', (evt) => {
// if ($(window).scrollTop()) {
// $('.header-top').css('height', 0);
// $('.wrapper').css('marginTop', margin_top - hidden_header_height + 'rem');
// } else {
// $('.header-top').css('height', '2rem');
// $('.wrapper').css('marginTop', margin_top + hidden_header_height + 'rem');
// }
// });
//
window.addEventListener('resize', this.handleHeightResize);
},
beforeDestroy() {
// 在组件销毁前移除监听器,防止内存泄漏
window.removeEventListener('resize', this.handleHeightResize);
},
methods: {
goToHome () { // 跳转到首页
this.$router.push({
path: '/',
});
},
handleHeightResize () {
// 高度监听
this.$nextTick(() => {
var $productRightWrapper = $('.product-right-wrapper');
const productRightWrapper = document.querySelector('.product-right-wrapper');
// 获取窗口的高度
const windowHeight = $(window).height();
// 计算适合的高度,假设元素的 top 值固定,调整高度为可用高度
const topOffset = $('.product-right-wrapper').offset().top; // 获取元素距离顶部的偏移量
const availableHeight = windowHeight - topOffset;
if (isElementVisible($productRightWrapper)) {
// console.log(".product-right-wrapper 完全可见");
productRightWrapper.style.removeProperty('height');
productRightWrapper.style.removeProperty('overflow-y');
} else {
// console.log(".product-right-wrapper 被部分或完全遮挡");
// 设置 .product-right-wrapper 的最大高度,确保不会溢出窗口
productRightWrapper.style.height = availableHeight + 'px';
productRightWrapper.style.overflowY = 'scroll';
}
});
},
async getUserInfo () {
const { code, data } = await getUserInfoAPI();
if (code) {
if (data) {
this.is_login = true; // 已登录
} else {
this.is_login = false; // 未登录
}
}
},
splitProductList (products) {
const maxLength = 5; // 超过5个需要拆分
let result = [];
products.forEach(product => {
// 如果 list 长度不超过 5,则直接加入 result
if (product.list?.length <= maxLength || product.list === null) {
result.push(product);
} else {
// 如果 list 长度超过 5,则拆分
let listChunks = [];
for (let i = 0; i < product.list?.length; i += maxLength) {
listChunks.push(product.list.slice(i, i + maxLength));
}
// 第一个块保留原有的 name,后续块 name 设为空
listChunks.forEach((chunk, index) => {
if (index === 0) {
result.push({
id: product.id,
category_name: product.category_name,
list: chunk
});
} else {
result.push({
id: '',
category_name: '',
list: chunk
});
}
});
}
});
return result;
},
onClickProduct() {
// 当鼠标悬停时
this.show = true;
this.$nextTick(() => {
var $productRightWrapper = $('.product-right-wrapper');
const productRightWrapper = document.querySelector('.product-right-wrapper');
// 获取窗口的高度
const windowHeight = $(window).height();
// 计算适合的高度,假设元素的 top 值固定,调整高度为可用高度
const topOffset = $('.product-right-wrapper').offset().top; // 获取元素距离顶部的偏移量
const availableHeight = windowHeight - topOffset;
if (isElementVisible($productRightWrapper)) {
// console.log(".product-right-wrapper 完全可见");
productRightWrapper.style.removeProperty('height');
productRightWrapper.style.removeProperty('overflow-y');
} else {
// console.log(".product-right-wrapper 被部分或完全遮挡");
// 设置 .product-right-wrapper 的最大高度,确保不会溢出窗口
productRightWrapper.style.height = availableHeight + 'px';
productRightWrapper.style.overflowY = 'scroll';
}
});
},
onClick (item, index) {
this.product_info = item.children;
// 修改数据结构显示
this.product_info = this.splitProductList(this.product_info);
this.p_index = index
},
closeMenu () {
this.show = false;
},
goTo (path) { // 跳转页面
setTimeout(() => {
if (this.$route.path !== path) { // 不能重复点击当前页面
this.$router.push({
path,
});
}
}, 100);
if (path.indexOf('/solution') === -1) { // 不统计解决方案页面
// TAG: 统计埋点
this.maEvent(`h5_${path.slice(1)}`);
}
},
goToC (item) { // 跳转产品二级
this.$router.push({
path: `/product/index/${item.id}/${Date.now()}`,
});
this.closeMenu();
},
goToP (item) { // 跳转产品详情
this.$router.push({
path: `/product/detail/${item.id}/${item.category_id}/${Date.now()}`,
});
this.closeMenu();
},
goToWeb () {
window.open('https://hager.com/', '_blank')
},
goToLogin () {
if (this.is_login) {
this.$router.push({
path: '/user/info'
});
} else {
this.$router.push({
path: '/user/login'
});
}
},
openMenu () {
this.show_menu = !this.show_menu;
// TAG: 解决body滚动被禁止
if (this.show_menu) {
$('body').css('overflow', 'hidden');
// 设置自适应菜单高度
$('.menu-mini').css('top', $('.mobile-menu-wrapper').outerHeight());
} else {
$('body').css('overflow', 'auto');
}
},
closeMiniMenu () {
this.show_menu = false;
$('body').css('overflow', 'auto');
},
goToSearch () {
if (this.keyword) {
this.updateURL()
}
},
updateURL() {
const currentKeyword = this.$route.params.keyword;
// 如果输入的 keyword 和当前路由的 keyword 相同,不做更新
if (this.keyword === currentKeyword) return;
// 使用 Vue Router 动态更新 URL 中的 keyword 参数
this.$router.replace({
path: `/search/${this.keyword}`
});
}
}
}
</script>
<style lang="less" scoped>
.hager-header {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 9;
.location-banner {
display: flex;
background: linear-gradient(0deg, rgba(0, 0, 0, .2), rgba(0, 0, 0, .2)), #00477e;
width: 100%;
height: 5rem;
color: white;
}
.header-top {
display: flex;
background-color: #00477e;
height: 2rem;
transition: all .4s ease-in-out;
}
.product-center {
position: absolute;
top: 5rem;
left: 0;
width: 100%;
height: auto;
background-color: #fff;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
.product-left-wrapper {
flex-basis: 33.33%;
padding: 1rem;
border-top: 1px solid rgba(218, 218, 218, 0.4);
&:hover {
cursor: pointer;
}
.product-left-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 1rem;
&.active {
background-color: @background-color;
border-radius: 5px;
}
i {
color: #F56400;
}
.icon-text {
font-size: 0.95rem;
display: flex;
align-items: center;
}
}
}
.product-right-wrapper {
flex-basis: 66.67%;
background-color: @background-color;
padding: 1.75rem 1.5rem;
box-sizing:border-box;
.product-right-title {
display: flex;
justify-content: space-between;
margin-bottom: 0.25rem;
.text {
font-size: 1.25rem;
color: @text-color;
font-weight: bold;
}
i {
&:hover {
cursor: pointer;
}
}
}
.closeBtn {
position: absolute;
right: 1rem;
top: 1rem;
&:hover {
cursor: pointer;
}
}
.product-right-desc {
margin-bottom: 1.5rem;
color: @secondary-text-color;
}
.product-right-center {
display: flex;
flex-wrap: wrap;
// justify-content: space-between;
.product-right-center-item {
flex-basis: calc(33.33%);
margin-bottom: 10px;
.header-text {
font-size: 0.95rem;
margin-bottom: 0.25rem;
color: @text-color;
&:hover {
cursor: pointer;
text-decoration: underline;
}
}
.item-text {
font-size: 0.95rem;
line-height: 1.75;
color: @secondary-text-color;
&:hover {
cursor: pointer;
text-decoration: underline;
color: #F56400;
}
}
}
}
}
}
.nav-logo {
display: flex;
height: 5rem;
align-items: center;
&:hover {
cursor: pointer;
}
}
.nav-wrapper {
display: flex;
height: 5rem;
// align-items: center;
// justify-content: space-around;
margin-top: 1.25rem;
.item {
text-decoration: none;
color: @secondary-color;
margin-right: 1.5rem;
font-size: 0.95rem;
height: 1.5rem;
&:hover {
cursor: pointer;
color: @primary-color;
}
}
.active {
color: @primary-color;
}
}
.nav-right {
display: flex;
height: 5rem;
align-items: center;
justify-content: right;
.search {
display: flex;
align-items: center;
border-radius: 1rem;
border: 1px solid #E3F1F7;
background-color: #E3F1F7;
padding: 0.5rem 2rem 0.5rem 1rem;
overflow: hidden;
width: 100%;
input {
border: 0;
background-color: #E3F1F7;
margin-left: 0.5rem;
width: calc(100% - 0.5rem);
&::placeholder {
color: #00477E;
}
}
}
.tooltip {
margin: 0 1rem;
&:hover {
cursor: pointer;
}
}
}
.menu-mini {
position: absolute;
top: 6.7rem;
right: 0;
left: 0;
width: 100vw;
height: calc(100vh - 6.7rem);
background-color: #F7F7F7;
}
.mobile-menu-wrapper {
background-color: #fff;
padding: 0.5rem 1rem;
box-sizing: border-box;
.mobile-menu {
display: flex;
align-items: center;
justify-content: space-between;
}
}
}
</style>