xuruiqian
2025-06-04 17ce21955b4e3402d3d5868b52e50bfdd55bc572
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
<template>
    <!-- 默认使用系统头部,如需开启自定义头部配置
        1. pages.json -> globalStyle -> navigationStyle: custom
        2. common -> config -> navbar: custom
    -->
    <view v-if="iShow">
        <view class="use-navbar w-full pos-t-full" style="padding-bottom: 6px;" :style="{ 'background': bgColor, 'opacity': bgOpacity }" :class="{ 'fixed': fixed, 'bg-main': type == 'white', 'bg-drak': type == 'gray' }">
            <view class="w-full" :style="{ height: menuButtonInfo.top + 'px' }"></view>
 
            <!-- 头部导航 -->
            <view class="dflex-b border-radius-lg padding-lr w-full use-hover" :style="{ height: navbarTitleHeight + 'px' }">
                <view class="wpre-30 dflex">
                    <view v-if="back" class="iconfont fwbd" :class="leftIconfont" @click="toback"></view>
                    <block v-else>
                        <slot name="left" />
                    </block>
                </view>
                <view>
                    <block v-if="type == 'custom'">
                        <slot />
                    </block>
                    <block v-else>
                        <text class="fwbd fs">{{ apptitle }}</text>
                    </block>
                </view>
                <view class="wpre-30">
                    <slot name="right" />
                </view>
            </view>
        </view>
 
        <!-- 头部组件占位符 -->
        <view v-if="fixed && placeholder" :style="{ height: navbarHeight + 'px' }"></view>
    </view>
</template>
 
<script>
    export default {
        props: {
            fixed: {
                type: [Number, Boolean],
                default: !0
            },
            placeholder: {
                type: [Number, Boolean],
                default: !0
            },
            back: {
                type: Boolean,
                default: !0
            },
            title: {
                type: String,
                default: ''
            },
            type: {
                type: String,
                default: 'gray'
            },
            leftIcon: {
                type: String,
                default: 'iconfanhui'
            },
            bgColor: {
                type: String,
                default: '#f5f5f5'
            },
            bgOpacity: {
                type: Number,
                default: .97
            },
            show: {
                type: Boolean,
                default: null
            }
        },
        data() {
            return {
                apptitle: ''
            };
        },
        watch: {
            title (nv, ov) {
                this.apptitle = nv;
            }
        },
        computed: {
            menuButtonInfo() {
                let menuButtonInfo = { top: 6 };
                // #ifdef MP-WEIXIN
                menuButtonInfo = uni.getMenuButtonBoundingClientRect();
                console.log('menuButtonInfo', menuButtonInfo);
                // #endif
 
                // #ifdef H5-WEIXIN
                menuButtonInfo.top = 6;
                // #endif
 
                return menuButtonInfo;
            },
            navbarTitleHeight() {
 
                return 32;
            },
            navbarHeight() {
                if (this.iShow) {
                    return this.menuButtonInfo.top + this.navbarTitleHeight + 6;
                }
 
                return 0;
            },
            leftIconfont() {
                const pageArrs = getCurrentPages();
 
                if (!pageArrs.length) return '';
 
                if (pageArrs[pageArrs.length - 1].route.indexOf('/tabbar/') !== -1) {
                    return '';
                }
 
                if (pageArrs.length < 2) {
                    return 'iconshouye- fs-xl';
                }
 
                return this.leftIcon
            },
            config() {
                return this.$config;
            },
            iShow() {
                return this.show !== null ? this.show : this.config.navbar == 'custom';
            }
        },
        methods: {
            toback() {
                if (getCurrentPages().length < 2) {
                    this.$navigator.tohome();
                    return;
                }
 
                uni.navigateBack({
                    delta: 1
                });
            },
        },
        created() {
            this.apptitle = "aa";
        }
    };
</script>
 
<style lang="scss">
    .use-navbar-placeholder {
        min-height: 44px;
    }
 
    .use-navbar {
        min-height: 44px;
        z-index: 99999;
    }
 
    .use-search {
        height: 70rpx;
        line-height: 70rpx;
 
        text {
            color: #c0c0c0;
        }
 
        .iconfont {
            font-size: $font-base + 6upx;
            color: #c0c0c0;
        }
    }
</style>